Monday 7 March 2016

Apache PDFBox: Write some data to PDF document

In previous post, I explained how to create empty PDF document. In this post I am going to explain how to write message "Hello World" to a PDF document.

Step 1: Get new PDDocument instance.
PDDocument document = new PDDocument()

Step 2: Create new PDPage.
PDPage page = new PDPage();

Step 3: Add pdf page to document. We must add at least one page for the document to be valid.
document.addPage(page);

Step 4: Create a new font object
PDFont font = PDType1Font.HELVETICA_BOLD;

Step 5: Create a new PDPage content stream.
PDPageContentStream contentStream = new PDPageContentStream(document, page)

Step 6: Following lines are used to write message to pdf file.

contentStream.beginText();
contentStream.setFont(font, 16);
contentStream.newLineAtOffset(200, 700);
contentStream.showText(message);
contentStream.endText();

Step 7: Once everything is done, save the document.

document.save(fileName);
import java.io.IOException;
import java.util.Objects;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class HelloWorldPDF {

 /**
  * Write given message to file.
  * 
  * @param fileName
  * @param message
  * @return true on success, else false
  */
 public static boolean writeMessageToPDF(final String fileName,
   final String message) {
  if (Objects.isNull(fileName)) {
   throw new NullPointerException("fileName shoudn't be null");
  }

  if (Objects.isNull(message)) {
   throw new NullPointerException("message shoudn't be null");
  }

  try (final PDDocument document = new PDDocument()) {
   /*
    * Create a new blank page and add it to the document, we must add
    * at least one page for the document to be valid.
    */
   final PDPage page = new PDPage();
   document.addPage(page);

   /* Create a new font object */
   final PDFont font = PDType1Font.HELVETICA_BOLD;

   /*
    * Start a new content stream which will "hold" the to be created
    * content
    */
   try (final PDPageContentStream contentStream = new PDPageContentStream(
     document, page)) {
    /*
     * Define a text content stream using the selected font, moving
     * the cursor and drawing the text message
     */
    contentStream.beginText();
    contentStream.setFont(font, 16);
    contentStream.newLineAtOffset(200, 700);
    contentStream.showText(message);
    contentStream.endText();
   }

   /* Save the document to a file. */
   document.save(fileName);

   return true;
  } catch (IOException e) {
   System.out.println(e.getMessage());
   return false;
  }
 }

 public static void main(String args[]) {
  final String fileName = "/Users/harikrishna_gurram/tutorials/blank.pdf";
  final String message = "Happy Birthday PTR";

  if (writeMessageToPDF(fileName, message)) {
   System.out.println(message + " is written to file : " + fileName);
  } else {
   System.out.println("Message writing failed");
  }
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Write message to Given file");
  return builder.toString();
 }

}

Output
Happy Birthday PTR is written to file : /Users/harikrishna_gurram/tutorials/blank.pdf







Previous                                                 Next                                                 Home

No comments:

Post a Comment