Merging pdf documents using the BI-Publisher API 13422386 1019544571447648 7687716130941590224 o1

Merging pdf documents using the BI-Publisher API

BI Publisher (named XML-Publisher until a few weeks ago, but what is in the name) offers besides the possibility to create documents also the possibility to interact with the application through its application programming interface. These apis offer the programmer several functions of which the PDF document merger is one.
This morning I looked into the functionality this api could offer me in one of my projects for which I need to create a singel pdf document out of multiple documents. How does it work and what do you need to do

  • Since we want to use the BI-Publisher api we will need to get this one first. Download XML-Publisher and unzip it.
  • Start JDeveloper and create a new application and a new project
  • Add a java class to the project. This class will implement the code to merge pdf documents to one single document
  • Add the XML-Publisher API’s jar files to the project. Note that the jar files are located in the  <XMLP562_win>manual\lib directory where <XMLP562_win> is the directory where you unzipped the XML-Publiser zipfile.

Ok so now for the code:
Create input streams and a output stream

      FileInputStream[] input = new FileInputStream[2];
      input[0] = new FileInputStream("PDFDoc1.pdf");
      input[1] = new FileInputStream("PDFDoc2.pdf");
      FileOutputStream output = new FileOutputStream("MergedPDF.pdf");

Create a new PDFDOcMerger object with as input the input and output streams

      PDFDocMerger pdfMerger = new PDFDocMerger(input, output);

optionally set the page number and a watermark

      pdfMerger.setPageNumberCoordinates(300, 20);
      pdfMerger.setPageNumberFontInfo("Arial", 10);
      pdfMerger.setPageNumberValue(1, 1);
      pdfMerger.setTextDefaultWatermark("DRAFT");

Merge the pdf documents with the PDFMerger class

      pdfMerger.process();

Execute the Java class and you will have your documents merged.
The full class below

package nl.amis.xmlpublisher;

   import oracle.apps.xdo.XDOException;
   import oracle.apps.xdo.common.pdf.util.PDFDocMerger;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;

public class XMLPublisher {
public XMLPublisher() {
}

public void pdfDocumentMerger() {
try {
FileInputStream[] input = new FileInputStream[2];
input[0] = new FileInputStream(“PDFDoc1.pdf”);
input[1] = new FileInputStream(“PDFDoc2.pdf”);

FileOutputStream output = new FileOutputStream(“MergedPDF.pdf”);
PDFDocMerger pdfMerger = new PDFDocMerger(input, output);

pdfMerger.setPageNumberCoordinates(300, 20);
pdfMerger.setPageNumberFontInfo(“Arial”, 10);
pdfMerger.setPageNumberValue(1, 1);
pdfMerger.setTextDefaultWatermark(“DRAFT”);
pdfMerger.process();
pdfMerger = null;
} catch (XDOException e) {
System.out.println(“XDOException” + e.getMessage());
} catch (FileNotFoundException e) {
System.out.println(“FileNotFoundException ” + e.getMessage());
}
}

public static void main(String[] argv) {
MLPublisher xmlPublisher = new XMLPublisher();
xmlPublisher.pdfDocumentMerger();
}
}

 

 

One Response

  1. Subramanian February 20, 2012