Quantcast

ADD IMAGE TO EACH PAGE IN PDF

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ADD IMAGE TO EACH PAGE IN PDF

adityashukla74
This post has NOT been accepted by the mailing list yet.
I was able to embed my image on on page.
My Question : How do I add this image to each page of the pdf (multiple pages), by creating an instance id of the image in the PDF!

package org.QrCodeEmbed;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.FileOutputStream;
import java.io.IOException;

public class QrCodeEmbed {
    public static void main(String[] args) throws Exception {
        String workingDir = System.getProperty("user.dir");
        System.out.println(workingDir);
        //ADD IMAGE TO PDF FROMT THE WORKING DIRECTORY
        addImageToPdf(workingDir + "/PDF1.pdf", workingDir + "/document-OUT.pdf", workingDir+"/qrimage.png");
    }

    public static void addImageToPdf(String srcPdf, String destPdf, String imagePath) throws IOException, DocumentException {
        //srcPdf string with the full path to existing PDF File,
    PdfReader reader = new PdfReader(srcPdf);
    // destPdf is the destination PDF where the modified file is created.
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf));
        //Stamp or embed the image on the PDF file
        PdfContentByte content = stamper.getOverContent(1);
        //Load Image Path
        Image image = Image.getInstance(imagePath);
        // scale the image to 50px height
        image.scaleAbsoluteHeight(50);
        image.scaleAbsoluteWidth((image.getWidth() * 50) / image.getHeight());
        image.setAbsolutePosition(30, 740);
        content.addImage(image);
        stamper.close();
    }
}
Loading...