gpt4 book ai didi

java - 如何使用 onEndPage 方法打印 PDF 页眉中的动态字符串

转载 作者:行者123 更新时间:2023-12-02 10:02:37 26 4
gpt4 key购买 nike

我的 pdf 文档中有多个表格,在打印 pdf 中的其中一个表格时,我需要在打印表格的所有页面的页眉部分中打印表格的名称。另外,表 header value 需要根据表名进行更改。

我尝试为每个表格的标题设置新的表格名称,但是所有页面只打印最后一个表格名称。

示例代码

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();
}
}

标题类

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.

要使动态标题正常工作,您必须更早添加内容;特别是,每次添加新表格时,即每次标题发生更改时,都必须将章节的当前内容添加到文档中。

您可以利用 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)

关于java - 如何使用 onEndPage 方法打印 PDF 页眉中的动态字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55524484/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com