Example usage for org.apache.pdfbox.pdmodel PDDocument getVersion

List of usage examples for org.apache.pdfbox.pdmodel PDDocument getVersion

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument getVersion.

Prototype

public float getVersion() 

Source Link

Document

Returns the PDF specification version this document conforms to.

Usage

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

License:Apache License

/**
 * Sets value of {@link PDSignatureField}.
 * @param doc {@link PDDocument}/*  w ww  .  j  ava  2 s  .  c  o m*/
 * @param field {@link PDSignatureField}
 * @param signatureInputStream {@link InputStream}
 * @return {@link SignatureOptions}
 * @throws IOException IOException
 */
private SignatureOptions setValue(final PDDocument doc, final PDSignatureField field,
        final InputStream signatureInputStream) throws IOException {

    int accessPermissions = SigUtils.getMDPPermission(doc);
    if (accessPermissions == 1) {
        throw new IllegalStateException("No changes to the document are "
                + "permitted due to DocMDP transform parameters " + "dictionary");
    }

    // retrieve signature dictionary
    PDSignature signature = field.getSignature();

    if (signature == null) {
        signature = new PDSignature();
        // after solving PDFBOX-3524 - signatureField.setValue(signature)
        // until then:
        field.getCOSObject().setItem(COSName.V, signature);
    } else {
        throw new IllegalStateException(
                "The signature field " + field.getFullyQualifiedName() + " is already signed.");
    }

    // Optional: certify
    // can be done only if version is at least 1.5 and if not already set
    // doing this on a PDF/A-1b file fails validation by Adobe
    // preflight (PDFBOX-3821)
    // PDF/A-1b requires PDF version 1.4 max, so don't increase the version
    // on such files.
    final float version = 1.5f;
    if (doc.getVersion() >= version && accessPermissions == 0) {
        SigUtils.setMDPPermission(doc, signature, 2);
    }

    PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
    if (acroForm != null && acroForm.getNeedAppearances()) {
        // PDFBOX-3738 NeedAppearances true results in visible signature
        // becoming invisible
        // with Adobe Reader
        if (acroForm.getFields().isEmpty()) {
            // we can safely delete it if there are no fields
            acroForm.getCOSObject().removeItem(COSName.NEED_APPEARANCES);
            // note that if you've set MDP permissions, the removal of this
            // item
            // may result in Adobe Reader claiming that the document has
            // been changed.
            // and/or that field content won't be displayed properly.
            // ==> decide what you prefer and adjust your code accordingly.
        }
    }

    // default filter
    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);

    // subfilter for basic and PAdES Part 2 signatures
    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);

    PDVisibleSignDesigner visibleSignDesigner = new PDVisibleSignDesigner(signatureInputStream);

    PDVisibleSigProperties visibleSigProps = new PDVisibleSigProperties();
    visibleSigProps
            //        .signerName(name)  // TODO add..
            //        .signerLocation(location) // TODO add.
            //        .signatureReason(reason)
            //        .preferredSize(preferredSize)
            //        .page(0) // TODO fix
            .visualSignEnabled(true).setPdVisibleSignature(visibleSignDesigner);

    visibleSigProps.buildSignature();

    signature.setName(visibleSigProps.getSignerName());
    signature.setLocation(visibleSigProps.getSignerLocation());
    signature.setReason(visibleSigProps.getSignatureReason());

    // the signing date, needed for valid signature
    signature.setSignDate(Calendar.getInstance());

    SignatureOptions sigOptions = new SignatureOptions();
    sigOptions.setVisualSignature(visibleSigProps.getVisibleSignature());
    sigOptions.setPage(visibleSigProps.getPage() - 1);
    doc.addSignature(signature, this, sigOptions);

    return sigOptions;
}

From source file:org.pdfsam.pdf.DefaultPDFBoxLoader.java

License:Open Source License

public void accept(PDDocument document, PdfDocumentDescriptor descriptor) {
    descriptor.pages(document.getNumberOfPages());
    descriptor.setVersion(getVersion(Float.toString(document.getVersion())));
    PDDocumentInformation info = document.getDocumentInformation();
    descriptor.putInformation(PdfMetadataKey.TITLE.getKey(), info.getTitle());
    descriptor.putInformation(PdfMetadataKey.AUTHOR.getKey(), info.getAuthor());
    descriptor.putInformation(PdfMetadataKey.CREATOR.getKey(), info.getCreator());
    descriptor.putInformation(PdfMetadataKey.SUBJECT.getKey(), info.getSubject());
    descriptor.putInformation(PdfMetadataKey.KEYWORDS.getKey(), info.getKeywords());
    descriptor.putInformation("Producer", info.getProducer());
    Optional.ofNullable(info.getCreationDate()).map(FORMATTER::format)
            .ifPresent(c -> descriptor.putInformation("FormattedCreationDate", c));
}