📜  使用Java将嵌套表添加到 PDF

📅  最后修改于: 2022-05-13 01:54:43.480000             🧑  作者: Mango

使用Java将嵌套表添加到 PDF

我们可以通过安装文档类将嵌套表添加到 PDF 中。在实例化此类时,您希望将 PdfDocument 对象作为参数传递给其构造函数。然后,要将表格添加到文档中,您需要实例化 Table 类并使用 add() 方法将此对象添加到文档中。

要将表添加到该表中,您需要创建另一个表(嵌套表),并使用 Cell 类的 add() 方法将其传递给单元对象。

以下是使用Java将嵌套表添加到 PDF 的步骤:

1.创建一个PDF writer对象

此处的 PdfWriter 类代表 PDF 的 DocWriter。此类属于包 com.itextpdf.kernel.pdf。此类的构造函数接受一个字符串,表示要创建 PDF 的文件的路径。

通过将字符串值(表示您要制作 PDF 的路径)传递给其构造函数来创建 PdfWriter 类。

2. 创建一个 PdfDocument 对象

PdfDocument 类是在 iText 中表示 PDF 文档的类。此类属于包 com.itextpdf.kernel.pdf。要创建此类(在写作模式下),您需要将 PdfWriter 类别的对象传递给其构造函数。

通过将上面创建的 PdfWriter 对象传递给其构造函数来创建 PdfDocument 类。

3. 创建文档对象

包 com.itextpdf.layout 的 Document 类是创建自给自足的 PDF 时的根元素。此类的构造函数之一接受类别 PdfDocument 的对象。

通过传递在前面的步骤中创建的类别 PdfDocument 的事物来创建 Document 类。

4.创建一个Table对象

Table 类表示一个二维网格,里面塞满了单元格,按行和列排序。它属于 com.itextpdf.layout.element 包。

5. 创建单元格

通过创建包 com.itextpdf.layout 的 Cell 类来创建单元对象。

6. 创建嵌套表

创建单元格后,创建一个嵌套表,并填充其单元格。

7.在单元格中添加嵌套表

使用 Cell 类的 add() 方法将上一步创建的嵌套表添加到容器表的单元格中。使用 Table 类的 addCell() 方法将此单元格添加到容器表

8.将表格添加到文档中

使用 Document 类的 add() 方法添加上一步创建的 table 对象

9.关闭文档

使用 Document 类的 close() 方法关闭文档

现在,让我们看看如何应用这些步骤的一些示例

Java
// Java Program to add Nested Tables to a PDF
  
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 nestedTablesPdf {
    public static void main(String args[]) throws Exception
    {
        // Creating a PdfWriter object
        String destination
            = "C:/itextExamples/addingNestedTable.pdf";
        PdfWriter writer = new PdfWriter(destination);
  
        // Creating a PdfDocument object
        PdfDocument pdfDoc = new PdfDocument(writer);
  
        // Creating a Document object
        Document doc = new Document(pdfDoc);
  
        // Creating a table
        float[] pointColumnWidths1 = { 130f, 130f };
        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("Mayank Tyagi");
        table.addCell(cell2);
  
        // Populating row 2 and adding it to the table
        Cell cell3 = new Cell();
        cell3.add("Designation");
        table.addCell(cell3);
  
        Cell cell4 = new Cell();
        cell4.add("Tech. Content Writer");
        table.addCell(cell4);
  
        // Populating row 3 and adding it to the table
        Cell cell5 = new Cell();
        cell5.add("Company");
        table.addCell(cell5);
  
        Cell cell6 = new Cell();
        cell6.add("GeeksforGeeks");
        table.addCell(cell6);
  
        // Creating nested table for contact
        float[] pointColumnWidths2 = { 130f, 130f };
        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("1122334455");
        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("Mayanktyagi1709@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("Delhi");
        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("Table successfully added");
    }
}


输出

将嵌套表格添加到 PDF