如何使用 Itext 创建 table 并将其添加到预定义的 pdf 模板?
How to create and add table to predefined pdf template using Itext?
我有一个预定义的模板,我想创建 table 并将其添加到模板的第 3 页。有可能这样做吗?有很多使用 itext 创建新 pdf 的选项,但我还没有看到任何修改现有 pdf 并向其添加 table 的示例。代码示例将不胜感激。
PdfReader reader = new PdfReader("BCC Statements-Template.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
try {
PdfStamper stamper= new PdfStamper(reader, fileOutputStream);
PdfContentByte overContentByte = stamper.getOverContent(3);
PdfPTable pdfPTable = new PdfPTable(4);
pdfPTable.setTotalWidth(40);
//Create cells
PdfPCell pdfPCell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell pdfPCell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell pdfPCell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell pdfPCell4 = new PdfPCell(new Paragraph("Cell 4"));
//Add cells to table
pdfPTable.addCell(pdfPCell1);
pdfPTable.addCell(pdfPCell2);
pdfPTable.addCell(pdfPCell3);
pdfPTable.addCell(pdfPCell4);
pdfPTable.writeSelectedRows(1, 1, 110, 150, overContentByte);
stamper.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
}`
为此您使用 PdfStamper
。代码如下:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte canvas = stamper.getOverContent(1);
PdfPTable table = ...;
//add data to table
table.writeSelectedRows(... , canvas);
stamper.close();
reader.close();
我有一个预定义的模板,我想创建 table 并将其添加到模板的第 3 页。有可能这样做吗?有很多使用 itext 创建新 pdf 的选项,但我还没有看到任何修改现有 pdf 并向其添加 table 的示例。代码示例将不胜感激。
PdfReader reader = new PdfReader("BCC Statements-Template.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("test.pdf");
try {
PdfStamper stamper= new PdfStamper(reader, fileOutputStream);
PdfContentByte overContentByte = stamper.getOverContent(3);
PdfPTable pdfPTable = new PdfPTable(4);
pdfPTable.setTotalWidth(40);
//Create cells
PdfPCell pdfPCell1 = new PdfPCell(new Paragraph("Cell 1"));
PdfPCell pdfPCell2 = new PdfPCell(new Paragraph("Cell 2"));
PdfPCell pdfPCell3 = new PdfPCell(new Paragraph("Cell 3"));
PdfPCell pdfPCell4 = new PdfPCell(new Paragraph("Cell 4"));
//Add cells to table
pdfPTable.addCell(pdfPCell1);
pdfPTable.addCell(pdfPCell2);
pdfPTable.addCell(pdfPCell3);
pdfPTable.addCell(pdfPCell4);
pdfPTable.writeSelectedRows(1, 1, 110, 150, overContentByte);
stamper.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
}`
为此您使用 PdfStamper
。代码如下:
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte canvas = stamper.getOverContent(1);
PdfPTable table = ...;
//add data to table
table.writeSelectedRows(... , canvas);
stamper.close();
reader.close();