পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Sunday, January 22, 2017

PDFBox - Adding Text

In the previous chapter, we discussed how to add pages to a PDF document. In this chapter, we will discuss how to add text to an existing PDF document.

Adding Text to an Existing PDF Document

You can add contents to a document using the PDFBox library, this provides you a class named PDPageContentStream which contains the required methods to insert text, images, and other types of contents in a page of a PDFDocument.
Following are the steps to create an empty document and add contents to a page in it.

Step 1: Loading an Existing Document

You can load an existing document using the load() method of the PDDocument class. Therefore, instantiate this class and load the required document as shown below.
File file = new File("Path of the document"); 
PDDocument doc = document.load(file);

Step 2: Getting the Required Page

You can get the required page in a document using the getPage() method. Retrieve the object of the required page by passing its index to this method as shown below.
PDPage page = doc.getPage(1);

Step 3: Preparing the Content Stream

You can insert various kinds of data elements using the object of the class PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.
PDPageContentStream contentStream = new PDPageContentStream(doc, page);

Step 4: Beginning the Text

While inserting text in a PDF document, you can specify the start and end points of the text using the beginText() and endText() methods of the PDPageContentStream class as shown below.
contentStream.beginText(); 
……………………….. 
code to add text content 
……………………….. 
contentStream.endText();
Therefore, begin the text using the beginText() method as shown below.
contentStream.beginText();

Step 5: Setting the Position of the Text

Using the newLineAtOffset() method, you can set the position on the content stream in the page.
//Setting the position for the line 
contentStream.newLineAtOffset(25, 700);

Step 6: Setting the Font

You can set the font of the text to the required style using the setFont() method of the PDPageContentStream class as shown below. To this method you need to pass the type and size of the font.
contentStream.setFont( font_type, font_size );

Step 7: Inserting the Text

You can insert the text into the page using the ShowText() method of the PDPageContentStream class as shown below. This method accepts the required text in the form of string.
contentStream.ShowText(text);

Step 8: Ending the Text

After inserting the text, you need to end the text using the endText() method of the PDPageContentStream class as shown below.
contentStream.endText();

Step 9: Closing the PDPageContentStream

Close the PDPageContentStream object using the close() method as shown below.
contentstream.close();

Step 10: Saving the Document

After adding the required content, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
doc.save("Path");

Step 11: Closing the Document

Finally, close the document using the close() method of the PDDocument class as shown below.
doc.close();

Example

This example demonstrates how to add contents to a page in a document. Here, we will create a Java program to load the PDF document named my_doc.pdf, which is saved in the path C:/PdfBox_Examples/, and add some text to it. Save this code in a file with name AddingContent.java.
import java.io.File; 
import java.io.IOException;
  
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.pdmodel.PDPage; 
import org.apache.pdfbox.pdmodel.PDPageContentStream; 
import org.apache.pdfbox.pdmodel.font.PDType1Font;
  
public class AddingContent {
   public static void main (String args[]) throws IOException {

      //Loading an existing document
      File file = new File("C:/PdfBox_Examples/my_doc.pdf");
      PDDocument document = PDDocument.load(file);
       
      //Retrieving the pages of the document 
      PDPage page = document.getPage(1);
      PDPageContentStream contentStream = new PDPageContentStream(document, page);
      
      //Begin the Content stream 
      contentStream.beginText(); 
       
      //Setting the font to the Content stream  
      contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

      //Setting the position for the line 
      contentStream.newLineAtOffset(25, 500);

      String text = "This is the sample document and we are adding content to it.";

      //Adding text in the form of string 
      contentStream.ShowText(text);      

      //Ending the content stream
      contentStream.endText();

      System.out.println("Content added");

      //Closing the content stream
      contentStream.close();

      //Saving the document
      document.save(new File("C:/PdfBox_Examples/new.pdf"));

      //Closing the document
      document.close();
   }
}
Compile and execute the saved Java file from the command prompt using the following commands.
javac AddingContent.java 
java AddingContent 
Upon execution, the above program adds the given text to the document and displays the following message.
Content added
If you verify the PDF Document new.pdf in the specified path, you can observe that the given content is added to the document as shown below.
Adding text

No comments:

Post a Comment