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

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#setDirect() . 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: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField)
        throws IOException
{
    @SuppressWarnings("unchecked")
    List<PDField> acroFormFields = acroForm.getFields();
    COSDictionary acroFormDict = acroForm.getCOSObject();
    acroForm.setSignaturesExist(true);
    acroForm.setAppendOnly(true);
    acroFormDict.setDirect(true);
    acroFormFields.add(signatureField);
    acroForm.setDefaultAppearance("/sylfaen 0 Tf 0 g");
    pdfStructure.setAcroFormFields(acroFormFields);
    pdfStructure.setAcroFormDictionary(acroFormDict);
    LOG.info("AcroForm dictionary has been created");
}
 
Example 2
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void assignAppearanceDictionary(PDSignatureField signatureField, COSDictionary apDict)
{
    // read and set Appearance Dictionary
    PDAppearanceDictionary ap = new PDAppearanceDictionary(apDict);
    apDict.setDirect(true);
    signatureField.getWidgets().get(0).setAppearance(ap);
}
 
Example 3
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
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);
            }
        }
    }
}
 
Example 4
Source File: PDPropBuildDataDict.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 */
public PDPropBuildDataDict()
{
    dictionary = new COSDictionary();
    // the specification claim to use direct objects
    dictionary.setDirect(true);
}
 
Example 5
Source File: PDSeedValue.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Default constructor.
 */
public PDSeedValue()
{
    dictionary = new COSDictionary();
    dictionary.setItem(COSName.TYPE, COSName.SV);
    dictionary.setDirect(true); // the specification claim to use direct objects
}
 
Example 6
Source File: CreateSignature.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * A minimal signing frame work merely requiring a {@link SignatureInterface}
 * instance signing an existing field.
 * </p>
 * @see #testSignWithLocking()
 */
void signExistingFieldWithLock(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);
    }

    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);
}
 
Example 7
Source File: PDSeedValueMDP.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public PDSeedValueMDP()
{
    dictionary = new COSDictionary();
    dictionary.setDirect(true);
}
 
Example 8
Source File: PDSeedValueTimeStamp.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public PDSeedValueTimeStamp()
{
    dictionary = new COSDictionary();
    dictionary.setDirect(true);
}
 
Example 9
Source File: PDPropBuild.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public PDPropBuild()
{
    dictionary = new COSDictionary();
    dictionary.setDirect(true); // the specification claim to use direct objects
}
 
Example 10
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);
}
 
Example 11
Source File: PdfBoxSignatureService.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private COSDictionary buildDSSDictionary(PDDocument pdDocument, List<DSSDictionaryCallback> callbacks)
		throws IOException {
	COSDictionary dss = new COSDictionary();

	Map<String, Long> knownObjects = buildKnownObjects(callbacks);

	Map<String, COSStream> streams = new HashMap<>();

	Set<CRLToken> allCrls = new HashSet<>();
	Set<OCSPToken> allOcsps = new HashSet<>();
	Set<CertificateToken> allCertificates = new HashSet<>();

	COSDictionary vriDictionary = new COSDictionary();
	for (DSSDictionaryCallback callback : callbacks) {
		COSDictionary sigVriDictionary = new COSDictionary();
		sigVriDictionary.setDirect(true);

		Set<CertificateToken> currentCerts = callback.getCertificates();
		if (Utils.isCollectionNotEmpty(currentCerts)) {
			allCertificates.addAll(currentCerts);
			sigVriDictionary.setItem(PAdESConstants.CERT_ARRAY_NAME_VRI,
					buildArray(pdDocument, streams, currentCerts, knownObjects));
		}

		List<OCSPToken> currentOcsps = callback.getOcsps();
		if (Utils.isCollectionNotEmpty(currentOcsps)) {
			allOcsps.addAll(currentOcsps);
			sigVriDictionary.setItem(PAdESConstants.OCSP_ARRAY_NAME_VRI,
					buildArray(pdDocument, streams, currentOcsps, knownObjects));
		}

		List<CRLToken> currentCrls = callback.getCrls();
		if (Utils.isCollectionNotEmpty(currentCrls)) {
			allCrls.addAll(currentCrls);
			sigVriDictionary.setItem(PAdESConstants.CRL_ARRAY_NAME_VRI,
					buildArray(pdDocument, streams, currentCrls, knownObjects));
		}

		// We can't use CMSSignedData, the pdSignature content is trimmed (000000)
		String vriKey = callback.getSignature().getVRIKey();
		vriDictionary.setItem(vriKey, sigVriDictionary);
	}
	dss.setItem(PAdESConstants.VRI_DICTIONARY_NAME, vriDictionary);

	if (Utils.isCollectionNotEmpty(allCertificates)) {
		dss.setItem(PAdESConstants.CERT_ARRAY_NAME_DSS,
				buildArray(pdDocument, streams, allCertificates, knownObjects));
	}

	if (Utils.isCollectionNotEmpty(allOcsps)) {
		dss.setItem(PAdESConstants.OCSP_ARRAY_NAME_DSS, buildArray(pdDocument, streams, allOcsps, knownObjects));
	}

	if (Utils.isCollectionNotEmpty(allCrls)) {
		dss.setItem(PAdESConstants.CRL_ARRAY_NAME_DSS, buildArray(pdDocument, streams, allCrls, knownObjects));
	}

	return dss;
}