我有一个创建标题的标题类。该页眉在每页上输出。如果文档是草稿而不是最终文档,我需要在标题中添加DRAFT REPORT
每次都会显示headerTable
文字。第一次创建文档时,draftTable
文本不会显示,但如果我再次创建文档,则会显示。
有谁知道如何解决这个问题或为什么会发生这种情况?我有以下代码:
public void onEndPage(PdfWriter writer, Document document) {
CustomPdfReport.incrementPageNum();
PdfContentByte cb = writer.getDirectContent();
String pgTitle = CustomPdfPage.getPageTitle();
if (null != pgTitle && !pgTitle.isEmpty()){
Phrase pageTitle = getHeaderPageTitle(pgTitle);
BaseResource baseResource = BaseResource.getInstance();
String AppPath = baseResource != null ? baseResource.getStringValue(BaseResource.APPLICATION_PATH) : BaseResource.getString(BaseResource.APPLICATION_PATH);
String imagePath = AppPath + "shared" + File.separator + "images"+ File.separator + "printTitleHeaderImage.jpg";
try{
logoImage = Image.getInstance(imagePath);
} catch (DocumentException | IOException e) {
LOGGER.error("CustomHeaderFooter.onOpenDocument had an error seting up the image instance "+ e);
e.printStackTrace();
}
float [] imageScale = {50,50};
float width = PdfConversionFromInches.convertInchesToPageUnits(10.0);
float [] headerTableWidth = {80,20};
PdfPTable headerTable = new PdfPTable(2);
try{
headerTable.setWidths(headerTableWidth);
headerTable.setTotalWidth(width);
headerTable.setLockedWidth(true);
headerTable.getDefaultCell().setFixedHeight(topMargin);
PdfPCell headerCell = PdfCell.createTextCell(pageTitle, Element.ALIGN_LEFT, 1, pageColor);
headerCell.setHorizontalAlignment(Element.ALIGN_LEFT);
headerTable.addCell(headerCell);
PdfPCell imageCell = PdfCell.createImageCell(imagePath, Element.ALIGN_TOP, Element.ALIGN_RIGHT, 1, imageScale, pageColor);
headerTable.addCell(imageCell);
headerTable.addCell(PdfCell.createEmptyTableCell(2,pageColor,PdfCell.BIGGER_STANDARD_CELL_HEIGHT));
headerTable.writeSelectedRows(0, -1, leftMargin, PdfConversionFromInches.convertInchesToPageUnits(8.5), cb);
//check for report being a draft report so we can put the DRAFT REPORT indicator on the page's header
if (isDraftReport){
PdfPTable draftTable = new PdfPTable(1);
draftTable.setTotalWidth(PdfConversionFromInches.convertInchesToPageUnits(2.25));
draftTable.setSplitLate(false);
Phrase draftTitle = new Phrase(draftText,customFonts.getDraftFont());
draftTable.addCell(PdfCell.createTextCell(draftTitle, Element.ALIGN_LEFT, 3*PdfCell.STANDARD_CELL_HEIGHT, 1, pageColor));
float height = 8.2f;
float xposition = document.leftMargin() + PdfConversionFromInches.convertInchesToPageUnits(7);
draftTable.writeSelectedRows(0, -1, xposition, PdfConversionFromInches.convertInchesToPageUnits(height), cb);
}
catch(DocumentException de) {
LOGGER.debug("Custom HeaderFooter. An error occurred creating the header. Error = "+de);
throw new ExceptionConverter(de);
}