How can I get the current page number?
Use case: An apllication written using itext5 needs to be rewritten using itext7. Our app generates PDF files for our customer's monthly bills.
Structure of the pdf:
Create document:
PdfWriter writer = new PdfWriter(new FileOutputStream(filename)); PdfDocument pdfDocument = new PdfDocument(writer); Document document = new Document(pdfDocument, PageSize.A4); document.setMargins(82, 35, 0, 38); buildBody(pdfData, document, writer); pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, new PdfStartPageHandler<>(pdfData, document, this)); pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new PdfEndPageHandler(document)); buildBodyContent(pdfData, document, writer); document.close();
2.Each IEventHandler instance has this format(except the rectangle values):
public void handleEvent(Event event) {
document.add(lineSeparatorHeader);
document.add(paragraphInfo); //
document.add(lineSeparatorHeader);
}
paragraphInfo - is a Paragraph() which consists of one table with two columns
lineSeparatorHeader - is a LineSeparator()
- The buildBody(pdfData, document, writer) must fill the body of my pdf page.
My problem is that I don't know how to handle the situation when the content exceeds the size of the rectangle. I would like it to create a new page, generate a new rectangle with the same dimensions and continue writing there.
A. is the header which needs to be on every page B. is the body whose content is may occupy more than one page. In case it occupies more than one page, the new page must also contain the header and the footer. C. is the footer
CodePudding user response:
Accumulating your ideas and some suggestions from the comments, the following could be done:
Reserve space for a footer and a header by setting margins on a
Document
instance, ensuring that no content added to theDocument
will be placed on them.Add footer and header events as you've already done.
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName)); Document doc = new Document(pdfDoc); int headerHeight = 100; int footerHeight = 200; float[] oldMargins = new float[] {doc.getTopMargin(), doc.getRightMargin(), doc.getBottomMargin(), doc.getLeftMargin()}; doc.setMargins(doc.getTopMargin() headerHeight, doc.getRightMargin(), doc.getBottomMargin() footerHeight, doc.getLeftMargin()); pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new IEventHandler() { @Override public void handleEvent(Event event) { PdfDocumentEvent pdfDocumentEvent = (PdfDocumentEvent) event; PdfPage page = pdfDocumentEvent.getPage(); Rectangle pageRectangle = page.getPageSize(); Rectangle headerArea = new Rectangle(oldMargins[3], pageRectangle.getTop() - oldMargins[0] - headerHeight, pageRectangle.getWidth() - oldMargins[1] - oldMargins[3], headerHeight); new Canvas(page, headerArea).add( new Div() .setHeight(headerArea.getHeight()) .setWidth(headerArea.getWidth()) .setBackgroundColor(ColorConstants.RED)) .close(); } }); pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new IEventHandler(){ @Override public void handleEvent(Event event) { PdfDocumentEvent pdfDocumentEvent = (PdfDocumentEvent) event; PdfPage page = pdfDocumentEvent.getPage(); Rectangle pageRectangle = page.getPageSize(); Rectangle footerArea = new Rectangle(oldMargins[3], pageRectangle.getBottom() oldMargins[2], pageRectangle.getWidth() - oldMargins[1] - oldMargins[3], footerHeight); new Canvas(page, footerArea).add( new Div() .setHeight(footerArea.getHeight()) .setWidth(footerArea.getWidth()) .setBackgroundColor(ColorConstants.YELLOW)) .close(); } }); for (int i = 0; i < 100; i ) { doc.add(new Paragraph("I'm body's paragraph #" i)); } doc.close();
The resultant PDF looks as expected (no content overflows the footers and the headers):