Example usage for org.apache.pdfbox.pdmodel.encryption AccessPermission isOwnerPermission

List of usage examples for org.apache.pdfbox.pdmodel.encryption AccessPermission isOwnerPermission

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.encryption AccessPermission isOwnerPermission.

Prototype

public boolean isOwnerPermission() 

Source Link

Document

This will tell if the access permission corresponds to owner access permission (no restriction).

Usage

From source file:com.ackpdfbox.app.Decrypt.java

License:Apache License

private void decrypt() throws IOException {
    PDDocument document = null;/*from  w  w w .  j  a v  a2  s  .com*/
    try {
        InputStream keyStoreStream = null;
        if (keyStore != null) {
            keyStoreStream = new FileInputStream(keyStore);
        }

        document = PDDocument.load(new File(infile), password, keyStoreStream, alias);

        if (document.isEncrypted()) {
            AccessPermission ap = document.getCurrentAccessPermission();
            if (ap.isOwnerPermission()) {
                document.setAllSecurityToBeRemoved(true);
                document.save(outfile);
            } else {
                throw new IOException(
                        "Error: You are only allowed to decrypt a document with the owner password.");
            }
        } else {
            System.err.println("Error: Document is not encrypted.");
        }
    } finally {
        if (document != null) {
            document.close();
        }
    }
}

From source file:com.exlibris.dps.repository.plugin.riskExtractor.drmlint.PDFBoxWrapper.java

License:Apache License

/**
 * Check for encryption with Apache PDFBox
 * -> query the encryption dictionary (might allow more granular checks of protection)
 * @param pPDF pdf file to check/*from  ww w .ja va2  s .c o m*/
 * @return whether or not the file has DRM
 */
public static boolean hasDRMGranular(File pPDF) {

    boolean ret = false;

    try {
        PDFParser parser = new PDFParser(new FileInputStream(pPDF));
        parser.parse();

        COSDictionary dict = parser.getDocument().getEncryptionDictionary();
        if (dict != null) {

            //print encryption dictionary
            //            for(COSName key:dict.keySet()) {
            //               System.out.print(key.getName());
            //               String value = dict.getString(key);
            //               if(value!=null){
            //                  System.out.println(": "+value);
            //               } else {
            //                  System.out.println(": "+dict.getLong(key));
            //               }
            //            }

            //this feaure in pdfbox is currently broken, see: https://issues.apache.org/jira/browse/PDFBOX-1651
            //AccessPermission perms = parser.getPDDocument().getCurrentAccessPermission();
            //this is a work around; creating a new object from the data
            AccessPermission perms = new AccessPermission(dict.getInt("P"));

            boolean debug = false;

            if (debug) {

                System.out.println("canAssembleDocument()        : " + perms.canAssembleDocument());
                System.out.println("canExtractContent()          : " + perms.canExtractContent());
                System.out.println("canExtractForAccessibility() : " + perms.canExtractForAccessibility());
                System.out.println("canFillInForm()              : " + perms.canFillInForm());
                System.out.println("canModify()                  : " + perms.canModify());
                System.out.println("canModifyAnnotations()       : " + perms.canModifyAnnotations());
                System.out.println("canPrint()                   : " + perms.canPrint());
                System.out.println("canPrintDegraded()           : " + perms.canPrintDegraded());
                System.out.println("isOwnerPermission()          : " + perms.isOwnerPermission());
                System.out.println("isReadOnly()                 : " + perms.isReadOnly());

            }
        }

        parser.getDocument().close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return ret;
}

From source file:org.nuxeo.pdf.test.PDFEncryptionTest.java

License:Open Source License

protected void checkIsReadOnly(Blob inBlob, String ownerPwd, String userPwd) throws Exception {

    assertNotNull(inBlob);/*from   w  ww .  j a v  a2  s . c o m*/

    PDDocument pdfDoc = utils.loadAndTrack(inBlob);
    assertTrue(pdfDoc.isEncrypted());

    // Decrypt as user
    pdfDoc.openProtection(new StandardDecryptionMaterial(userPwd));
    assertFalse(pdfDoc.isEncrypted());
    AccessPermission ap = pdfDoc.getCurrentAccessPermission();
    assertTrue(ap.canExtractContent());
    assertTrue(ap.canExtractForAccessibility());
    assertTrue(ap.canPrint());
    assertTrue(ap.canPrintDegraded());

    assertFalse(ap.canAssembleDocument());
    assertFalse(ap.canFillInForm());
    assertFalse(ap.canModifyAnnotations());

    // Decrypt as owner
    utils.closeAndUntrack(pdfDoc);
    pdfDoc = utils.loadAndTrack(inBlob);
    pdfDoc.openProtection(new StandardDecryptionMaterial(ownerPwd));
    assertFalse(pdfDoc.isEncrypted());
    ap = pdfDoc.getCurrentAccessPermission();
    assertTrue(ap.isOwnerPermission());

    utils.closeAndUntrack(pdfDoc);

}

From source file:uk.bl.dpt.qa.flint.wrappers.PDFBoxWrapper.java

License:Apache License

/**
 * Check for encryption with Apache PDFBox
 * -> query the encryption dictionary (might allow more granular checks of protection)
 * @param pPDF pdf file to check/*  www  . j a v a 2 s .  c om*/
 * @return whether or not the file has DRM
 */
public boolean hasDRMGranular(File pPDF) {

    boolean ret = false;

    File tmp = null;
    try {
        System.setProperty("org.apache.pdfbox.baseParser.pushBackSize", "1024768");
        // NOTE: we use loadNonSeq here as it is the latest parser
        // load() and parser.parse() have hung on test files
        tmp = File.createTempFile("flint-", ".tmp");
        tmp.deleteOnExit();
        RandomAccess scratchFile = new RandomAccessFile(tmp, "rw");
        PDDocument doc = PDDocument.loadNonSeq(new FileInputStream(pPDF), scratchFile);

        PDEncryptionDictionary dict = doc.getEncryptionDictionary();
        if (dict != null) {

            //print encryption dictionary
            //            for(COSName key:dict.keySet()) {
            //               System.out.print(key.getName());
            //               String value = dict.getString(key);
            //               if(value!=null){
            //                  System.out.println(": "+value);
            //               } else {
            //                  System.out.println(": "+dict.getLong(key));
            //               }
            //            }

            //this feaure in pdfbox is currently broken, see: https://issues.apache.org/jira/browse/PDFBOX-1651
            //AccessPermission perms = parser.getPDDocument().getCurrentAccessPermission();
            //this is a work around; creating a new object from the data
            AccessPermission perms = new AccessPermission(dict.getPermissions());//.getInt("P"));

            boolean debug = true;

            if (debug) {

                System.out.println("canAssembleDocument()        : " + perms.canAssembleDocument());
                System.out.println("canExtractContent()          : " + perms.canExtractContent());
                System.out.println("canExtractForAccessibility() : " + perms.canExtractForAccessibility());
                System.out.println("canFillInForm()              : " + perms.canFillInForm());
                System.out.println("canModify()                  : " + perms.canModify());
                System.out.println("canModifyAnnotations()       : " + perms.canModifyAnnotations());
                System.out.println("canPrint()                   : " + perms.canPrint());
                System.out.println("canPrintDegraded()           : " + perms.canPrintDegraded());
                System.out.println("isOwnerPermission()          : " + perms.isOwnerPermission());
                System.out.println("isReadOnly()                 : " + perms.isReadOnly());

            }
        }

        doc.close();

    } catch (Exception e) {
        LOGGER.warn("Exception while doing granular DRM checks leads to invalidity: {}", e);
    } finally {
        if (tmp != null)
            tmp.delete();
    }

    return ret;
}