how to add an image to my header in iText generated PDF?

后端 未结 4 1400
花落未央
花落未央 2021-01-13 01:39

I\'m using iText to generate a PDF. I created a custom PdfPageEventHelper to add a header (and footer) to each page.

My problem is I don\'t know how to add the imag

4条回答
  •  情歌与酒
    2021-01-13 02:15

    I've set abolute positions and alignment to the image (in this case I placed 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've also adjusted the the document margins in order to have delimited space in the header and footer of the document.

    document.setMargins(20, 20, 100, 100);
    

提交回复
热议问题