Search in sources :

Example 1 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.

the class PDDocument method assignAcroFormDefaultResource.

private void assignAcroFormDefaultResource(PDAcroForm acroForm, COSDictionary newDict) {
    // read and set/update AcroForm default resource dictionary /DR if available
    COSBase newBase = newDict.getDictionaryObject(COSName.DR);
    if (newBase instanceof COSDictionary) {
        COSDictionary newDR = (COSDictionary) newBase;
        PDResources defaultResources = acroForm.getDefaultResources();
        if (defaultResources == null) {
            acroForm.getCOSObject().setItem(COSName.DR, newDR);
            newDR.setDirect(true);
            newDR.setNeedToBeUpdated(true);
        } else {
            COSDictionary oldDR = defaultResources.getCOSObject();
            COSBase newXObjectBase = newDR.getItem(COSName.XOBJECT);
            COSBase oldXObjectBase = oldDR.getItem(COSName.XOBJECT);
            if (newXObjectBase instanceof COSDictionary && oldXObjectBase instanceof COSDictionary) {
                ((COSDictionary) oldXObjectBase).addAll((COSDictionary) newXObjectBase);
                oldDR.setNeedToBeUpdated(true);
            }
        }
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 2 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.

the class PDDocument method prepareVisibleSignature.

private void prepareVisibleSignature(PDSignatureField signatureField, PDAcroForm acroForm, COSDocument visualSignature) {
    // Obtain visual signature object
    boolean annotNotFound = true;
    boolean sigFieldNotFound = true;
    for (COSObject cosObject : visualSignature.getObjects()) {
        if (!annotNotFound && !sigFieldNotFound) {
            break;
        }
        COSBase base = cosObject.getObject();
        if (base instanceof COSDictionary) {
            COSDictionary cosBaseDict = (COSDictionary) base;
            // Search for signature annotation
            COSBase type = cosBaseDict.getDictionaryObject(COSName.TYPE);
            if (annotNotFound && COSName.ANNOT.equals(type)) {
                assignSignatureRectangle(signatureField, cosBaseDict);
                annotNotFound = false;
            }
            // Search for signature field
            COSBase fieldType = cosBaseDict.getDictionaryObject(COSName.FT);
            COSBase apDict = cosBaseDict.getDictionaryObject(COSName.AP);
            if (sigFieldNotFound && COSName.SIG.equals(fieldType) && apDict instanceof COSDictionary) {
                assignAppearanceDictionary(signatureField, (COSDictionary) apDict);
                assignAcroFormDefaultResource(acroForm, cosBaseDict);
                sigFieldNotFound = false;
            }
        }
    }
    if (annotNotFound || sigFieldNotFound) {
        throw new IllegalArgumentException("Template is missing required objects");
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 3 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.

the class PDDocument method addSignatureField.

/**
 * This will add a list of signature fields to the document.
 *
 * @param sigFields are the PDSignatureFields that should be added to the document
 * @param signatureInterface is an interface whose implementation provides
 * signing capabilities. Can be null if external signing if used.
 * @param options signature options
 * @throws IOException if there is an error creating required fields
 * @deprecated The method is misleading, because only one signature may be
 * added in a document. The method will be removed in the future.
 */
@Deprecated
public void addSignatureField(List<PDSignatureField> sigFields, SignatureInterface signatureInterface, SignatureOptions options) throws IOException {
    PDDocumentCatalog catalog = getDocumentCatalog();
    catalog.getCOSObject().setNeedToBeUpdated(true);
    PDAcroForm acroForm = catalog.getAcroForm();
    if (acroForm == null) {
        acroForm = new PDAcroForm(this);
        catalog.setAcroForm(acroForm);
    }
    COSDictionary acroFormDict = acroForm.getCOSObject();
    acroFormDict.setDirect(true);
    acroFormDict.setNeedToBeUpdated(true);
    if (!acroForm.isSignaturesExist()) {
        // 1 if at least one signature field is available
        acroForm.setSignaturesExist(true);
    }
    List<PDField> acroformFields = acroForm.getFields();
    for (PDSignatureField sigField : sigFields) {
        sigField.getCOSObject().setNeedToBeUpdated(true);
        // Check if the field already exists
        boolean checkSignatureField = checkSignatureField(acroForm.getFieldIterator(), sigField);
        if (checkSignatureField) {
            sigField.getCOSObject().setNeedToBeUpdated(true);
        } else {
            acroformFields.add(sigField);
        }
        // Check if we need to add a signature
        if (sigField.getSignature() != null) {
            sigField.getCOSObject().setNeedToBeUpdated(true);
            if (options == null) {
            // TODO ??
            }
            addSignature(sigField.getSignature(), signatureInterface, options);
        }
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) PDSignatureField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDSignatureField)

Example 4 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.

the class PDDocument method getDocumentCatalog.

/**
 * This will get the document CATALOG. This is guaranteed to not return null.
 *
 * @return The documents /Root dictionary
 */
public PDDocumentCatalog getDocumentCatalog() {
    if (documentCatalog == null) {
        COSDictionary trailer = document.getTrailer();
        COSBase dictionary = trailer.getDictionaryObject(COSName.ROOT);
        if (dictionary instanceof COSDictionary) {
            documentCatalog = new PDDocumentCatalog(this, (COSDictionary) dictionary);
        } else {
            documentCatalog = new PDDocumentCatalog(this);
        }
    }
    return documentCatalog;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 5 with COSDictionary

use of com.tom_roush.pdfbox.cos.COSDictionary in project PdfBox-Android by TomRoush.

the class PDFMergerUtility method mergeIDTree.

private void mergeIDTree(PDFCloneUtility cloner, PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree) throws IOException {
    PDNameTreeNode<PDStructureElement> srcIDTree = srcStructTree.getIDTree();
    PDNameTreeNode<PDStructureElement> destIDTree = destStructTree.getIDTree();
    if (srcIDTree == null) {
        return;
    }
    if (destIDTree == null) {
        destIDTree = new PDStructureElementNameTreeNode();
    }
    Map<String, PDStructureElement> srcNames = getIDTreeAsMap(srcIDTree);
    Map<String, PDStructureElement> destNames = getIDTreeAsMap(destIDTree);
    for (Map.Entry<String, PDStructureElement> entry : srcNames.entrySet()) {
        if (destNames.containsKey(entry.getKey())) {
            Log.w("PdfBox-Android", "key " + entry.getKey() + " already exists in destination IDTree");
        } else {
            destNames.put(entry.getKey(), new PDStructureElement((COSDictionary) cloner.cloneForNewDocument(entry.getValue().getCOSObject())));
        }
    }
    destIDTree = new PDStructureElementNameTreeNode();
    destIDTree.setNames(destNames);
    destStructTree.setIDTree(destIDTree);
// Note that all elements are stored flatly. This could become a problem for large files
// when these are opened in a viewer that uses the tagging information.
// If this happens, then PDNameTreeNode should be improved with a convenience method that
// stores the map into a B+Tree, see https://en.wikipedia.org/wiki/B+_tree
}
Also used : PDStructureElementNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDStructureElementNameTreeNode) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PDStructureElement(com.tom_roush.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement)

Aggregations

COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)221 COSArray (com.tom_roush.pdfbox.cos.COSArray)68 COSBase (com.tom_roush.pdfbox.cos.COSBase)68 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)24 COSName (com.tom_roush.pdfbox.cos.COSName)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)22 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)15 HashMap (java.util.HashMap)14 COSStream (com.tom_roush.pdfbox.cos.COSStream)13 Map (java.util.Map)12 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 COSString (com.tom_roush.pdfbox.cos.COSString)7 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)6 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 List (java.util.List)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Test (org.junit.Test)5