问题
I am looking forward to create a pdf using iText. The pdf will have a header and a footer. Both the header and footer will have a image (company logo) in it.
I am looking forward to an example similar to that. I will be using the iText library for the first time so I am not sure where to start off from.
回答1:
To add headers and footers to a PDF you generate using iText 7.x, you will generally create event listeners for page starts and/or page ends and add the header and footer contents there-in.
For example you can do it like in this sample on the iText site. Here two event listeners are defined:
//Header event handler
protected class Header implements IEventHandler {
String header;
public Header(String header) {
this.header = header;
}
@Override
public void handleEvent(Event event) {
//Retrieve document and
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
Rectangle pageSize = page.getPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(
page.getLastContentStream(), page.getResources(), pdf);
Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
canvas.setFontSize(18f);
//Write text at position
canvas.showTextAligned(header,
pageSize.getWidth() / 2,
pageSize.getTop() - 30, TextAlignment.CENTER);
}
}
This event handler adds a simple constant string as header to the current page. Similarly you can also add an image.
//page X of Y
protected class PageXofY implements IEventHandler {
protected PdfFormXObject placeholder;
protected float side = 20;
protected float x = 300;
protected float y = 25;
protected float space = 4.5f;
protected float descent = 3;
public PageXofY(PdfDocument pdf) {
placeholder =
new PdfFormXObject(new Rectangle(0, 0, side, side));
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
int pageNumber = pdf.getPageNumber(page);
Rectangle pageSize = page.getPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(
page.getLastContentStream(), page.getResources(), pdf);
Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
Paragraph p = new Paragraph()
.add("Page ").add(String.valueOf(pageNumber)).add(" of");
canvas.showTextAligned(p, x, y, TextAlignment.RIGHT);
pdfCanvas.addXObject(placeholder, x + space, y - descent);
pdfCanvas.release();
}
public void writeTotal(PdfDocument pdf) {
Canvas canvas = new Canvas(placeholder, pdf);
canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
0, descent, TextAlignment.LEFT);
}
}
This event handler is a bit more complex, it adds a Page x of y footer to the current page. As the total number of pages y is unknown at the time, a reference to a placeholder XObject is added instead, and as soon as you know all the pages are created, you can call writeTotal
to have this method write the current number of document pages into the placeholder XObject.
You register these event listeners as following:
PdfWriter writer= new PdfWriter(pdfDest);
PdfDocument pdfDocument = new PdfDocument(writer);
//Create event-handlers
String header = "pdfHtml Header and footer example using page-events";
Header headerHandler = new Header(header);
PageXofY footerHandler = new PageXofY(pdfDocument);
//Assign event-handlers
pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE,headerHandler);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,footerHandler);
//Add content
[... here you add the regular page content to the PdfDocument ...]
//Write the total number of pages to the placeholder and close the document
footerHandler.writeTotal(pdfDocument);
pdfDocument.close();
(The actual example adds the regular page content based on HTML using pdfHTML. You may do so, too, or you may generate the content directly using Document
, Paragraph
, Text
, and other layout classes.)
来源:https://stackoverflow.com/questions/56239771/generate-a-pdf-with-images-on-header-and-footer-with-itext