0

I am merging PDF content using PDFMergerUtility in Java.

PDFMergerUtility bundle= new PDFMergerUtility();
bundle.addSource(new ByteArrayInputStream(contentService.retrieveData(transaction1);
bundle.addSource(new ByteArrayInputStream(contentService.retrieveData(transaction2);
   
ByteArrayOutputStream outputStream = new ByteArrayOutputStream() 
bundle.setDestinationStream(outputStream);  // setting outputstream
bundle.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
return outputStream.toByteArray();  // returning result

But by default it is opening on the last page of PDF when I try to open the resultant PDF. I am not using PDocument for these as these are entity based documents. Due to this unable to use this below method. Can you please suggest any approach to directly set the opened page number in PDFMergerUtility.

PDPageXYZDestination destination = new PDPageXYZDestination();
destination .setPageNumber(num);
PDActionGoTo action = new PDActionGoTo();
action.setDestination(destination );
pdf.getDocumentCatalog().setOpenAction(action);
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • 1
    Use setPage(PDPage), not setPageNumber, see https://stackoverflow.com/questions/35619737/create-pdf-file-with-default-zoom-to-page-level-pdfbox – Tilman Hausherr Jan 24 '22 at 08:58
  • @TilmanHausherr I am using pdfMergeUtility.addSource(inputStreamSource); So not able to use PDocument to setPage method. I am using addSource with imputstream. Can you please suggest in this case. – Sarthak Garg Jan 24 '22 at 10:07
  • 2
    You will have to use PDDocument. You'd have to save and reopen the PDF, modify then resave. – Tilman Hausherr Jan 24 '22 at 10:18

0 Answers0