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

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

Introduction

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

Prototype

public void setAllSecurityToBeRemoved(boolean removeAllSecurity) 

Source Link

Document

Activates/Deactivates the removal of all security when writing the pdf.

Usage

From source file:merge_split.MergeSplit.java

License:Apache License

private void ConvertFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConvertFileButtonActionPerformed
    String fileName;//from w w w.j a  v a  2 s.c  om
    int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        fileName = file.toString();
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            if (doc.isEncrypted()) {

                doc.setAllSecurityToBeRemoved(true);

            }
        } catch (IOException ex) {

        }
        convertcode = "";
        if (doc == null) {
            JFrame frame = new JFrame("Input Dialog Example 3");

            convertcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted",
                    JOptionPane.WARNING_MESSAGE);
            try {
                doc = PDDocument.load(file, rotatecode);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
        if (doc != null) {
            int count = doc.getNumberOfPages();

            String currentpages;
            if (count > 1) {
                currentpages = "1 - " + count;
            } else {
                currentpages = "1";
            }
            ConvertPagesField.setText(currentpages);
            ConvertFileField.setText(fileName);
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0) {
                name = name.substring(0, pos);
            }
            ConvertNameField.setText(name);
            try {
                doc.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem accessing file.", "Problem accessing file",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void SplitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitButtonActionPerformed
    try {//from  ww w.ja v a  2s .c  om

        File file = new File(SplitFileField.getText());
        PDDocument doc1;
        if (splitcode.equals("ok")) {
            doc1 = PDDocument.load(file);
        } else {
            doc1 = PDDocument.load(file, splitcode);

        }
        doc1.setAllSecurityToBeRemoved(true);

        if (MultipleButton.isSelected()) {
            PDDocument pdf1 = new PDDocument();
            PDDocument pdf2 = new PDDocument();
            TreeSet tree = findPages(SplitPagesField.getText());
            for (int j = 0; j < doc1.getNumberOfPages(); j++) {
                PDPage page = doc1.getPage(j);
                if (tree.contains(j + 1)) {
                    pdf1.addPage(page);
                } else {
                    pdf2.addPage(page);

                }
            }
            String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "1.pdf";
            String destination2 = SplitDestinationField.getText() + "\\" + SplitNameField.getText() + "2.pdf";

            PDDocumentInformation info = pdf1.getDocumentInformation();
            info.setAuthor(SplitAuthorField.getText());
            PDDocumentInformation info2 = pdf2.getDocumentInformation();
            info2.setAuthor(SplitAuthorField.getText());
            if (pdf1.getNumberOfPages() > 0) {
                File output1 = new File(destination1);
                pdf1.save(output1);
            }
            if (pdf2.getNumberOfPages() > 0) {
                File output2 = new File(destination2);
                pdf2.save(output2);
            }
            pdf1.close();
            pdf2.close();
        } else if (SingleButton.isSelected()) {

            for (int j = 0; j < doc1.getNumberOfPages(); j++) {
                PDDocument pdf1 = new PDDocument();

                PDPage page = doc1.getPage(j);
                pdf1.addPage(page);
                int pagenumber = j + 1;
                String destination1 = SplitDestinationField.getText() + "\\" + SplitNameField.getText()
                        + pagenumber + ".pdf";

                PDDocumentInformation info = pdf1.getDocumentInformation();
                info.setAuthor(SplitAuthorField.getText());

                if (pdf1.getNumberOfPages() > 0) {
                    File output1 = new File(destination1);
                    pdf1.save(output1);
                }

                pdf1.close();
            }

        }
        doc1.close();

    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Your input is incorrect. Please fill all the fields.",
                "Input warning", JOptionPane.WARNING_MESSAGE);
        java.util.logging.Logger.getLogger(MergeSplit.class.getName()).log(java.util.logging.Level.SEVERE, null,
                ex);

    }
}

From source file:merge_split.MergeSplit.java

License:Apache License

private void SplitFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitFileButtonActionPerformed
    String fileName;/*from w w  w.  jav a 2 s.  c  o  m*/
    int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = jFileChooser1.getSelectedFile();
        fileName = file.toString();
        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            if (doc.isEncrypted()) {

                doc.setAllSecurityToBeRemoved(true);

            }
        } catch (IOException ex) {

        }
        splitcode = "";
        if (doc == null) {
            JFrame frame = new JFrame("Input Dialog Example 3");

            splitcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted",
                    JOptionPane.WARNING_MESSAGE);
            try {
                doc = PDDocument.load(file, rotatecode);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password",
                        JOptionPane.WARNING_MESSAGE);

            }

        }

        if (doc != null) {
            int count = doc.getNumberOfPages();

            String currentpages;
            if (count > 1) {
                currentpages = "1 - " + count;
            } else {
                currentpages = "1";
            }
            SplitPagesField.setText(currentpages);
            SplitFileField.setText(fileName);
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0) {
                name = name.substring(0, pos);
            }
            name = name + "Split";
            SplitNameField.setText(name);
            try {
                doc.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Problem finishing process.", "Problem finishing process",
                        JOptionPane.WARNING_MESSAGE);
            }

        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

From source file:org.apache.camel.component.pdf.PdfProducer.java

License:Apache License

private Object doAppend(Exchange exchange) throws IOException, BadSecurityHandlerException,
        CryptographyException, InvalidPasswordException, COSVisitorException {
    LOG.debug("Got {} operation, going to append text to provided pdf.", pdfConfiguration.getOperation());
    String body = exchange.getIn().getBody(String.class);
    PDDocument document = exchange.getIn().getHeader(PDF_DOCUMENT_HEADER_NAME, PDDocument.class);
    if (document == null) {
        throw new IllegalArgumentException(
                String.format("%s header is expected for append operation", PDF_DOCUMENT_HEADER_NAME));
    }/*from  w ww.  j av  a2s .  c  o  m*/

    if (document.isEncrypted()) {
        DecryptionMaterial decryptionMaterial = exchange.getIn().getHeader(DECRYPTION_MATERIAL_HEADER_NAME,
                DecryptionMaterial.class);
        if (decryptionMaterial == null) {
            throw new IllegalArgumentException(
                    String.format("%s header is expected for %s operation " + "on encrypted document",
                            DECRYPTION_MATERIAL_HEADER_NAME, pdfConfiguration.getOperation()));
        }

        document.openProtection(decryptionMaterial);
        document.setAllSecurityToBeRemoved(true);
    }

    ProtectionPolicy protectionPolicy = exchange.getIn().getHeader(PROTECTION_POLICY_HEADER_NAME,
            ProtectionPolicy.class);

    appendToPdfDocument(body, document, protectionPolicy);
    OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    document.save(byteArrayOutputStream);
    return byteArrayOutputStream;
}

From source file:org.apache.tika.parser.pdf.EnhancedPDFParser.java

License:Apache License

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {

    PDDocument pdfDocument = null;
    //config from context, or default if not set via context
    PDFParserConfig localConfig = context.get(PDFParserConfig.class, defaultConfig);
    String password = "";
    try {/*from www.jav a 2  s .com*/
        // PDFBox can process entirely in memory, or can use a temp file
        //  for unpacked / processed resources
        // Decide which to do based on if we're reading from a file or not already
        TikaInputStream tstream = TikaInputStream.cast(stream);
        password = getPassword(metadata, context);
        if (tstream != null && tstream.hasFile()) {
            // File based, take that as a cue to use a temporary file
            if (localConfig.getUseNonSequentialParser() == true) {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), password);
            } else {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), true);
            }
        } else {
            // Go for the normal, stream based in-memory parsing
            if (localConfig.getUseNonSequentialParser() == true) {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), password);
            } else {
                pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), true);
            }
        }
        metadata.set("pdf:encrypted", Boolean.toString(pdfDocument.isEncrypted()));

        pdfDocument.setAllSecurityToBeRemoved(true);

        metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
        extractMetadata(pdfDocument, metadata);
        if (handler != null) {
            String xfaXml = extractXFAText(pdfDocument);
            if (xfaXml != null) {
                try (BufferedInputStream is = new BufferedInputStream(
                        new ByteArrayInputStream(xfaXml.getBytes()))) {
                    new TXTParser().parse(is, handler, metadata, context);
                }
                metadata.set(Metadata.CONTENT_TYPE, "application/pdf");
            } else {
                EnhancedPDF2XHTML.process(pdfDocument, handler, context, metadata, localConfig);
            }
        }
    } finally {
        if (pdfDocument != null) {
            pdfDocument.close();
        }
    }
}

From source file:org.lockss.pdf.pdfbox.PdfBoxDocumentFactory.java

License:Open Source License

/**
 * <p>/*  w ww .  j  av  a  2  s.  c o  m*/
 * Override this method to alter the processing of the {@link PDDocument}
 * instance after it has been parsed by {@link PDFParser#parse()}.
 * </p>
 * 
 * @param pdDocument
 *          A freshly parsed {@link PDDocument} instance
 * @throws CryptographyException
 *           if a cryptography exception is thrown
 * @throws IOException
 *           if an I/O exception is thrown
 * @since 1.67
 */
protected void processAfterParse(PDDocument pdDocument) throws CryptographyException, IOException {
    pdDocument.setAllSecurityToBeRemoved(true);
    if (pdDocument.isEncrypted()) {
        pdDocument.decrypt("");
    }
}