Example usage for org.apache.pdfbox.cos COSName PARENT

List of usage examples for org.apache.pdfbox.cos COSName PARENT

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSName PARENT.

Prototype

COSName PARENT

To view the source code for org.apache.pdfbox.cos COSName PARENT.

Click Source Link

Usage

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void handleAnnotations(PDDocument sourceDoc, PDPage page, AffineTransform at) throws IOException {
    PDDocumentCatalog srcCatalog = sourceDoc.getDocumentCatalog();
    PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
    List pageAnnotations = page.getAnnotations();
    if (srcAcroForm == null && pageAnnotations.isEmpty()) {
        return;//www .j a v a 2 s.  co  m
    }

    moveAnnotations(page, pageAnnotations, at);

    //Pseudo-cache the target page in place of the original source page.
    //This essentially replaces the original page reference with the target page.
    COSObject cosPage = null;
    COSDictionary parentDic = (COSDictionary) page.getCOSObject().getDictionaryObject(COSName.PARENT,
            COSName.P);
    COSArray kids = (COSArray) parentDic.getDictionaryObject(COSName.KIDS);
    for (int i = 0; i < kids.size(); i++) {
        //Hopefully safe to cast, as kids need to be indirect objects
        COSObject kid = (COSObject) kids.get(i);
        if (!pageNumbers.containsKey(i)) {
            PDFArray a = new PDFArray();
            a.add(null);
            pdfDoc.assignObjectNumber(a);
            pdfDoc.addTrailerObject(a);
            pageNumbers.put(i, a);
        }
        cacheClonedObject(kid, pageNumbers.get(i));
        if (kid.getObject() == page.getCOSObject()) {
            cosPage = kid;
        }
    }
    if (cosPage == null) {
        throw new IOException("Illegal PDF. Page not part of parent page node.");
    }

    Set<COSObject> fields = copyAnnotations(page);

    boolean formAlreadyCopied = getCachedClone(srcAcroForm) != null;
    PDFRoot catalog = this.pdfDoc.getRoot();
    PDFDictionary destAcroForm = (PDFDictionary) catalog.get(COSName.ACRO_FORM.getName());
    if (formAlreadyCopied) {
        //skip, already copied
    } else if (destAcroForm == null) {
        if (srcAcroForm != null) {
            //With this, only the first PDF's AcroForm is copied over. If later AcroForms have
            //different properties besides the actual fields, these get lost. Only fields
            //get merged.
            Collection exclude = Collections.singletonList(COSName.FIELDS);
            destAcroForm = (PDFDictionary) cloneForNewDocument(srcAcroForm, srcAcroForm, exclude);
        } else {
            //Work-around for incorrectly split PDFs which lack an AcroForm but have widgets
            //on pages. This doesn't handle the case where field dicts have "C" entries
            //(for the "CO" entry), so this may produce problems, but we have almost no chance
            //to guess the calculation order.
            destAcroForm = new PDFDictionary(pdfDoc.getRoot());
        }
        pdfDoc.registerObject(destAcroForm);
        catalog.put(COSName.ACRO_FORM.getName(), destAcroForm);
    }
    PDFArray clonedFields = (PDFArray) destAcroForm.get(COSName.FIELDS.getName());
    if (clonedFields == null) {
        clonedFields = new PDFArray();
        destAcroForm.put(COSName.FIELDS.getName(), clonedFields);
    }
    for (COSObject field : fields) {
        PDFDictionary clone = (PDFDictionary) cloneForNewDocument(field, field, Arrays.asList(COSName.KIDS));
        clonedFields.add(clone);
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private Set<COSObject> copyAnnotations(PDPage page) throws IOException {
    COSArray annots = (COSArray) page.getCOSObject().getDictionaryObject(COSName.ANNOTS);
    Set<COSObject> fields = Collections.emptySet();
    if (annots != null) {
        fields = new TreeSet<COSObject>(new CompareFields());
        for (Object annot1 : annots) {
            Collection<COSName> exclude = new ArrayList<COSName>();
            exclude.add(COSName.P);//from   www  .j  a  va2  s.c om
            if (annot1 instanceof COSObject) {
                COSObject annot = (COSObject) annot1;
                COSObject fieldObject = annot;
                COSDictionary field = (COSDictionary) fieldObject.getObject();
                COSObject parent;
                while ((parent = (COSObject) field.getItem(COSName.PARENT)) != null) {
                    fieldObject = parent;
                    field = (COSDictionary) fieldObject.getObject();
                }
                fields.add(fieldObject);

                if (((COSDictionary) annot.getObject()).getItem(COSName.getPDFName("StructParent")) != null) {
                    exclude.add(COSName.PARENT);
                }
            }

            PDFObject clonedAnnot = (PDFObject) cloneForNewDocument(annot1, annot1, exclude);
            if (clonedAnnot instanceof PDFDictionary) {
                clonedAnnot.setParent(targetPage);
                updateAnnotationLink((PDFDictionary) clonedAnnot);
            }
            targetPage.addAnnotation(clonedAnnot);
        }
    }
    return fields;
}