Java利用iTextPDF庫實現(xiàn)制作PDF表格模板并填充數(shù)據(jù)
要使用Java的iTextPDF庫制作一個PDF表格模板并填充數(shù)據(jù),你需要遵循以下步驟:
添加依賴:首先,確保你的項目中包含了iTextPDF庫的依賴。如果你使用Maven,可以在你的pom.xml文件中添加以下依賴:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
創(chuàng)建PDF模板:你可以使用iTextPDF創(chuàng)建一個簡單的PDF模板,或者使用其他工具(比如Adobe Acrobat)創(chuàng)建PDF模板,并在模板中添加表格。
填充表格數(shù)據(jù):使用iTextPDF API向PDF模板中的表格填充數(shù)據(jù)。
下面是一個簡單的例子,演示如何使用PDFBox創(chuàng)建一個包含表格的PDF文檔,并向表格中填充數(shù)據(jù):
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中表格需要填充的數(shù)據(jù)
List<List<String>> data = Lists.newArrayList();
data.add(Lists.newArrayList("列1值", "列2值", "列3值", "列4值", "列5值"));
//創(chuàng)建文件
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";
// 創(chuàng)建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 = "文本內(nèi)容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 = "文本內(nèi)容2";
Paragraph paragraph4 = new Paragraph(contentParagraph4, fontChineseParagraph);
paragraph4.setAlignment(Element.ALIGN_RIGHT);
document.add(paragraph4);
String contentParagraph5 = "文本內(nèi)容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是指定字體編碼的參數(shù),它表示字體將支持Unicode字符集的水平顯示,這對于渲染中文字符是必須的。
BaseFont.NOT_EMBEDDED參數(shù)表示字體不會被嵌入到PDF文檔中,如果你想要確保PDF在不同設備上的兼容性,你可能需要將字體嵌入到PDF中,這時可以將此參數(shù)改為BaseFont.EMBEDDED。
如果你的PDF模板是預先存在的,并且包含可編輯的表單字段,你可以使用PDDocument和PDAcroForm類來填充這些字段,而不是手動繪制表格。這通常是處理復雜模板的更好方法。
Java的iTextPDF庫和Apache PDFBox庫都是用于生成PDF文檔的開源庫,它們有一些區(qū)別,適用于不同的場景。
1.iTextPDF庫:
- iTextPDF是一個功能強大且靈活的PDF庫,支持創(chuàng)建、修改和處理PDF文檔。
- iTextPDF提供了豐富的API和功能,可以創(chuàng)建復雜的PDF文檔,包括表格、圖像、水印、書簽等。
- iTextPDF支持各種字體、顏色和樣式的自定義,可以實現(xiàn)高度定制化的PDF生成。
- iTextPDF有商業(yè)和開源版本可用,商業(yè)版本提供了更多的功能和支持。
適用場景:
- 需要創(chuàng)建復雜的PDF文檔,包括表格、圖像、水印、書簽等。
- 需要高度定制化的PDF生成。
- 需要商業(yè)版本提供的額外功能和支持。
2.Apache PDFBox庫:
- Apache PDFBox是一個功能強大的PDF庫,主要用于處理和操作PDF文檔。
- PDFBox提供了一系列的API,可以讀取、創(chuàng)建、修改和提取PDF文檔的內(nèi)容。
- PDFBox支持文本提取、圖像提取、加密解密、表單處理等功能。
- PDFBox的API相對較底層,需要開發(fā)者對PDF文檔結構和操作有一定的了解。
適用場景:
- 需要處理和操作PDF文檔的內(nèi)容,如文本提取、圖像提取、加密解密、表單處理等。
- 對PDF文檔結構和操作有一定的了解。
總的來說,如果你需要創(chuàng)建復雜的PDF文檔并且需要高度定制化,可以選擇iTextPDF庫。如果你主要需要處理和操作PDF文檔的內(nèi)容,可以選擇Apache PDFBox庫。
以上就是Java利用iTextPDF庫實現(xiàn)制作PDF表格模板并填充數(shù)據(jù)的詳細內(nèi)容,更多關于Java iTextPDF制作PDF表格模板的資料請關注腳本之家其它相關文章!
相關文章
Springboot新建項目Spring Initializr Error問題及解決
這篇文章主要介紹了Springboot新建項目Spring Initializr Error問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Spring Security實現(xiàn)登錄認證實戰(zhàn)教程
這篇文章主要介紹了Spring Security實現(xiàn)登錄認證實戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-06-06
SpringLDAP目錄服務之LdapTemplate與LDAP操作方式
本文將深入探討Spring LDAP的核心概念、LdapTemplate的使用方法以及如何執(zhí)行常見的LDAP操作,幫助開發(fā)者有效地將LDAP集成到Spring應用中,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

