0

I have code to merge between 2 pdf files using the library OpenPDF like this

public byte[] mergePDF(byte[] pdf1, byte[] pdf2) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter writer;
    try {
      writer = PdfWriter.getInstance(document, os);
    } catch (DocumentException e) {
      throw new IllegalStateException(e);
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    PdfReader pdfReader1 = new PdfReader(pdf1);
    PdfReader pdfReader2 = new PdfReader(pdf2);

    for (int i = 0; i < pdfReader1.getNumberOfPages(); i++) {
      PdfImportedPage page = writer.getImportedPage(pdfReader1, i + 1);
      document.setPageSize(pdfReader1.getPageSize(i + 1));
      document.newPage();
      cb.addTemplate(page, 0, 0);
    }

    for (int i = 0; i < pdfReader2.getNumberOfPages(); i++) {
      PdfImportedPage page = writer.getImportedPage(pdfReader2, i + 1);
      document.setPageSize(pdfReader2.getPageSize(i + 1));
      document.newPage();
      cb.addTemplate(page, 0, 0);
    }

    document.close();
    return os.toByteArray();
}

Merged PDF generated showing a blank page from on Page come from the second pdf but it showing when open with adobe acrobat, firefox, and ubuntu document viewer. Does anyone know what issue? or any missing configuration that needs to be set in my code?

Fairuz Yassar
  • 129
  • 3
  • 11
  • Is `document` the new PDF? The only thing your code is doing with `document` are `open()`, `setPageSize()`, `newPage()`, and `close()`. Where are you putting actual content into `document`? – Kaan Jun 23 '22 at 04:29
  • @Kaan basically its merges between 2 pdf files (has content) – Fairuz Yassar Jun 23 '22 at 04:37
  • Which line in your code does the merging? I see making a new document, setting a page size to a number, calling new page, and then closing the document. Where does it copy anything from the original input PDF documents? – Kaan Jun 23 '22 at 04:49
  • Your code only copies the static page content from the source files but not any annotations. Could it be that your second pdf only contains annotations? Also it doesn't copy any Javascript. Could it be that the content of the second pdf is only revealed via Javascript? Also you create the new pages with the page size rectangle of the original pdf but always position the imported pages at 0,0. Could it be that the second pdf has uncommon page sizes where the origin is outside the visible area? Etc etc etc – mkl Jun 23 '22 at 05:31
  • For a more faithful merge with iText 2.x, iText 5.x, or OpenPDF, you should use a `PdfCopy` based solution instead of a `PdfWriter` based one. See [this answer](https://stackoverflow.com/a/15945467/1729265) for backgrounds - the links unfortunately meanwhile are dead but the description still applies. – mkl Jun 23 '22 at 05:38
  • @mkl Sorry I am a newbie in file processing. idk what mean annotations and revealed via javascript is? The second pdf has different sizes on each page. I just tried to reprint using firefox and ubuntu document viewer the second pdf has a similar page size and a blank page is showing the content in all pdf readers, idk the process behind it. btw thank you for your related link – Fairuz Yassar Jun 23 '22 at 13:58
  • *"idk what mean ..."* - Essentially there may be numerous reasons why the pages from your second document do appear empty after copying. Simply try using `PdfCopy` as hinted at in the referenced answer. – mkl Jun 23 '22 at 14:13

0 Answers0