简体   繁体   English

在itext中添加表的单元格

[英]adding cell of a table in itext

why is the cell containing paragraph p not being added to table? 为什么包含段落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){

        }
    }
}

ps I'm a newbie at this itext. ps我是这个itext的新手。 It would be great if anyone could explain me how to generate pdf forms through itext. 如果有人能解释我如何通过itext生成pdf表格,那就太好了。 Thanks in advance :) 提前致谢 :)

You're create a white font and drawing it on a canvas that is usually displayed as white. 您将创建一种白色字体并将其绘制在通常显示为白色的画布上。 It is there, you just can't see it. 它在那里,您只是看不到它。 Try changing the color to something else like BaseColor.BLACK . 尝试将颜色更改为BaseColor.BLACK其他颜色。

EDIT 编辑

To answer the question in your comment, one of the overloads for the Document constructor takes a rectangle that defines the default size for pages. 为了回答您评论中的问题, Document构造函数的重载之一采用一个矩形,该矩形定义页面的默认大小。 iText has a helper class called PageSize that defines many common pages but you are free to use any dimension from 3 x 3 up to 14,400 x 14,400 . iText有一个名为PageSize的帮助程序类,该类定义了许多常见的页面,但是您可以自由使用3 x 314,400 x 14,400任何尺寸。 For instance, you could say new Document(PageSize.LETTER) or new Document(new RectangleReadOnly(612, 702)) . 例如,您可以说new Document(PageSize.LETTER)new Document(new RectangleReadOnly(612, 702))

Once you know that, you've got your maximum x and y and unless you do something really weird your minimum x and y are both zero. 知道这一点后,您便拥有了xy的最大值,并且除非您确实做了一些奇怪的事情,否则最小值xy均为零。 With PDFs, the bottom left corner is the origin point and thus 0 x 0 . 对于PDF,左下角是原点,因此是0 x 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM