title: PDFBox LayerUtility - Importing layers into existing PDF
body: <p>I am using pdfbox to manipulate PDF content. I have a big PDF file (say 500 pages). I also have a few other single page PDF files containing only a single image which are around 8-15kb per file at the max. What I need to do is to import these single page pdf's like an overlay onto certain pages of the big PDF file. </p>
  
  <p>I have tried the LayerUtility of pdfbox where I've succeeded but it creates a very large sized file as the output. The source pdf is about 1MB before processing and when added with the smaller pdf files, the size goes upto 64MB. And sometimes I need to include two smaller PDF's onto the bigger one. </p>
  
  <p>Is there a better way to do this or am I just doing this wrong? Posting code below trying to add two layers onto a single page:</p>
  
  <pre><code>...
  ...
  ..
  overlayDoc[pCounter] = PDDocument.load("data\\" + overlay + ".pdf");
  outputPage[pCounter] = (PDPage) overlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0);
  
  LayerUtility lu = new LayerUtility( overlayDoc[pCounter] );
  form[pCounter] = lu.importPageAsForm( bigPDFDoc, Integer.parseInt(pageNo)-1);
  lu.appendFormAsLayer( outputPage[pCounter], form[pCounter], aTrans, "OVERLAY_"+pCounter );
  outputDoc.addPage(outputPage[pCounter]);
  
  mOverlayDoc[pCounter] = PDDocument.load("data\\" + overlay2 + ".pdf");                      
  mOutputPage[pCounter] = (PDPage) mOverlayDoc[pCounter].getDocumentCatalog().getAllPages().get(0);
  
  LayerUtility lu2 = new LayerUtility( mOverlayDoc[pCounter] );
  mForm[pCounter] = lu2.importPageAsForm(outputDoc, outputDoc.getNumberOfPages()-1);
  lu.appendFormAsLayer( mOutputPage[pCounter], mForm[pCounter], aTrans, "OVERLAY_2"+pCounter );
  
  outputDoc.removePage(outputPage[pCounter]);
  outputDoc.addPage(mOutputPage[pCounter]);
  ...
  ...
  </code></pre>