如何通过Java的iTextPDF库制作一个PDF表格模板并填充数据
要使用Java的iTextPDF库制作一个PDF表格模板并填充数据,你需要遵循以下步骤:
- 添加依赖:首先,确保你的项目中包含了iTextPDF库的依赖。如果你使用Maven,可以在你的
pom.xml
文件中添加以下依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
-
创建PDF模板:你可以使用iTextPDF创建一个简单的PDF模板,或者使用其他工具(比如Adobe Acrobat)创建PDF模板,并在模板中添加表格。
-
填充表格数据:使用iTextPDF API向PDF模板中的表格填充数据。
下面是一个简单的例子,演示如何使用PDFBox创建一个包含表格的PDF文档,并向表格中填充数据:
import com.google.common.collect.Lists;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
public class ITextPDFExample {
public static void main(String[] args) {
// 保存的pdf全路径
String outPdfPath = "/path/out.pdf";
// pdf中表格需要填充的数据
List<List<String>> data = Lists.newArrayList();
data.add(Lists.newArrayList("列1值", "列2值", "列3值", "列4值", "列5值"));
//创建文件
Document document = new Document(PageSize.A4);
File file = new File(outPdfPath);
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
//打开文件
document.open();
//BaseFont-确认支持中文
String fontPath = "/path/to/your/chinese/font.ttf";
// 创建BaseFont对象,指定字体路径和编码
BaseFont bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChineseTitle = new Font(bfChinese, 12, Font.NORMAL);
String contentTitle = "标题";
Paragraph paragraphTitle = new Paragraph(contentTitle, fontChineseTitle);
paragraphTitle.setAlignment(Element.ALIGN_CENTER);
document.add(paragraphTitle);
Paragraph blankRow1 = new Paragraph(18f, " ", fontChineseTitle);
document.add(blankRow1);
Font fontChineseParagraph = new Font(bfChinese, 12, Font.NORMAL);
String contentParagraph2 = "文本内容1";
Paragraph paragraph2 = new Paragraph(contentParagraph2, fontChineseParagraph);
paragraph2.setFirstLineIndent(28);
document.add(paragraph2);
paragraphTitle.setSpacingAfter(100);
paragraphTitle.setSpacingBefore(100);
Paragraph blankRow2 = new Paragraph(18f, " ", fontChineseParagraph);
document.add(blankRow2);
Font fontChineseTable = new Font(bfChinese, 12, Font.NORMAL);
PdfPTable table = new PdfPTable(5);
List<PdfPRow> listRow = table.getRows();
//设置列宽
float[] columnWidths = {10, 27, 24, 16, 23};
table.setWidths(columnWidths);
//行1
PdfPCell[] cells1 = new PdfPCell[5];
PdfPRow row1 = new PdfPRow(cells1);
//单元格
cells1[0] = new PdfPCell(new Paragraph("列1", fontChineseTable));
cells1[0].setBorderColor(BaseColor.BLACK);
cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
cells1[1] = new PdfPCell(new Paragraph("列2", fontChineseTable));
cells1[1].setBorderColor(BaseColor.BLACK);
cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
cells1[2] = new PdfPCell(new Paragraph("列3", fontChineseTable));
cells1[2].setBorderColor(BaseColor.BLACK);
cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
cells1[3] = new PdfPCell(new Paragraph("列4", fontChineseTable));
cells1[3].setBorderColor(BaseColor.BLACK);
cells1[3].setVerticalAlignment(Element.ALIGN_MIDDLE);
cells1[4] = new PdfPCell(new Paragraph("列5", fontChineseTable));
cells1[4].setBorderColor(BaseColor.BLACK);
cells1[4].setVerticalAlignment(Element.ALIGN_MIDDLE);
//把第一行添加到集合
listRow.add(row1);
for (int i = 0; i < data.size(); i++) {
PdfPCell[] cellsi = new PdfPCell[5];
PdfPRow rowi = new PdfPRow(cellsi);
cellsi[0] = new PdfPCell(new Paragraph(data.get(i).get(0), fontChineseParagraph));
cellsi[0].setBorderColor(BaseColor.BLACK);
cellsi[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
cellsi[1] = new PdfPCell(new Paragraph(data.get(i).get(1), fontChineseParagraph));
cellsi[1].setBorderColor(BaseColor.BLACK);
cellsi[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
cellsi[2] = new PdfPCell(new Paragraph(data.get(i).get(2), fontChineseParagraph));
cells1[2].setBorderColor(BaseColor.BLACK);
cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
cellsi[3] = new PdfPCell(new Paragraph(data.get(i).get(3), fontChineseParagraph));
cellsi[3].setBorderColor(BaseColor.BLACK);
cellsi[3].setVerticalAlignment(Element.ALIGN_MIDDLE);
cellsi[4] = new PdfPCell(new Paragraph(data.get(i).get(4), fontChineseParagraph));
cellsi[4].setBorderColor(BaseColor.BLACK);
cellsi[4].setVerticalAlignment(Element.ALIGN_MIDDLE);
listRow.add(rowi);
}
//把表格添加到文件中
document.add(table);
Paragraph blankRow3 = new Paragraph(18f, " ", fontChineseParagraph);
document.add(blankRow3);
String contentParagraph4 = "文本内容2";
Paragraph paragraph4 = new Paragraph(contentParagraph4, fontChineseParagraph);
paragraph4.setAlignment(Element.ALIGN_RIGHT);
document.add(paragraph4);
String contentParagraph5 = "文本内容3";
;
Paragraph paragraph5 = new Paragraph(contentParagraph5, fontChineseParagraph);
paragraph5.setAlignment(Element.ALIGN_RIGHT);
document.add(paragraph5);
//关闭文档
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个例子中,你需要替换fontPath的值为你的中文字体文件的实际路径。BaseFont.IDENTITY_H是指定字体编码的参数,它表示字体将支持Unicode字符集的水平显示,这对于渲染中文字符是必须的。BaseFont.NOT_EMBEDDED参数表示字体不会被嵌入到PDF文档中,如果你想要确保PDF在不同设备上的兼容性,你可能需要将字体嵌入到PDF中,这时可以将此参数改为BaseFont.EMBEDDED。
如果你的PDF模板是预先存在的,并且包含可编辑的表单字段,你可以使用PDDocument
和PDAcroForm
类来填充这些字段,而不是手动绘制表格。这通常是处理复杂模板的更好方法。
Java的iTextPDF库和Apache PDFBox库都是用于生成PDF文档的开源库,它们有一些区别,适用于不同的场景。
- iTextPDF库:
- iTextPDF是一个功能强大且灵活的PDF库,支持创建、修改和处理PDF文档。
- iTextPDF提供了丰富的API和功能,可以创建复杂的PDF文档,包括表格、图像、水印、书签等。
- iTextPDF支持各种字体、颜色和样式的自定义,可以实现高度定制化的PDF生成。
- iTextPDF有商业和开源版本可用,商业版本提供了更多的功能和支持。
适用场景:
- 需要创建复杂的PDF文档,包括表格、图像、水印、书签等。
- 需要高度定制化的PDF生成。
- 需要商业版本提供的额外功能和支持。
- Apache PDFBox库:
- Apache PDFBox是一个功能强大的PDF库,主要用于处理和操作PDF文档。
- PDFBox提供了一系列的API,可以读取、创建、修改和提取PDF文档的内容。
- PDFBox支持文本提取、图像提取、加密解密、表单处理等功能。
- PDFBox的API相对较底层,需要开发者对PDF文档结构和操作有一定的了解。
适用场景:
- 需要处理和操作PDF文档的内容,如文本提取、图像提取、加密解密、表单处理等。
- 对PDF文档结构和操作有一定的了解。
总的来说,如果你需要创建复杂的PDF文档并且需要高度定制化,可以选择iTextPDF库。如果你主要需要处理和操作PDF文档的内容,可以选择Apache PDFBox库。