Question

How to merge the multiple PDF files into the Single PDF in Pega?

Hi,

I need to merge the multiple PDF files into the Single PDF.

As there is no OOTB function for this, the custom functions have to be created.

Please share if there is any logic for this.

Thanks

Correct Answer
March 27, 2019 - 9:46am

Please refer below code,

String listPageName;

 

ClipboardPage listPage;

 

ClipboardProperty linkList;

 

int i;

 

 

 

try

  {

  

org.apache.pdfbox.pdmodel.PDDocument pd1=null;

org.apache.pdfbox.pdmodel.PDDocument pd2=null;

  

org.apache.pdfbox.multipdf.PDFMergerUtility pdfMergeUtil;

  

listPageName=tools.getParamValue("ListPageName");

 

listPage=tools.findPage(listPageName, true);

 

linkList=listPage.getIfPresent(tools.getParamValue("EmbeddedListProperty"));

  

  Database db=tools.getDatabase();

 

  ClipboardPage pdfPG1;

  

  

  String stream;

  String Doc1;

  String Doc2;

  String handle=null;

  if(linkList.size()>=1)

    {

    

     

if((linkList.getPageValue(1).getString("pxObjClass")).equals("Link-Attachment"))

      {

    handle=linkList.getPageValue(1).getString("pxLinkedRefTo");

      }

if((linkList.getPageValue(1).getString("pxObjClass")).equals("Data-WorkAttach-File"))

      {

    handle=linkList.getPageValue(1).getString("pzInsKey");

      }

  pdfPG1=db.open(handle, false);

    stream=pdfPG1.getString("pyAttachStream");

    

    pd1=org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(stream));

    

  }

  

  

  

  if(linkList.size()>1)

    {

   

 

for(i=1; i<linkList.size(); i++ )

  {

  if((linkList.getPageValue(i+1).getString("pxObjClass")).equals("Link-Attachment"))

          {

handle=linkList.getPageValue(i+1).getString("pxLinkedRefTo");

          }

if((linkList.getPageValue(i+1).getString("pxObjClass")).equals("Data-WorkAttach-File"))

          {

handle=linkList.getPageValue(i+1).getString("pzInsKey");

          }

  pdfPG1=db.open(handle, false);

  Doc2=pdfPG1.getString("pyAttachStream");

pd2=org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(Doc2));

  pdfMergeUtil=new org.apache.pdfbox.multipdf.PDFMergerUtility();

  pdfMergeUtil.appendDocument(pd1,pd2);

  

}

 

}

 

  

  

  //org.apache.pdfbox.multipdf.PDFMergerUtility pdfMergeUtil=new org.apache.pdfbox.multipdf.PDFMergerUtility();

  

 

  

 // pdfMergeUtil.appendDocument(pd1,pd2);

  

java.io.ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();

  //pdfMergeUtil.setDestinationStream(baos);

  

  pd1.save(baos);

   

  byte[] bytes= new byte[15000000];

  bytes=baos.toByteArray();

  FileData=Base64Util.encodeToString(bytes);

 

  

  

  }

catch(Exception e)

  {

 

  

}

Comments

Keep up to date on this post and subscribe to comments

March 26, 2019 - 11:25pm

I have the sample Java code which is being used along with apache PDFBox library.

The temp files to created in place of taking the actual files in the following code.

import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
  
import java.io.File; 
import java.io.IOException;

public class MergePDFs {
   public static void main(String[] args) throws IOException {

      //Loading an existing PDF document
      File file1 = new File("C:/PdfBox_Examples/sample1.pdf");
      PDDocument doc1 = PDDocument.load(file1);
       
      File file2 = new File("C:/PdfBox_Examples/sample2.pdf");
      PDDocument doc2 = PDDocument.load(file2);
         
      //Instantiating PDFMergerUtility class
      PDFMergerUtility PDFmerger = new PDFMergerUtility();

      //Setting the destination file
      PDFmerger.setDestinationFileName("C:/PdfBox_Examples/merged.pdf");

      //adding the source files
      PDFmerger.addSource(file1);
      PDFmerger.addSource(file2);

      //Merging the two documents
      PDFmerger.mergeDocuments();

      System.out.println("Documents merged");
      //Closing the documents
      doc1.close();
      doc2.close();
   }

}

March 27, 2019 - 9:46am

Please refer below code,

String listPageName;

 

ClipboardPage listPage;

 

ClipboardProperty linkList;

 

int i;

 

 

 

try

  {

  

org.apache.pdfbox.pdmodel.PDDocument pd1=null;

org.apache.pdfbox.pdmodel.PDDocument pd2=null;

  

org.apache.pdfbox.multipdf.PDFMergerUtility pdfMergeUtil;

  

listPageName=tools.getParamValue("ListPageName");

 

listPage=tools.findPage(listPageName, true);

 

linkList=listPage.getIfPresent(tools.getParamValue("EmbeddedListProperty"));

  

  Database db=tools.getDatabase();

 

  ClipboardPage pdfPG1;

  

  

  String stream;

  String Doc1;

  String Doc2;

  String handle=null;

  if(linkList.size()>=1)

    {

    

     

if((linkList.getPageValue(1).getString("pxObjClass")).equals("Link-Attachment"))

      {

    handle=linkList.getPageValue(1).getString("pxLinkedRefTo");

      }

if((linkList.getPageValue(1).getString("pxObjClass")).equals("Data-WorkAttach-File"))

      {

    handle=linkList.getPageValue(1).getString("pzInsKey");

      }

  pdfPG1=db.open(handle, false);

    stream=pdfPG1.getString("pyAttachStream");

    

    pd1=org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(stream));

    

  }

  

  

  

  if(linkList.size()>1)

    {

   

 

for(i=1; i<linkList.size(); i++ )

  {

  if((linkList.getPageValue(i+1).getString("pxObjClass")).equals("Link-Attachment"))

          {

handle=linkList.getPageValue(i+1).getString("pxLinkedRefTo");

          }

if((linkList.getPageValue(i+1).getString("pxObjClass")).equals("Data-WorkAttach-File"))

          {

handle=linkList.getPageValue(i+1).getString("pzInsKey");

          }

  pdfPG1=db.open(handle, false);

  Doc2=pdfPG1.getString("pyAttachStream");

pd2=org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(Doc2));

  pdfMergeUtil=new org.apache.pdfbox.multipdf.PDFMergerUtility();

  pdfMergeUtil.appendDocument(pd1,pd2);

  

}

 

}

 

  

  

  //org.apache.pdfbox.multipdf.PDFMergerUtility pdfMergeUtil=new org.apache.pdfbox.multipdf.PDFMergerUtility();

  

 

  

 // pdfMergeUtil.appendDocument(pd1,pd2);

  

java.io.ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();

  //pdfMergeUtil.setDestinationStream(baos);

  

  pd1.save(baos);

   

  byte[] bytes= new byte[15000000];

  bytes=baos.toByteArray();

  FileData=Base64Util.encodeToString(bytes);

 

  

  

  }

catch(Exception e)

  {

 

  

}

April 12, 2019 - 10:50pm
Response to mathanagururajs

Thank you @mathanagururajs