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

后端 未结 4 1367
花落未央
花落未央 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 01:56

    A generic solution to add image at the top of the page. We can do it by positing image to the top also. it may 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();
            }
    }
    

    }

提交回复
热议问题