How to Modify - Add Text To Existing PDF in java


In this core java tutorial we will learn How to Add Text To Existing PDF in java using iText library - core java tutorial with program and examples.

We will modify/edit the existing pdf in java.



how to Create Paragraph In Pdf - iText java tutorial program

Create Pie Chart In Pdf in java using itext - iText java tutorial

How to Modify - Add Text To Existing PDF in java - iText java tutorial example



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 text 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.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
/**
*
*  How to Add Text To Existing PDF in java
*  Example Using iText library - core java tutorial
*
*/
public class AddTextToExistingPDF {
   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);
          // loop on all the PDF pages
          // i is the pdfPageNumber
          for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
              //getOverContent() allows you to write content on TOP of existing pdf content.
              //getUnderContent() allows you to write content on BELOW of existing pdf content.
                
                 PdfContentByte pdfContentByte = pdfStamper.getOverContent(i);
                 // Add text in existing PDF
                 pdfContentByte.beginText();
                 pdfContentByte.setFontAndSize(BaseFont.createFont
                                                   (BaseFont.TIMES_BOLD, //Font name
                                                    BaseFont.CP1257, //Font encoding
                                                    BaseFont.EMBEDDED //Font embedded
                                                    )
                              , 12); // set font and size
                 pdfContentByte.setTextMatrix(35, 760); // set x and y co-ordinates
                                            //0, 800 will write text on TOP LEFT of pdf page
                                            //0, 0 will write text on BOTTOM LEFT of pdf page
                 pdfContentByte.showText("Newly added text - pdf modified"); // add the text
                 System.out.println("Text added in "+outputFilePath);
                
                 pdfContentByte.endText();
          }
          pdfStamper.close(); //close pdfStamper
          System.out.println("Modified PDF created in >> "+ outputFilePath);
   }
}
/*OUTPUT
Text 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 itext tutorial we learned How to Add Text To Existing PDF in java i.e how to modify/edit the existing pdf in java.




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>

How to Create Images In Pdf generated Table in java itext


How to Create Nested Tables in Pdf in java


Create Greek List In pdf in java using iText


How to Add And Rotate Images In Pdf in java

Creating the UnOrdered List In pdf in java


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


How to Create Roman List In pdf in java



eEdit
Must read for you :