final String CLONESRC = "clone-src.pdf";
final String CLONEDST = "clone-dst.pdf";
new File(TESTDIR).mkdirs();
PDDocument srcDoc = new PDDocument();
PDPage pdPage = new PDPage();
srcDoc.addPage(pdPage);
PDPageContentStream pdPageContentStream1 = new PDPageContentStream(srcDoc, pdPage, true, false);
pdPageContentStream1.setNonStrokingColor(Color.black);
pdPageContentStream1.fillRect(100, 600, 300, 100);
pdPageContentStream1.close();
PDPageContentStream pdPageContentStream2 = new PDPageContentStream(srcDoc, pdPage, true, false);
pdPageContentStream2.setNonStrokingColor(Color.red);
pdPageContentStream2.fillRect(100, 500, 300, 100);
pdPageContentStream2.close();
PDPageContentStream pdPageContentStream3 = new PDPageContentStream(srcDoc, pdPage, true, false);
pdPageContentStream3.setNonStrokingColor(Color.yellow);
pdPageContentStream3.fillRect(100, 400, 300, 100);
pdPageContentStream3.close();
srcDoc.save(TESTDIR + CLONESRC);
PDFMergerUtility merger = new PDFMergerUtility();
PDDocument dstDoc = new PDDocument();
// this calls PDFCloneUtility.cloneForNewDocument(),
// which would fail before the fix in PDFBOX-2052
merger.appendDocument(dstDoc, srcDoc);
// save and reload PDF, so that one can see that the files are legit
dstDoc.save(TESTDIR + CLONEDST);
PDDocument.load(TESTDIR + CLONESRC).close();
PDDocument.loadNonSeq(new File(TESTDIR + CLONESRC), null).close();
PDDocument.load(TESTDIR + CLONEDST).close();
PDDocument.loadNonSeq(new File(TESTDIR + CLONEDST), null).close();
}