Sunday, January 22, 2017

PDFBox - Adding Multiple Lines

In the example provided in the previous chapter we discussed how to add text to a page in a PDF but through this program, you can only add the text that would fit in a single line. If you try to add more content, all the text that exceeds the line space will not be displayed.

For example, if you execute the above program in the previous chapter by passing the following string only a part of it will be displayed.
String text = "This is an example of adding text to a page in the pdf document. we can
   add as many lines as we want like this using the showText() method of the 
   ContentStream class";
Replace the string text of the example in the previous chapter with the above mentioned string and execute it. Upon execution, you will receive the following output.
Single line extended If you observe the output carefully, you can notice that only a part of the string is displayed.
In order to add multiple lines to a PDF you need to set the leading using the setLeading() method and shift to new line using newline() method after finishing each line.

Steps

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 = PDDocument.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 named 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: Setting the Text Leading

You can set the text leading using the setLeading() method as shown below.
contentStream.setLeading(14.5f);

Step 8: Inserting Multiple Strings Using newline()

You can insert multiple strings using the ShowText() method of the PDPageContentStream class, by dividing each of them using the newline() method as shown below.
contentStream. ShowText(text1); 
contentStream.newLine(); 
contentStream. ShowText(text2);

Step 9: 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 10: Closing the PDPageContentStream

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

Step 11: 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 12: 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 multiple lines in a PDF using PDFBox. Save this program in a file with name AddMultipleLines.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 AddMultipleLines {
   public static void main(String args[]) throws IOException {

      //Loading an existing document
      File file = new File("C:/PdfBox_Examples/my_pdf.pdf");
      PDDocument doc = document.load(file);
       
      //Creating a PDF Document
      PDPage page = doc.getPage(1);  
       
      PDPageContentStream contentStream = new PDPageContentStream(doc, page); 
       
      //Begin the Content stream 
      contentStream.beginText(); 
       
      //Setting the font to the Content stream
      contentStream.setFont( PDType1Font.TIMES_ROMAN, 16 );
       
      //Setting the leading
      contentStream.setLeading(14.5f);

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

      String text1 = "This is an example of adding text to a page in the pdf document.
         we can add as many lines";
      String text2 = "as we want like this using the ShowText()  method of the
         ContentStream class";

      //Adding text in the form of string
      contentStream. ShowText(text1);
      contentStream.newLine();
      contentStream. ShowText(text2);
      //Ending the content stream
      contentStream.endText();

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

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

      //Saving the document
      doc.save(new File("C:/PdfBox_Examples/new.pdf"));
            
      //Closing the document
      doc.close();
   }
}
Compile and execute the saved Java file from the command prompt using the following commands.
javac AddMultipleLines.java 
java AddMultipleLines
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 in multiple lines as shown below.
Adding multiplelines

No comments:

Post a Comment