如何使用 onEndPage 方法在 PDF header 中打印动态字符串
How to print dynamic string in PDF header using onEndPage method
我的 pdf 文档中有多个 table 打印 pdf 中的一个 table 我需要 table 的名称打印在 header 部分在打印 table 的所有页面中。此外,header 值需要根据 table 名称进行更改。
我已经尝试为每个 table 的 header 设置新的 table 名称,但是所有页面只打印最后的 table 名称。
示例代码
PdfGenerator.class
public static void main(String[] args) throws FileNotFoundException, DocumentException {
try {
String pdfFilePath = "C:\Users\ychitela\Desktop\demo\NewPdf.pdf";
File file = new File(pdfFilePath);
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document(PageSize.A4.rotate(), 36, 36, 55, 25);
PdfWriter writer = PdfWriter.getInstance(document, fileout);
ReportHeader event = new ReportHeader();
writer.setPageEvent(event);
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
document.addAuthor("Me");
document.addTitle("Table Report");
Font font = FontFactory.getFont("TIMES_ROMAN", 12, BaseColor.BLACK);
document.add(new Paragraph("Intro Page"));
document.newPage();
Chapter chapter = new Chapter(new Paragraph("Table \n\n"), 0);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(" "));
for (int i = 1; i < 5; i++) {
float[] columnWidths = { 1f, 1f };
// create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(30.0f);
Section subsection = chapter.addSection(new Paragraph("Table "+i+" \n\n"), 0);
event.setTableName("Table header" + i);
writer.setPageEvent(event);
table.addCell(new PdfPCell(new Phrase("Column 1", font)));
table.addCell(new PdfPCell(new Phrase("Column 2", font)));
table.setHeaderRows(1);
for (int j = 0; j < 25; j++) {
table.addCell(new PdfPCell(new Phrase("Hello" + j, font)));
table.addCell(new PdfPCell(new Phrase("World" + j, font)));
}
subsection.add(table);
subsection.newPage();
}
document.add(chapter);
document.close();
System.out.println("Done");
} catch (DocumentException e) {
e.printStackTrace();
}
}
Header.class
public class ReportHeader extends PdfPageEventHelper {
private String tableName;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table2 = new PdfPTable(1);
try {
BaseColor basecolour = BaseColor.DARK_GRAY;
Font fontboldHead = FontFactory.getFont("TIMES_ROMAN", 8, basecolour);
table2.setTotalWidth(300);
PdfPCell cell2 = new PdfPCell(new Paragraph(tableName, fontboldHead));
cell2.setBorder(Rectangle.NO_BORDER);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_BOTTOM);
table2.addCell(cell2);
table2.writeSelectedRows(0, -1, document.left(), 580, writer.getDirectContent());
} catch (Exception e) {
e.printStackTrace();
}
}
}
正如您自己发现的,问题的原因是
I was using chapter and subsection in my actual code and looping and at the end of the loop, I'm adding the chapter to document. so that has been leading to only print only last table name.
要使动态 header 正常工作,您必须更早地添加内容;特别是每次添加新的 table 时都必须将章节的当前内容添加到文档中,即每次 header 都会更改。
您可以利用 Chapter
实现 LargeElement
的事实来做到这一点,即您可以多次向您的文档添加一个 Chapter
实例,并且每次只添加所有内容自上次以来章节中的新内容被写入文档。为此,您必须在开始时将其 Complete
属性 设置为 false
,然后仅在最后一次添加之前将其设置回 true
。因此,
Chapter chapter = new Chapter(new Paragraph("Table \n\n"), 0);
// prepare chapter to be set piecewise
chapter.setComplete(false);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(" "));
for (int i = 1; i < 5; i++) {
float[] columnWidths = { 1f, 1f };
// create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(30.0f);
// add recent chapter additions
document.add(chapter);
Section subsection = chapter.addSection(new Paragraph("Table "+i+" \n\n"), 0);
event.setTableName("Table header" + i);
writer.setPageEvent(event);
table.addCell(new PdfPCell(new Phrase("Column 1", font)));
table.addCell(new PdfPCell(new Phrase("Column 2", font)));
table.setHeaderRows(1);
for (int j = 0; j < 25; j++) {
table.addCell(new PdfPCell(new Phrase("Hello" + j, font)));
table.addCell(new PdfPCell(new Phrase("World" + j, font)));
}
subsection.add(table);
subsection.newPage();
}
// prepare chapter to be completed
chapter.setComplete(true);
// final adding of chapter
document.add(chapter);
(ChapterAndDynamicHeader 测试 testLikeYuvarajChitelaImproved
)
我的 pdf 文档中有多个 table 打印 pdf 中的一个 table 我需要 table 的名称打印在 header 部分在打印 table 的所有页面中。此外,header 值需要根据 table 名称进行更改。
我已经尝试为每个 table 的 header 设置新的 table 名称,但是所有页面只打印最后的 table 名称。
示例代码
PdfGenerator.class
public static void main(String[] args) throws FileNotFoundException, DocumentException {
try {
String pdfFilePath = "C:\Users\ychitela\Desktop\demo\NewPdf.pdf";
File file = new File(pdfFilePath);
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document(PageSize.A4.rotate(), 36, 36, 55, 25);
PdfWriter writer = PdfWriter.getInstance(document, fileout);
ReportHeader event = new ReportHeader();
writer.setPageEvent(event);
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
document.open();
document.addAuthor("Me");
document.addTitle("Table Report");
Font font = FontFactory.getFont("TIMES_ROMAN", 12, BaseColor.BLACK);
document.add(new Paragraph("Intro Page"));
document.newPage();
Chapter chapter = new Chapter(new Paragraph("Table \n\n"), 0);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(" "));
for (int i = 1; i < 5; i++) {
float[] columnWidths = { 1f, 1f };
// create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(30.0f);
Section subsection = chapter.addSection(new Paragraph("Table "+i+" \n\n"), 0);
event.setTableName("Table header" + i);
writer.setPageEvent(event);
table.addCell(new PdfPCell(new Phrase("Column 1", font)));
table.addCell(new PdfPCell(new Phrase("Column 2", font)));
table.setHeaderRows(1);
for (int j = 0; j < 25; j++) {
table.addCell(new PdfPCell(new Phrase("Hello" + j, font)));
table.addCell(new PdfPCell(new Phrase("World" + j, font)));
}
subsection.add(table);
subsection.newPage();
}
document.add(chapter);
document.close();
System.out.println("Done");
} catch (DocumentException e) {
e.printStackTrace();
}
}
Header.class
public class ReportHeader extends PdfPageEventHelper {
private String tableName;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table2 = new PdfPTable(1);
try {
BaseColor basecolour = BaseColor.DARK_GRAY;
Font fontboldHead = FontFactory.getFont("TIMES_ROMAN", 8, basecolour);
table2.setTotalWidth(300);
PdfPCell cell2 = new PdfPCell(new Paragraph(tableName, fontboldHead));
cell2.setBorder(Rectangle.NO_BORDER);
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setVerticalAlignment(Element.ALIGN_BOTTOM);
table2.addCell(cell2);
table2.writeSelectedRows(0, -1, document.left(), 580, writer.getDirectContent());
} catch (Exception e) {
e.printStackTrace();
}
}
}
正如您自己发现的,问题的原因是
I was using chapter and subsection in my actual code and looping and at the end of the loop, I'm adding the chapter to document. so that has been leading to only print only last table name.
要使动态 header 正常工作,您必须更早地添加内容;特别是每次添加新的 table 时都必须将章节的当前内容添加到文档中,即每次 header 都会更改。
您可以利用 Chapter
实现 LargeElement
的事实来做到这一点,即您可以多次向您的文档添加一个 Chapter
实例,并且每次只添加所有内容自上次以来章节中的新内容被写入文档。为此,您必须在开始时将其 Complete
属性 设置为 false
,然后仅在最后一次添加之前将其设置回 true
。因此,
Chapter chapter = new Chapter(new Paragraph("Table \n\n"), 0);
// prepare chapter to be set piecewise
chapter.setComplete(false);
chapter.setNumberDepth(0);
chapter.add(new Paragraph(" "));
for (int i = 1; i < 5; i++) {
float[] columnWidths = { 1f, 1f };
// create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(30.0f);
// add recent chapter additions
document.add(chapter);
Section subsection = chapter.addSection(new Paragraph("Table "+i+" \n\n"), 0);
event.setTableName("Table header" + i);
writer.setPageEvent(event);
table.addCell(new PdfPCell(new Phrase("Column 1", font)));
table.addCell(new PdfPCell(new Phrase("Column 2", font)));
table.setHeaderRows(1);
for (int j = 0; j < 25; j++) {
table.addCell(new PdfPCell(new Phrase("Hello" + j, font)));
table.addCell(new PdfPCell(new Phrase("World" + j, font)));
}
subsection.add(table);
subsection.newPage();
}
// prepare chapter to be completed
chapter.setComplete(true);
// final adding of chapter
document.add(chapter);
(ChapterAndDynamicHeader 测试 testLikeYuvarajChitelaImproved
)