Search code examples
javapdfitext

How to add an image in the last page of pdf using iText?


How do i add an image on the last page of existing PDF document. Please help me.


Solution

  • You can read the text from the PDF using the same ITEXT library.Try this

        PdfReader reader = new PdfReader(INPUTFILE);
        int n = reader.getNumberOfPages();
        PdfTextExtractor parser =new PdfTextExtractor(new PdfReader("C:/Text.pdf"));
        parser.getTextFromPage(3); // Extracting the content from a particular page.
    

    After you have add your data ,You can load images either from file or from a URL, like this:

       Image image1 = Image.getInstance("watermark.png");
       document.add(image1);
    
       String imageUrl = "http://applause-voice.com/wp-content/uploads/2011/04/1hello.jpg";
       Image image2 = Image.getInstance(new URL(imageUrl));
       document.add(image2);
    

    If you will add this code at the end of your Java Program , then the image will automatically comes at the end of your page.