[iText] Inserting Image onCloseDocument
- by David
I'm trying to insert an image in the footer of my document using iText's onCloseDocument event. I have the following code:
public void onCloseDocument(PdfWriter writer, Document document) {
PdfContentByte pdfByte = writer.getDirectContent();
try {
// logo is a non-null global variable
Image theImage = new Jpeg(logo);
pdfByte.addImage(theImage, 400.0f, 0.0f, 0.0f, 400.0f, 0.0f, 0.0f);
} catch (Exception e) { e.printStackTrace(); }
}
The code throws no exceptions, but it also fails to insert the image. This identical code is used onOpenDocument to insert the same logo. The only difference between the two methods are the coordinates in pdfByte.addImage. However, I've tried quite a few different coordinations in onCloseDocument and none of them appear anywhere in my document.
Is there any troubleshooting technique for detecting content which is displayed off-page in a PDF? If not, can anyone see the problem with my onCloseDocument method?
Edit: As a followup, it seems that onDocumentClose puts its content on page document.length() + 1 (according to its API). However, I don't know how to change the page number back to document.length() and place my logo on the last page.