java - 在 itext 中添加表格的单元格

标签 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的新手。如果有人能向我解释如何通过 itext 生成 pdf 表单,那就太好了。预先感谢:)

最佳答案

您将创建一种白色字体并将其绘制在通常显示为白色的 Canvas 上。它就在那里,只是你看不到而已。尝试将颜色更改为其他颜色,例如 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

关于java - 在 itext 中添加表格的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24684337/

相关文章:

java - TransactionRolledbackLocalException 客户端的事务在访问@Singleton 时中止

java - 创建一个方法来打印先前创建的数组

java - 如何存储 ArrayList<HashMap<String, String>>

java - 如何以正确的顺序使用 iText 从 PDF 中提取图像?

java - 在java中实现blowfish和RC5

java - 使用 Spring Integration 和 RabbitMQ 时如何在消费者端处理格式错误的消息

java - iText/列表 : How to remove symbol from list?

java - 使用 Flying Saucer 的 PDF 标题后的巨大空白

java - com.itextpdf.kernel.PdfException : Document has no pages

c# - 通过 ItextSharp 合并 PDF