How To Read And Add Image To Existing Pdf in java Example


In this core java tutorial we will learn How To Read And Add Image To Existing Pdf i.e. to modify existing pdf to add image in it in java  - using iText library - core java tutorial with program and examples.

How can you Add the Images from URL In Pdf in java


STEP 1 >
Create Existing.pdf using the this program. PDF before modification/editing i.e. before executing below java program will look like this >


STEP 2 >
Now, we will modify/edit/add image to Existing.pdf


Download jars required to execute program >

Or, you may download jars from here.

Program/Example to  - Using iText library - core java tutorial

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
/**
*
* How To Read And Add Image To Existing Pdf Example
* modify the pdf, Using iText library in core java
*
*/
public class HowToReadAndAddImageToExistingPdfExample {
   public static void main(String[] args) throws Exception {
                 String inputFilePath = "E:/Existing.pdf"; // Existing file
                 String outputFilePath = "E:/ModifiedPdf.pdf"; // New file
                 OutputStream fos = new FileOutputStream(new File(outputFilePath));
                 PdfReader pdfReader = new PdfReader(inputFilePath);
                 PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);
                 //Image to be added in existing pdf file.
                 Image image = Image.getInstance("E:/tiger.jpg");
                 image.scaleAbsolute(150, 80); //Scale image's width and height
                 image.setAbsolutePosition(200, 700); //Set position for image in PDF
                 // loop on all the PDF pages
                 // i is the pdfPageNumber
                 for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
                       //getOverContent() allows you to write pdfContentByte on TOP of existing pdf pdfContentByte.
                       //getUnderContent() allows you to write pdfContentByte on BELOW of existing pdf pdfContentByte.
                       PdfContentByte pdfContentByte = pdfStamper.getOverContent(i);
                       pdfContentByte.addImage(image);
                       System.out.println("Image added in "+outputFilePath);
                 }
                 pdfStamper.close();
                 System.out.println("Modified PDF created in >> "+ outputFilePath);
   }
}
/*OUTPUT
Image added in E:/ModifiedPdf.pdf
Modified PDF created in >> E:/ModifiedPdf.pdf
*/



PDF formed after executing above java program will look like this >


Summary -

So in this core java tutorial we learned How To Read And Add Image To Existing Pdf in java  - using iText library - core java tutorial with program and examples.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

Adding the Images In Pdf in java example using iText

How to Add And Rotate Images In Pdf in java

How can you Add the Images from URL In Pdf in java



eEdit
Must read for you :