- iText 教程
- iText - 首页
- iText 画布
- iText - 绘制圆弧
- iText - 绘制线条
- iText - 绘制圆形
- iText 有用资源
- iText - 快速指南
- iText - 有用资源
- iText - 讨论
iText - 嵌套表格
本章我们将学习如何使用 iText 库在 PDF 文档中向表格添加嵌套表格。
在 PDF 中添加嵌套表格
您可以通过实例化Document类来创建一个空的 PDF 文档。实例化此类时,需要将PdfDocument对象作为参数传递给它的构造函数。然后,要向文档添加表格,需要实例化Table类并将此对象使用add()方法添加到文档中。
要向此表格添加表格,需要创建另一个表格(嵌套表格),并使用Cell类的add()方法将其传递给单元格对象。
以下是将表格插入表格单元格的步骤。
步骤 1:创建 PdfWriter 对象
PdfWriter类表示 PDF 的 DocWriter。此类属于com.itextpdf.kernel.pdf包。此类的构造函数接受一个字符串,表示要创建 PDF 文件的路径。
通过将字符串值(表示需要创建 PDF 的路径)传递给其构造函数来实例化 PdfWriter 类,如下所示。
// Creating a PdfWriter String dest = "C:/itextExamples/addingNestedTable.pdf"; PdfWriter writer = new PdfWriter(dest);
当此类型的对象传递给 PdfDocument(类)时,添加到此文档的每个元素都将写入指定的文件。
步骤 2:创建 PdfDocument 对象
PdfDocument类是表示 iText 中 PDF 文档的类。此类属于com.itextpdf.kernel.pdf包。要实例化此类(在写入模式下),需要将PdfWriter类的对象传递给其构造函数。
通过将上面创建的 PdfWriter 对象传递给其构造函数来实例化 PdfDocument 类,如下所示。
// Creating a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
创建 PdfDocument 对象后,可以使用其类提供的相应方法添加各种元素,例如页面、字体、文件附件和事件处理程序。
步骤 3:创建 Document 对象
com.itextpdf.layout包中的Document类是创建自包含 PDF 时的根元素。此类的构造函数之一接受PdfDocument类的对象。
通过传递在前面步骤中创建的PdfDocument类的对象来实例化Document类,如下所示。
// Creating a Document Document document = new Document(pdfDoc);
步骤 4:创建 Table 对象
Table类表示一个二维网格,其中填充了单元格,按行和列排序。它属于com.itextpdf.layout.element包。
如下所示实例化Table类。
// Creating a table
float [] pointColumnWidths = {200F, 200F};
Table table = new Table(pointColumnWidths);
步骤 5:创建单元格
通过实例化com.itextpdf.layout包中的Cell类来创建一个cell对象,如下所示。
// Adding cell to the table Cell contact = new Cell(); // Creating a cell
步骤 6:创建嵌套表格
创建cell后,创建嵌套表格并填充其单元格,如下所示。
// Creating nested table for contact
float [] pointColumnWidths2 = {150f, 150f};
Table nestedTable = new Table(pointColumnWidths2);
// Populating row 1 and adding it to the nested table
Cell nested1 = new Cell();
nested1.add("Phone");
nestedTable.addCell(nested1);
Cell nested2 = new Cell();
nested2.add("9848022338");
nestedTable.addCell(nested2);
// Populating row 2 and adding it to the nested table
Cell nested3 = new Cell();
nested3.add("email");
nestedTable.addCell(nested3);
Cell nested4 = new Cell();
nested4.add("Raju123@gmail.com");
nestedTable.addCell(nested4);
// Populating row 3 and adding it to the nested table
Cell nested5 = new Cell();
nested5.add("Address");
nestedTable.addCell(nested5);
Cell nested6 = new Cell();
nested6.add("Hyderabad");
nestedTable.addCell(nested6);
步骤 7:将嵌套表格添加到单元格
现在,使用Cell类的add()方法将上面创建的嵌套表格添加到父(容器)表格的单元格中。并使用Table类的addCell()方法将此单元格添加到父表格中,如下所示。
contact.add(nestedTable); table.addCell(contact);
步骤 8:将表格添加到文档
使用Document类的add()方法添加上一步中创建的table对象,如下所示。
// Adding list to the document document.add(table);
步骤 9:关闭文档
使用Document类的close()方法关闭文档,如下所示。
// Closing the document document.close();
示例
下面的 Java 程序演示了如何使用 iText 库在 PDF 文档中向表格的单元格添加表格(嵌套表格)。它创建一个名为addingNestedTable.pdf的 PDF 文档,向其中添加一个表格,将另一个表格插入其单元格之一,并将其保存在C:/itextExamples/路径中。
将此代码保存在名为AddNestedTable.java的文件中。
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
public class a4AddNestedTablesPdf {
public static void main(String args[]) throws Exception {
// Creating a PdfWriter object
String dest = "C:/itextExamples/addingNestedTable.pdf";
PdfWriter writer = new PdfWriter(dest);
// Creating a PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Creating a table
float [] pointColumnWidths1 = {150f, 150f};
Table table = new Table(pointColumnWidths1);
// Populating row 1 and adding it to the table
Cell cell1 = new Cell();
cell1.add("Name");
table.addCell(cell1);
Cell cell2 = new Cell();
cell2.add("Raju");
table.addCell(cell2);
// Populating row 2 and adding it to the table
Cell cell3 = new Cell();
cell3.add("Id");
table.addCell(cell3);
Cell cell4 = new Cell();
cell4.add("1001");
table.addCell(cell4);
// Populating row 3 and adding it to the table
Cell cell5 = new Cell();
cell5.add("Designation");
table.addCell(cell5);
Cell cell6 = new Cell();
cell6.add("Programmer");
table.addCell(cell6);
// Creating nested table for contact
float [] pointColumnWidths2 = {150f, 150f};
Table nestedTable = new Table(pointColumnWidths2);
// Populating row 1 and adding it to the nested table
Cell nested1 = new Cell();
nested1.add("Phone");
nestedTable.addCell(nested1);
Cell nested2 = new Cell();
nested2.add("9848022338");
nestedTable.addCell(nested2);
// Populating row 2 and adding it to the nested table
Cell nested3 = new Cell();
nested3.add("email");
nestedTable.addCell(nested3);
Cell nested4 = new Cell();
nested4.add("Raju123@gmail.com");
nestedTable.addCell(nested4);
// Populating row 3 and adding it to the nested table
Cell nested5 = new Cell();
nested5.add("Address");
nestedTable.addCell(nested5);
Cell nested6 = new Cell();
nested6.add("Hyderabad");
nestedTable.addCell(nested6);
// Adding table to the cell
Cell cell7 = new Cell();
cell7.add("Contact");
table.addCell(cell7);
Cell cell8 = new Cell();
cell8.add(nestedTable);
table.addCell(cell8);
// Adding table to the document
doc.add(table);
// Closing the document
doc.close();
System.out.println("Nested Table Added successfully..");
}
}
使用以下命令从命令提示符编译并执行保存的 Java 文件:
javac AddNestedTable.java java AddNestedTable
执行后,上述程序将创建一个显示以下消息的 PDF 文档。
Nested Table Added successfully..
如果验证指定的路径,则可以找到创建的 PDF 文档,如下所示。