use of com.tom_roush.pdfbox.pdmodel.common.COSObjectable in project PdfBox-Android by TomRoush.
the class PDFMergerUtility method updatePageReferences.
/**
* Update the Pg and Obj references to the new (merged) page.
*/
private void updatePageReferences(PDFCloneUtility cloner, Map<Integer, COSObjectable> numberTreeAsMap, Map<COSDictionary, COSDictionary> objMapping) throws IOException {
for (COSObjectable obj : numberTreeAsMap.values()) {
if (obj == null) {
continue;
}
PDParentTreeValue val = (PDParentTreeValue) obj;
COSBase base = val.getCOSObject();
if (base instanceof COSArray) {
updatePageReferences(cloner, (COSArray) base, objMapping);
} else {
updatePageReferences(cloner, (COSDictionary) base, objMapping);
}
}
}
use of com.tom_roush.pdfbox.pdmodel.common.COSObjectable in project PdfBox-Android by TomRoush.
the class PDFCloneUtility method cloneForNewDocument.
/**
* Deep-clones the given object for inclusion into a different PDF document identified by
* the destination parameter.
* @param base the initial object as the root of the deep-clone operation
* @return the cloned instance of the base object
* @throws IOException if an I/O error occurs
*/
public COSBase cloneForNewDocument(Object base) throws IOException {
if (base == null) {
return null;
}
COSBase retval = clonedVersion.get(base);
if (retval != null) {
// we are done, it has already been converted.
return retval;
}
if (base instanceof COSBase && clonedValues.contains(base)) {
// Don't clone a clone
return (COSBase) base;
}
if (base instanceof List) {
COSArray array = new COSArray();
List<?> list = (List<?>) base;
for (Object obj : list) {
array.add(cloneForNewDocument(obj));
}
retval = array;
} else if (base instanceof COSObjectable && !(base instanceof COSBase)) {
retval = cloneForNewDocument(((COSObjectable) base).getCOSObject());
} else if (base instanceof COSObject) {
COSObject object = (COSObject) base;
retval = cloneForNewDocument(object.getObject());
} else if (base instanceof COSArray) {
COSArray newArray = new COSArray();
COSArray array = (COSArray) base;
for (int i = 0; i < array.size(); i++) {
newArray.add(cloneForNewDocument(array.get(i)));
}
retval = newArray;
} else if (base instanceof COSStream) {
COSStream originalStream = (COSStream) base;
COSStream stream = destination.getDocument().createCOSStream();
OutputStream output = stream.createRawOutputStream();
InputStream input = originalStream.createRawInputStream();
IOUtils.copy(input, output);
input.close();
output.close();
clonedVersion.put(base, stream);
for (Map.Entry<COSName, COSBase> entry : originalStream.entrySet()) {
stream.setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
}
retval = stream;
} else if (base instanceof COSDictionary) {
COSDictionary dic = (COSDictionary) base;
retval = new COSDictionary();
clonedVersion.put(base, retval);
for (Map.Entry<COSName, COSBase> entry : dic.entrySet()) {
((COSDictionary) retval).setItem(entry.getKey(), cloneForNewDocument(entry.getValue()));
}
} else {
retval = (COSBase) base;
}
clonedVersion.put(base, retval);
clonedValues.add(retval);
return retval;
}
use of com.tom_roush.pdfbox.pdmodel.common.COSObjectable in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testStructureTreeMerge7.
/**
* PDFBOX-4423: test merging a PDF where a widget has no StructParent.
*
* @throws IOException
*/
@Test
public void testStructureTreeMerge7() throws IOException {
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4423-000746.pdf", "https://issues.apache.org/jira/secure/attachment/12953866/000746.pdf");
assumeTrue(inputPdf.exists());
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument src = PDDocument.load(inputPdf);
PDStructureTreeRoot structureTreeRoot = src.getDocumentCatalog().getStructureTreeRoot();
PDNumberTreeNode parentTree = structureTreeRoot.getParentTree();
Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(33, numberTreeAsMap.size());
assertEquals(64, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(31, (int) Collections.min(numberTreeAsMap.keySet()));
assertEquals(126, structureTreeRoot.getParentTreeNextKey());
PDDocument dst = new PDDocument();
pdfMergerUtility.appendDocument(dst, src);
src.close();
dst.save(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
dst.close();
dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
checkWithNumberTree(dst);
checkForPageOrphans(dst);
structureTreeRoot = dst.getDocumentCatalog().getStructureTreeRoot();
parentTree = structureTreeRoot.getParentTree();
numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(33, numberTreeAsMap.size());
assertEquals(64, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(31, (int) Collections.min(numberTreeAsMap.keySet()));
assertEquals(64, structureTreeRoot.getParentTreeNextKey());
dst.close();
checkStructTreeRootCount(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
}
use of com.tom_roush.pdfbox.pdmodel.common.COSObjectable in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testParentTree.
/**
* Test of the parent tree. Didn't work before PDFBOX-4003 because of incompatible class for
* PDNumberTreeNode.
*
* @throws IOException
*/
@Test
public void testParentTree() throws IOException {
File inputPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-3999-GeneralForbearance.pdf", "https://issues.apache.org/jira/secure/attachment/12896905/GeneralForbearance.pdf");
assumeTrue(inputPdf.exists());
PDDocument doc = PDDocument.load(inputPdf);
PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
PDNumberTreeNode parentTree = structureTreeRoot.getParentTree();
parentTree.getValue(0);
Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(31, numberTreeAsMap.size());
assertEquals(31, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(0, (int) Collections.min(numberTreeAsMap.keySet()));
assertEquals(31, structureTreeRoot.getParentTreeNextKey());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.common.COSObjectable in project PdfBox-Android by TomRoush.
the class PDFMergerUtilityTest method testStructureTreeMerge6.
/**
* PDFBOX-4418: test merging PDFs where ParentTree have a hierarchy.
*
* @throws IOException
*/
@Test
public void testStructureTreeMerge6() throws IOException {
File srcPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4418-000671.pdf", "https://issues.apache.org/jira/secure/attachment/12953421/000671.pdf");
assumeTrue(srcPdf.exists());
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
PDDocument src = PDDocument.load(srcPdf);
PDStructureTreeRoot structureTreeRoot = src.getDocumentCatalog().getStructureTreeRoot();
PDNumberTreeNode parentTree = structureTreeRoot.getParentTree();
Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(381, numberTreeAsMap.size());
assertEquals(743, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(0, (int) Collections.min(numberTreeAsMap.keySet()));
assertEquals(743, structureTreeRoot.getParentTreeNextKey());
File dstPdf = TestResourceGenerator.downloadTestResource(TARGETPDFDIR, "PDFBOX-4418-000314.pdf", "https://issues.apache.org/jira/secure/attachment/12953423/000314.pdf");
assumeTrue(dstPdf.exists());
PDDocument dst = PDDocument.load(dstPdf);
structureTreeRoot = dst.getDocumentCatalog().getStructureTreeRoot();
parentTree = structureTreeRoot.getParentTree();
numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(7, numberTreeAsMap.size());
assertEquals(328, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(321, (int) Collections.min(numberTreeAsMap.keySet()));
// ParentTreeNextKey should be 321 but PDF has a higher value
assertEquals(408, structureTreeRoot.getParentTreeNextKey());
pdfMergerUtility.appendDocument(dst, src);
src.close();
dst.save(new File(TARGETTESTDIR, "PDFBOX-4418-merged.pdf"));
dst.close();
dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-4418-merged.pdf"));
checkWithNumberTree(dst);
checkForPageOrphans(dst);
structureTreeRoot = dst.getDocumentCatalog().getStructureTreeRoot();
parentTree = structureTreeRoot.getParentTree();
numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
assertEquals(381 + 7, numberTreeAsMap.size());
assertEquals(408 + 743, Collections.max(numberTreeAsMap.keySet()) + 1);
assertEquals(321, (int) Collections.min(numberTreeAsMap.keySet()));
assertEquals(408 + 743, structureTreeRoot.getParentTreeNextKey());
dst.close();
checkStructTreeRootCount(new File(TARGETTESTDIR, "PDFBOX-4418-merged.pdf"));
}
Aggregations