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

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

Introduction

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

Prototype

public AccessPermission getCurrentAccessPermission() 

Source Link

Document

Returns the access permissions granted when the document was decrypted.

Usage

From source file:org.tnc.doctrack.behaviours.docTrackBehaviours.java

License:Open Source License

private Result[] extractQRfromPDF(InputStream PDF) throws Exception {
    System.out.println("TNC - DocTrack  - extractQRfromPDF starting....");
    //Initialize variable for QR decoding.

    PDDocument document = null;
    String password = "";
    String prefix = null;/* ww  w.  jav a 2  s  . c o m*/
    boolean addKey = false;
    Result[] QR = null;
    try {
        //read PDF document 
        document = PDDocument.loadNonSeq(PDF, null, password);
        //Check permission to PDF
        AccessPermission ap = document.getCurrentAccessPermission();
        if (!ap.canExtractContent()) {
            System.out.println(
                    "TNC - DocTrack  Error - extractQRfromPDF - You do not have permission to extract images from PDF.");
            throw new IOException(
                    "TNC - DocTrack  Error - extractQRfromPDF - You do not have permission to extract images from PDF.");
        }
        //Iterate throw the PDF pages. 
        List<?> pages = document.getDocumentCatalog().getAllPages();
        Iterator<?> iter = pages.iterator();
        while (iter.hasNext()) {
            PDPage page = (PDPage) iter.next();
            PDResources resources = page.getResources();
            // extract all XObjectImages which are part of the page resources
            System.out.println("TNC - DocTrack  - extractQRfromPDF - Try to process image and find QR code");
            QR = processResources(resources, prefix, addKey);
        }

    } finally {
        if ((document != null)) {
            try {
                document.close();
            } catch (Exception e) {

            }

        }
    }
    System.out.println("TNC - DocTrack  - extractQRfromPDF finished. QR code string : " + QR);
    return QR;
}