Merging pdf documents using the BI-Publisher API

1

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();
}
}

 

 

Share.

About Author

1 Comment

  1. Hi Marcos,
    I am in need of merging two pdf documents each having single pages. The first page is just text and second is protected with password. when i try to merge these two documents, i receive no error but the merged document has second page always blank. The password protected pdf is also merged with no issues. If its contents are displayed at first, the second page of the plan pDf is still blank… can you please help me if there is any issue with pdf or need some config to merge without any issues.
     
    I appreciate your help.
     
    Thanks.