use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testMergeBogusStructParents2.
/**
* PDFBOX-4429: merge into destination that has /StructParent(s) entries in the source file but
* no structure tree.
*
* @throws IOException
*/
@Test
public void testMergeBogusStructParents2() throws IOException {
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4408.pdf", "https://issues.apache.org/jira/secure/attachment/12952086/form.pdf");
assumeTrue(inputPdf.exists());
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument src = PDDocument.load(inputPdf);
PDDocument dst = PDDocument.load(inputPdf);
src.getDocumentCatalog().setStructureTreeRoot(null);
src.getPage(0).setStructParents(9999);
src.getPage(0).getAnnotations().get(0).setStructParent(9998);
pdfMergerUtility.appendDocument(dst, src);
checkWithNumberTree(dst);
checkForPageOrphans(dst);
src.close();
dst.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testStructureTreeMergeIDTree.
/**
* PDFBOX-4416: Test merging of /IDTree
* <br>
* PDFBOX-4009: test merging to empty destination
*
* @throws IOException
*/
@Test
public void testStructureTreeMergeIDTree() throws IOException {
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument src = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + "PDFBOX-4417-001031.pdf"));
PDDocument dst = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + "PDFBOX-4417-054080.pdf"));
PDNameTreeNode<PDStructureElement> srcIDTree = src.getDocumentCatalog().getStructureTreeRoot().getIDTree();
Map<String, PDStructureElement> srcIDTreeMap = PDFMergerUtility.getIDTreeAsMap(srcIDTree);
PDNameTreeNode<PDStructureElement> dstIDTree = dst.getDocumentCatalog().getStructureTreeRoot().getIDTree();
Map<String, PDStructureElement> dstIDTreeMap = PDFMergerUtility.getIDTreeAsMap(dstIDTree);
int expectedTotal = srcIDTreeMap.size() + dstIDTreeMap.size();
assertEquals(192, expectedTotal);
// PDFBOX-4009, test that empty dest doc still merges structure tree
// (empty dest doc is used in command line app)
PDDocument emptyDest = new PDDocument();
pdfMergerUtility.appendDocument(emptyDest, src);
src.close();
src = emptyDest;
assertEquals(4, src.getDocumentCatalog().getStructureTreeRoot().getParentTreeNextKey());
pdfMergerUtility.appendDocument(dst, src);
src.close();
dst.save(new File(TARGETTESTDIR, "PDFBOX-4416-IDTree-merged.pdf"));
dst.close();
dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-4416-IDTree-merged.pdf"));
checkWithNumberTree(dst);
checkForPageOrphans(dst);
dstIDTree = dst.getDocumentCatalog().getStructureTreeRoot().getIDTree();
dstIDTreeMap = PDFMergerUtility.getIDTreeAsMap(dstIDTree);
assertEquals(expectedTotal, dstIDTreeMap.size());
dst.close();
checkStructTreeRootCount(new File(TARGETTESTDIR, "PDFBOX-4416-IDTree-merged.pdf"));
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testStructureTreeMerge2.
/**
* PDFBOX-3999: check that no streams are kept from the source document by the destination
* document, despite orphan annotations remaining in the structure tree.
*
* @throws IOException
*/
@Test
public void testStructureTreeMerge2() throws IOException {
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-3999-GeneralForbearance.pdf", "https://issues.apache.org/jira/secure/attachment/12896905/GeneralForbearance.pdf");
assumeTrue(inputPdf.exists());
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument doc = PDDocument.load(inputPdf);
doc.getDocumentCatalog().getAcroForm().flatten();
doc.save(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened.pdf"));
ElementCounter elementCounter = new ElementCounter();
elementCounter.walk(doc.getDocumentCatalog().getStructureTreeRoot().getK());
int singleCnt = elementCounter.cnt;
int singleSetSize = elementCounter.set.size();
assertEquals(134, singleCnt);
assertEquals(134, singleSetSize);
doc.close();
PDDocument src = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened.pdf"));
PDDocument dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened.pdf"));
pdfMergerUtility.appendDocument(dst, src);
// before solving PDFBOX-3999, the close() below brought
// IOException: COSStream has been closed and cannot be read.
src.close();
dst.save(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened-merged.pdf"));
dst.close();
doc = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-3999-GeneralForbearance-flattened-merged.pdf"));
checkForPageOrphans(doc);
// Assume that the merged tree has double element count
elementCounter = new ElementCounter();
elementCounter.walk(doc.getDocumentCatalog().getStructureTreeRoot().getK());
assertEquals(singleCnt * 2, elementCounter.cnt);
assertEquals(singleSetSize * 2, elementCounter.set.size());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testMergeBogusStructParents1.
/**
* PDFBOX-4429: merge into destination that has /StructParent(s) entries in the destination file
* but no structure tree.
*
* @throws IOException
*/
@Test
public void testMergeBogusStructParents1() throws IOException {
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4408.pdf", "https://issues.apache.org/jira/secure/attachment/12952086/form.pdf");
assumeTrue(inputPdf.exists());
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument src = PDDocument.load(inputPdf);
PDDocument dst = PDDocument.load(inputPdf);
dst.getDocumentCatalog().setStructureTreeRoot(null);
dst.getPage(0).setStructParents(9999);
dst.getPage(0).getAnnotations().get(0).setStructParent(9998);
pdfMergerUtility.appendDocument(dst, src);
checkWithNumberTree(dst);
checkForPageOrphans(dst);
src.close();
dst.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testMissingParentTreeNextKey.
/**
* PDFBOX-4009: Test that ParentTreeNextKey is recalculated correctly.
*/
@Test
public void testMissingParentTreeNextKey() throws IOException {
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4418-000314.pdf", "https://issues.apache.org/jira/secure/attachment/12953423/000314.pdf");
assumeTrue(inputPdf.exists());
PDDocument src = PDDocument.load(inputPdf);
PDDocument dst = PDDocument.load(inputPdf);
// existing numbers are 321..327; ParentTreeNextKey is 408.
// After deletion, it is recalculated in the merge 328.
// That value is added to all numbers of the destination,
// so the new numbers should be 321+328..327+328, i.e. 649..655,
// and this ParentTreeNextKey is 656 at the end.
dst.getDocumentCatalog().getStructureTreeRoot().getCOSObject().removeItem(COSName.PARENT_TREE_NEXT_KEY);
pdfMergerUtility.appendDocument(dst, src);
src.close();
dst.save(new File(TARGETTESTDIR, "PDFBOX-4418-000314-merged.pdf"));
dst.close();
dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-4418-000314-merged.pdf"));
assertEquals(656, dst.getDocumentCatalog().getStructureTreeRoot().getParentTreeNextKey());
dst.close();
}
Aggregations