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

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

Introduction

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

Prototype

COSName P

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

Click Source Link

Usage

From source file:com.formkiq.core.service.generator.pdfbox.SigUtils.java

License:Apache License

/**
 * Get the access permissions granted for this document in the DocMDP
 * transform parameters dictionary. Details are described in the table
 * "Entries in the DocMDP transform parameters dictionary" in the PDF
 * specification.//from ww  w  . j  a v  a 2  s  .com
 *
 * @param doc
 *            document.
 * @return the permission value. 0 means no DocMDP transform parameters
 *         dictionary exists. Other return values are 1, 2 or 3. 2 is also
 *         returned if the DocMDP transform parameters dictionary is found
 *         but did not contain a /P entry, or if the value is outside the
 *         valid range.
 */
public static int getMDPPermission(final PDDocument doc) {

    final int maxPerm = 3;
    COSBase base = doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PERMS);
    if (base instanceof COSDictionary) {
        COSDictionary permsDict = (COSDictionary) base;
        base = permsDict.getDictionaryObject(COSName.DOCMDP);
        if (base instanceof COSDictionary) {
            COSDictionary signatureDict = (COSDictionary) base;
            base = signatureDict.getDictionaryObject("Reference");
            if (base instanceof COSArray) {
                COSArray refArray = (COSArray) base;
                for (int i = 0; i < refArray.size(); ++i) {
                    base = refArray.getObject(i);
                    if (base instanceof COSDictionary) {
                        COSDictionary sigRefDict = (COSDictionary) base;
                        if (COSName.DOCMDP.equals(sigRefDict.getDictionaryObject("TransformMethod"))) {
                            base = sigRefDict.getDictionaryObject("TransformParams");
                            if (base instanceof COSDictionary) {
                                COSDictionary dict = (COSDictionary) base;
                                int accessPermissions = dict.getInt(COSName.P, 2);
                                if (accessPermissions < 1 || accessPermissions > maxPerm) {
                                    accessPermissions = 2;
                                }
                                return accessPermissions;
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

From source file:com.formkiq.core.service.generator.pdfbox.SigUtils.java

License:Apache License

/**
 * Set the access permissions granted for this document in the DocMDP
 * transform parameters dictionary. Details are described in the table
 * "Entries in the DocMDP transform parameters dictionary" in the PDF
 * specification./*from  w  ww.  j a  v  a 2s  .  c  om*/
 *
 * @param doc
 *            The document.
 * @param signature
 *            The signature object.
 * @param accessPermissions
 *            The permission value (1, 2 or 3).
 */
public static void setMDPPermission(final PDDocument doc, final PDSignature signature,
        final int accessPermissions) {

    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.DOCMDP);
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:com.modemo.javase.signature.SigUtils.java

License:Apache License

/**
 * Get the access permissions granted for this document in the DocMDP transform parameters
 * dictionary. Details are described in the table "Entries in the DocMDP transform parameters
 * dictionary" in the PDF specification.
 *
 * @param doc document.//from w  w w .  j  av a 2  s  .  c o  m
 * @return the permission value. 0 means no DocMDP transform parameters dictionary exists. Other
 * return values are 1, 2 or 3. 2 is also returned if the DocMDP transform parameters dictionary
 * is found but did not contain a /P entry, or if the value is outside the valid range.
 */
public static int getMDPPermission(PDDocument doc) {
    COSBase base = doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PERMS);
    if (base instanceof COSDictionary) {
        COSDictionary permsDict = (COSDictionary) base;
        base = permsDict.getDictionaryObject(COSName.DOCMDP);
        if (base instanceof COSDictionary) {
            COSDictionary signatureDict = (COSDictionary) base;
            base = signatureDict.getDictionaryObject("Reference");
            if (base instanceof COSArray) {
                COSArray refArray = (COSArray) base;
                for (int i = 0; i < refArray.size(); ++i) {
                    base = refArray.getObject(i);
                    if (base instanceof COSDictionary) {
                        COSDictionary sigRefDict = (COSDictionary) base;
                        if (COSName.DOCMDP.equals(sigRefDict.getDictionaryObject("TransformMethod"))) {
                            base = sigRefDict.getDictionaryObject("TransformParams");
                            if (base instanceof COSDictionary) {
                                COSDictionary transformDict = (COSDictionary) base;
                                int accessPermissions = transformDict.getInt(COSName.P, 2);
                                if (accessPermissions < 1 || accessPermissions > 3) {
                                    accessPermissions = 2;
                                }
                                return accessPermissions;
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

From source file:com.modemo.javase.signature.SigUtils.java

License:Apache License

/**
 * Set the access permissions granted for this document in the DocMDP transform parameters
 * dictionary. Details are described in the table "Entries in the DocMDP transform parameters
 * dictionary" in the PDF specification.
 *
 * @param doc The document./*from www  .j av  a2  s . c o  m*/
 * @param signature The signature object.
 * @param accessPermissions The permission value (1, 2 or 3).
 */
static public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions) {
    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.DOCMDP);
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

From source file:fixture.pdfboxeg.CreateSignatureBase.java

License:Apache License

/**
 * Get the access permissions granted for this document in the DocMDP transform parameters
 * dictionary. Details are described in the table "Entries in the DocMDP transform parameters
 * dictionary" in the PDF specification.
 *
 * @param doc document./* w  ww .  j  av a2  s  .com*/
 * @return the permission value. 0 means no DocMDP transform parameters dictionary exists. Other
 * return values are 1, 2 or 3. 2 is also returned if the DocMDP transform parameters dictionary
 * is found but did not contain a /P entry, or if the value is outside the valid range.
 */
public int getMDPPermission(PDDocument doc) {
    COSBase base = doc.getDocumentCatalog().getCOSObject().getDictionaryObject(COSName.PERMS);
    if (base instanceof COSDictionary) {
        COSDictionary permsDict = (COSDictionary) base;
        base = permsDict.getDictionaryObject(COSName.DOCMDP);
        if (base instanceof COSDictionary) {
            COSDictionary signatureDict = (COSDictionary) base;
            base = signatureDict.getDictionaryObject("Reference");
            if (base instanceof COSArray) {
                COSArray refArray = (COSArray) base;
                for (int i = 0; i < refArray.size(); ++i) {
                    base = refArray.getObject(i);
                    if (base instanceof COSDictionary) {
                        COSDictionary sigRefDict = (COSDictionary) base;
                        if (COSName.DOCMDP.equals(sigRefDict.getDictionaryObject("TransformMethod"))) {
                            base = sigRefDict.getDictionaryObject("TransformParams");
                            if (base instanceof COSDictionary) {
                                COSDictionary transformDict = (COSDictionary) base;
                                int accessPermissions = transformDict.getInt(COSName.P, 2);
                                if (accessPermissions < 1 || accessPermissions > 3) {
                                    accessPermissions = 2;
                                }
                                return accessPermissions;
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

From source file:fixture.pdfboxeg.CreateSignatureBase.java

License:Apache License

public void setMDPPermission(PDDocument doc, PDSignature signature, int accessPermissions) {
    COSDictionary sigDict = signature.getCOSObject();

    // DocMDP specific stuff
    COSDictionary transformParameters = new COSDictionary();
    transformParameters.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
    transformParameters.setInt(COSName.P, accessPermissions);
    transformParameters.setName(COSName.V, "1.2");
    transformParameters.setNeedToBeUpdated(true);

    COSDictionary referenceDict = new COSDictionary();
    referenceDict.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
    referenceDict.setItem("TransformMethod", COSName.getPDFName("DocMDP"));
    referenceDict.setItem("DigestMethod", COSName.getPDFName("SHA1"));
    referenceDict.setItem("TransformParams", transformParameters);
    referenceDict.setNeedToBeUpdated(true);

    COSArray referenceArray = new COSArray();
    referenceArray.add(referenceDict);/*  w w w . java  2s . c  om*/
    sigDict.setItem("Reference", referenceArray);
    referenceArray.setNeedToBeUpdated(true);

    // Catalog
    COSDictionary catalogDict = doc.getDocumentCatalog().getCOSObject();
    COSDictionary permsDict = new COSDictionary();
    catalogDict.setItem(COSName.PERMS, permsDict);
    permsDict.setItem(COSName.DOCMDP, signature);
    catalogDict.setNeedToBeUpdated(true);
    permsDict.setNeedToBeUpdated(true);
}

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;//  w w w .ja 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);
            if (annot1 instanceof COSObject) {
                COSObject annot = (COSObject) annot1;
                COSObject fieldObject = annot;
                COSDictionary field = (COSDictionary) fieldObject.getObject();
                COSObject parent;// w w w  .j  a  va 2  s . c o  m
                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;
}

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

License:Apache License

private void createParents(COSArray markedContentParents) throws IOException {
    for (COSBase entry : markedContentParents) {
        COSObject elemCos = (COSObject) entry;
        COSObject elemParent = (COSObject) elemCos.getItem(COSName.P);
        PDFStructElem elem = structElemCache.get((int) elemCos.getObjectNumber());
        createParents(elemCos, elemParent, elem);
    }//w w  w  .  j  a va 2s  . c  om
}

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

License:Apache License

private void createParents(COSObject cosElem, COSObject cosParentElem, PDFStructElem elem) throws IOException {
    int elemObjectID = (int) cosParentElem.getObjectNumber();
    COSDictionary parentElemDictionary = (COSDictionary) cosParentElem.getObject();
    PDFStructElem elemParent = structElemCache.get(elemObjectID);
    if (isStructureTreeRoot(parentElemDictionary)) {
        elem.setParent(currentSessionElem);
        currentSessionElem.addKid(elem);
        topElems.add(cosElem);/*from www .j a  v a  2s. c  o m*/
    } else if (elemParent != null) {
        if (!checkIfStructureTypeIsPresent(parentElemDictionary, StandardStructureTypes.TR)) {
            elem.setParent(elemParent);
            int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
            elemParent.addKidInSpecificOrder(position, elem);
        }
    } else if (!checkIfStructureTypeIsPresent(parentElemDictionary, StandardStructureTypes.DOCUMENT)) {
        elemParent = createAndRegisterStructElem(cosParentElem);
        copyElemEntries(cosParentElem, elemParent);
        elem.setParent(elemParent);
        fillKidsWithNull(elemParent, (COSDictionary) cosParentElem.getObject());
        if (((COSName) parentElemDictionary.getDictionaryObject(COSName.S)).getName()
                .equals(StandardStructureTypes.TR)) {
            COSBase rowKids = parentElemDictionary.getItem(COSName.K);
            createKids(rowKids, parentElemDictionary, elemParent, true);
        } else {
            int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
            elemParent.addKidInSpecificOrder(position, elem);
        }
        COSObject parentObj = (COSObject) parentElemDictionary.getItem(COSName.P);
        createParents(cosParentElem, parentObj, elemParent);
    } else {
        elem.setParent(currentSessionElem);
        int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
        currentSessionElem.addKidInSpecificOrder(position, elem);
        topElems.add(cosElem);
    }
}