Search in sources :

Example 6 with COSObjectable

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);
        }
    }
}
Also used : COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) COSArray(com.tom_roush.pdfbox.cos.COSArray) PDParentTreeValue(com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDParentTreeValue) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 7 with COSObjectable

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;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) List(java.util.List) COSObject(com.tom_roush.pdfbox.cos.COSObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with COSObjectable

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"));
}
Also used : PDNumberTreeNode(com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) PDStructureTreeRoot(com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) Test(org.junit.Test)

Example 9 with COSObjectable

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();
}
Also used : PDNumberTreeNode(com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) PDStructureTreeRoot(com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) Test(org.junit.Test)

Example 10 with COSObjectable

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"));
}
Also used : PDNumberTreeNode(com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) PDStructureTreeRoot(com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) Test(org.junit.Test)

Aggregations

COSObjectable (com.tom_roush.pdfbox.pdmodel.common.COSObjectable)11 PDNumberTreeNode (com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode)6 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 COSArray (com.tom_roush.pdfbox.cos.COSArray)4 PDStructureTreeRoot (com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot)4 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)3 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)3 File (java.io.File)3 Test (org.junit.Test)3 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 COSObject (com.tom_roush.pdfbox.cos.COSObject)2 COSStream (com.tom_roush.pdfbox.cos.COSStream)2 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)2 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)2 PDParentTreeValue (com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDParentTreeValue)2 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2