在itext中添加表的单元格

时间:2014-07-10 19:02:03

标签: java itext

为什么包含段落p的单元格没有被添加到表中?

package iText;

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.*;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class NewMain1 {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("C:\\Users\\om\\Desktop\\pdf\\2.pdf"));

            document.open();

            PdfPTable table = new PdfPTable(3); // 3 columns.
             Font font = new Font(FontFamily.HELVETICA, 22, Font.BOLD, BaseColor.WHITE);
        Paragraph p = new Paragraph("xyz", font);
        table.addCell("abc");    
        table.addCell(p);

            table.addCell("cef");
            table.addCell("ghi");
           // table.completeRow();
            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);

            document.add(table);

            document.close();
        } catch(Exception e){

        }
    }
}

p.s我是这个版本的新手。如果有人能解释我如何通过itext生成pdf表单会很棒。在此先感谢:)

1 个答案:

答案 0 :(得分:1)

您将创建一个白色字体并将其绘制在通常显示为白色的画布上。它在那里,你只是看不到它。尝试将颜色更改为其他内容,例如BaseColor.BLACK

修改

要回答评论中的问题,Document构造函数的一个重载采用一个矩形来定义页面的默认大小。 iText有一个名为PageSize的辅助类,它定义了许多常见页面,但您可以自由使用3 x 314,400 x 14,400之间的任何维度。例如,您可以说new Document(PageSize.LETTER)new Document(new RectangleReadOnly(612, 702))

一旦你知道,你已经获得了最大值xy,除非你做了一件非常奇怪的事情,否则你的最低xy都是零。对于PDF,左下角是原点,因此是0 x 0