Java Code Examples for org.apache.pdfbox.cos.COSDictionary#getCOSArray()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#getCOSArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Helper to get kids from malformed PDFs.
 * @param node page tree node
 * @return list of kids
 */
private List<COSDictionary> getKids(COSDictionary node)
{
    List<COSDictionary> result = new ArrayList<COSDictionary>();

    COSArray kids = node.getCOSArray(COSName.KIDS);
    if (kids == null)
    {
        // probably a malformed PDF
        return result;
    }

    for (int i = 0, size = kids.size(); i < size; i++)
    {
        COSBase base = kids.getObject(i);
        if (base instanceof COSDictionary)
        {
            result.add((COSDictionary) base);
        }
        else
        {
            LOG.warn("COSDictionary expected, but got " +
                    (base == null ? "null" : base.getClass().getSimpleName()));
        }
    }

    return result;
}
 
Example 2
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will write the trailer to the PDF document.
 *
 * @param doc The document to create the trailer for.
 *
 * @throws IOException If there is an IOError while writing the document.
 */
protected void doWriteTrailer(COSDocument doc) throws IOException
{
    getStandardOutput().write(TRAILER);
    getStandardOutput().writeEOL();

    COSDictionary trailer = doc.getTrailer();
    //sort xref, needed only if object keys not regenerated
    Collections.sort(getXRefEntries());
    COSWriterXRefEntry lastEntry = getXRefEntries().get( getXRefEntries().size()-1);
    trailer.setLong(COSName.SIZE, lastEntry.getKey().getNumber()+1);
    // Only need to stay, if an incremental update will be performed
    if (!incrementalUpdate) 
    {
      trailer.removeItem( COSName.PREV );
    }
    if (!doc.isXRefStream())
    {
        trailer.removeItem( COSName.XREF_STM );
    }
    // Remove a checksum if present
    trailer.removeItem( COSName.DOC_CHECKSUM );

    COSArray idArray = trailer.getCOSArray(COSName.ID);
    if (idArray != null)
    {
        idArray.setDirect(true);
    }

    trailer.accept(this);
}
 
Example 3
Source File: CreateSignature.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <p>
 * A minimal signing frame work merely requiring a {@link SignatureInterface}
 * instance signing an existing field and actually locking fields the transform
 * requires to be locked.
 * </p>
 * @see #signExistingFieldWithLock(PDDocument, OutputStream, SignatureInterface)
 */
void signAndLockExistingFieldWithLock(PDDocument document, OutputStream output, SignatureInterface signatureInterface) throws IOException
{
    PDSignatureField signatureField = document.getSignatureFields().get(0);
    PDSignature signature = new PDSignature();
    signatureField.setValue(signature);

    COSBase lock = signatureField.getCOSObject().getDictionaryObject(COS_NAME_LOCK);
    if (lock instanceof COSDictionary)
    {
        COSDictionary lockDict = (COSDictionary) lock;
        COSDictionary transformParams = new COSDictionary(lockDict);
        transformParams.setItem(COSName.TYPE, COSName.getPDFName("TransformParams"));
        transformParams.setItem(COSName.V, COSName.getPDFName("1.2"));
        transformParams.setDirect(true);
        COSDictionary sigRef = new COSDictionary();
        sigRef.setItem(COSName.TYPE, COSName.getPDFName("SigRef"));
        sigRef.setItem(COSName.getPDFName("TransformParams"), transformParams);
        sigRef.setItem(COSName.getPDFName("TransformMethod"), COSName.getPDFName("FieldMDP"));
        sigRef.setItem(COSName.getPDFName("Data"), document.getDocumentCatalog());
        sigRef.setDirect(true);
        COSArray referenceArray = new COSArray();
        referenceArray.add(sigRef);
        signature.getCOSObject().setItem(COSName.getPDFName("Reference"), referenceArray);

        final Predicate<PDField> shallBeLocked;
        final COSArray fields = lockDict.getCOSArray(COSName.FIELDS);
        final List<String> fieldNames = fields == null ? Collections.emptyList() :
            fields.toList().stream().filter(c -> (c instanceof COSString)).map(s -> ((COSString)s).getString()).collect(Collectors.toList());
        final COSName action = lockDict.getCOSName(COSName.getPDFName("Action"));
        if (action.equals(COSName.getPDFName("Include"))) {
            shallBeLocked = f -> fieldNames.contains(f.getFullyQualifiedName());
        } else if (action.equals(COSName.getPDFName("Exclude"))) {
            shallBeLocked = f -> !fieldNames.contains(f.getFullyQualifiedName());
        } else if (action.equals(COSName.getPDFName("All"))) {
            shallBeLocked = f -> true;
        } else { // unknown action, lock nothing
            shallBeLocked = f -> false;
        }
        lockFields(document.getDocumentCatalog().getAcroForm().getFields(), shallBeLocked);
    }

    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
    signature.setName("blablabla");
    signature.setLocation("blablabla");
    signature.setReason("blablabla");
    signature.setSignDate(Calendar.getInstance());
    document.addSignature(signature);
    ExternalSigningSupport externalSigning =
            document.saveIncrementalForExternalSigning(output);
    // invoke external signature service
    byte[] cmsSignature = signatureInterface.sign(externalSigning.getContent());
    // set signature bytes received from the service
    externalSigning.setSignature(cmsSignature);
}