25

iTextを使用して、テーブルを含むpdfまたはrtfファイルを生成するプログラムを作成しています。どちらのファイルも最後に生成できるように、より具体的な RtfTable や pdfTable ではなく、iText クラスのテーブルとセルを使用しました。セルのパディングを -1 の値に設定する必要がありました。そうしないと、印刷されたシートのデータの各行の間にスペースがありすぎました。ただし、境界線を (特に pdf ファイルに) 追加しようとしていますが、セルがテキストと並んでいません。各セルの下枠は、テキストを直接切り取ります。セルのパディングが 2 以上に設定されている場合にのみ、実際にテキストが囲まれます。以下は私がやっていることのサンプルです:

  Document document = new Document();
  Paragraph paragraph = new Paragraph();
  Font iTextFont = new Font(Font.TIMES_ROMAN, 9, Font.NORMAL);
  try{
    PdfWriter.getInstance(document, new FileOutputStream("C:/datafiles/TestiText.pdf"));
    document.open();

    Table table = new Table(3);
    table.setPadding(-1);
    table.setWidth(90);
    Cell cell1 = new Cell();
    cell1.setBorder(Rectangle.BOX);
    cell1.setVerticalAlignment(ElementTags.ALIGN_TOP);
    table.setDefaultCell(cell1);
    paragraph = new Paragraph("header", iTextFont);
    Cell cell = new Cell(paragraph);
    cell.setHeader(true);
    cell.setColspan(3);
    table.addCell(cell);
    paragraph = new Paragraph("example cell", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("one", iTextFont);
            table.addCell(cell);
    paragraph = new Paragraph("two", iTextFont);
    cell = new Cell(paragraph);
    table.addCell(paragraph);
    paragraph = new Paragraph("Does this start a new row?", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("Four", iTextFont);
    table.addCell(paragraph);
    paragraph = new Paragraph("Five", iTextFont);
    table.addCell(paragraph);
    document.add(table);
  } catch (Exception e) {
    //handle exception
  }
  document.close();

  }

この問題を解決する方法はありますか (テキストの配置に影響を与えずに) 境界線全体を下に移動するか、各行の間のスペースを取り除きます (スペースはテキストの上にのみ問題があり、下にはありません)。セルのパディングを -1 に設定せずに?

4

2 に答える 2

1

TableまたはPdfPTableのどちらを使用しているかにかかわらず、テーブルを作成するためのクラスまたは一般的なメソッドを記述します。

これらのメソッドは、標準の配置、アセンダー/ディセンダーに基づく測定などを処理します。また、「3pt空白段落」またはその他の標準フォーマットを追加するための一般的な場所を提供します。

OOソフトウェアは、コードの反復的で、潜在的に一貫性のないセクションを排除することを目的としたものではありません。

お役に立てれば。

于 2012-09-03T12:05:53.973 に答える
0

便利 PdfPTableで適切にラップされたコンポーネントがたくさんあるので、この回答を投稿したので、同じ問題に直面している人は誰でも、どこから始めればよいかを知っているので、質問に対する典型的な回答ではないかもしれませんが、ここに来ます...

表内のコンテンツの編成
PDF 出力

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class Spacing {

    /** The resulting PDF file. */
    public static final String RESULT = "results/part1/chapter04/spacing.pdf";

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100);
        Phrase p = new Phrase(
            "Dr. iText or: How I Learned to Stop Worrying " +
            "and Love the Portable Document Format.");
        PdfPCell cell = new PdfPCell(p);
        table.addCell("default leading / spacing");
        table.addCell(cell);
        table.addCell("absolute leading: 20");
        cell.setLeading(20f, 0f);
        table.addCell(cell);
        table.addCell("absolute leading: 3; relative leading: 1.2");
        cell.setLeading(3f, 1.2f);
        table.addCell(cell);
        table.addCell("absolute leading: 0; relative leading: 1.2");
        cell.setLeading(0f, 1.2f);
        table.addCell(cell);
        table.addCell("no leading at all");
        cell.setLeading(0f, 0f);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(
            "Dr. iText or: How I Learned to Stop Worrying and Love PDF"));
        table.addCell("padding 10");
        cell.setPadding(10);
        table.addCell(cell);
        table.addCell("padding 0");
        cell.setPadding(0);
        table.addCell(cell);
        table.addCell("different padding for left, right, top and bottom");
        cell.setPaddingLeft(20);
        cell.setPaddingRight(50);
        cell.setPaddingTop(0);
        cell.setPaddingBottom(5);
        table.addCell(cell);
        p = new Phrase("iText in Action Second Edition");
        table.getDefaultCell().setPadding(2);
        table.getDefaultCell().setUseAscender(false);
        table.getDefaultCell().setUseDescender(false);
        table.addCell("padding 2; no ascender, no descender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(false);
        table.addCell("padding 2; ascender, no descender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(false);
        table.getDefaultCell().setUseDescender(true);
        table.addCell("padding 2; descender, no ascender");
        table.addCell(p);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);
        table.addCell("padding 2; ascender and descender");
        cell.setPadding(2);
        cell.setUseAscender(true);
        cell.setUseDescender(true);
        table.addCell(p);
        document.add(table);
        // step 5
        document.close();
    }
}
于 2012-06-20T14:46:07.263 に答える