如果我在一个没有要在itext PDF中显示的行的新页面上,我需要能够告诉抑制列标题。我发现了一个自定义的PDfPageEvent类,它实现了原始的PdfPageEvent接口并实现了onStartPage方法,如下所示:
public class PdfPageEvent implements com.lowagie.text.pdf.PdfPageEvent {
Report report;
public PdfPageEvent(Report report) {
this.report = report;
}
public void onStartPage(PdfWriter writer, Document document) {
ReportTable table = report.getPageHeaderForPDF();
if (table != null) {
PdfPTable pTable = ((PdfReportTable) table).getTable();
pTable.setTotalWidth(document.getPageSize().getRight() - document.getPageSize().getLeft() - 20);
pTable.writeSelectedRows(0, -1, 10, document.getPageSize().getTop() - 10, writer.getDirectContent());
}
}
public void onEndPage(PdfWriter writer, Document document) {
ReportTable table = report.getPageFooter();
if (table != null) {
PdfPTable pTable = ((PdfReportTable) table).getTable();
pTable.setTotalWidth(document.getPageSize().getRight() - document.getPageSize().getLeft() - 20);
pTable.writeSelectedRows(0, -1, 10, document.getPageSize().getBottom() + 20, writer.getDirectContent());
}
}
public void onOpenDocument(PdfWriter writer, Document document) {
}
public void onCloseDocument(PdfWriter writer, Document document) {
}
public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
}
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
}
public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
}
public void onChapterEnd(PdfWriter writer, Document document, float paragraphPosition) {
}
public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {
}
public void onSectionEnd(PdfWriter writer, Document document, float paragraphPosition) {
}
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
}
}
打印列标题的逻辑存在于" report.getPageHeaderForPDF()"中。 我如何尝试检测我是否在新的pdf页面上并且writer.getDirectContent()至少有一行添加到该新页面?
我想我可以在新页面上使用这两个检测:
int pageNumber = writer.getPageNumber();
int currentPageNumber = writer.getCurrentPageNumber();
但是如何根据writer.getDirectContent()的内容判断是否还有任何实际行要在该页面上打印?