How to add image to my title in iText generated PDF?

I am using iText to create a PDF file. I created a custom PdfPageEventHelper to add a header (and footer) to each page.

My problem: I do not know how to add an image so that it appears in the "title bar". I only know how to add an image to the document itself (if that makes sense).

Here are some code snippets ...

public static void main(String[] args) { Rectangle headerBox = new Rectangle(36, 54, 559, 788); /* ... */ Document document = new Document(PageSize.A4, 36, 36, 154, 54); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILENAME)); HeaderFooter event = new HeaderFooter(); writer.setBoxSize("headerBox", headerBox); writer.setPageEvent(event); document.open(); addContent(); document.close(); } static class HeaderFooter extends PdfPageEventHelper { public void onEndPage(PdfWriter writer, Document document) { Rectangle rect = writer.getBoxSize("headerBox"); // add header text ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("Hello", fontHeader1), rect.getLeft(), rect.getTop(), 0); // add header image try { Image img = Image.getInstance("c:/mylogo.PNG"); img.scaleToFit(100,100); document.add(img); } catch (Exception x) { x.printStackTrace(); } } } 

Any suggestions on an appropriate way to add an image to the title are welcome.

Rob

+4
source share
4 answers

You make two main mistakes.

  • You create a new instance of the object for each new page. This will result in a bloated PDF file, since image bytes will be added as many times as there are pages. Create an Image object outside the onEndPage() method and reuse it. Thus, image bytes will be added to the PDF only once.
  • As indicated, the Document passed to the onEndPage() method as a parameter should be considered as a read-only parameter. It is forbidden to add content to it. This is a different object than the one you created using the new Document(PageSize.A4, 36, 36, 154, 54) . This is actually an instance of the PdfDocument class created internally by the PdfWriter instance. To add an image, you need to get PdfContentByte from the author and add the image using addImage() .

Errors like this can easily be avoided by reading the documentation. You can save a lot of time by reading my iText book in action .

+11
source

Can you try

 img.setAbsolutePosition(10, 10); writer.getDirectContent().addImage(img); 

instead

 document.add(img); 

inside onPageEnd ?

+5
source

I set the absolute position and alignment to the image (in this case I put my image in the header)

  try { Image img = Image.getInstance("url/logo.png"); img.scaleToFit(100,100); img.setAbsolutePosition((rect.getLeft() + rect.getRight()) / 2 - 45, rect.getTop() - 50); img.setAlignment(Element.ALIGN_CENTER); writer.getDirectContent().addImage(img); } catch (Exception x) { x.printStackTrace(); } 

I also adjusted the margins of the document to have limited space in the header and footer of the document.

 document.setMargins(20, 20, 100, 100); 
+1
source

The general solution is to add an image at the top of the page. We can do this by putting the image on top. he can fix your requirement

 public static void main(String[] args) throws MalformedURLException, IOException, DocumentException { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")); document.open(); // // Scale the image to a certain percentage // String filename = "image.jpg"; Image image = Image.getInstance(filename); image = Image.getInstance(filename); image.scalePercent(200f); image.setAbsolutePosition(0, (float) (PageSize.A4.getHeight() - 20.0)); System.out.println(image.getScaledHeight()); document.add(image); // // Scales the image so that it fits a certain width and // height // image.scaleToFit(100f, 200f); document.add(image); document.add(new Chunk("This is chunk 3. ")); System.out.println("created"); } catch (DocumentException | IOException e) { e.printStackTrace(); } finally { document.close(); } } 

}

+1
source

Source: https://habr.com/ru/post/1440384/


All Articles