Java Code Examples for org.apache.pdfbox.pdmodel.PDDocument#isEncrypted()

The following examples show how to use org.apache.pdfbox.pdmodel.PDDocument#isEncrypted() . 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: PdfEncryptor.java    From website with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void encrypt(String filename, String outputFilename) throws Exception {
	AccessPermission accessPermission = new AccessPermission();
	accessPermission.setCanAssembleDocument(true);
	accessPermission.setCanExtractContent(false);
	accessPermission.setCanExtractForAccessibility(false);
	accessPermission.setCanFillInForm(false);
	accessPermission.setCanModify(true);
	accessPermission.setCanModifyAnnotations(false);
	PDDocument document = PDDocument.load( filename );
	if( !document.isEncrypted() ) {
		StandardProtectionPolicy standardProtectionPolicy = new StandardProtectionPolicy(generatedOwnerPassword(), "", accessPermission);
		standardProtectionPolicy.setEncryptionKeyLength(40);
           document.protect(standardProtectionPolicy);
	}
	document.save(outputFilename);
}
 
Example 2
Source File: PdfBoxSignatureService.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void checkEncryptedAndSaveIncrementally(PDDocument pdDocument, OutputStream outputStream, PAdESCommonParameters parameters) {
	try {
		if (pdDocument.isEncrypted()) {
			SecureRandom secureRandom = getSecureRandomProvider(parameters).getSecureRandom();
			pdDocument.getEncryption().getSecurityHandler().setCustomSecureRandom(secureRandom);
		}
		saveDocumentIncrementally(pdDocument, outputStream);
	} catch (Exception e) {
		throw new DSSException(e);
	}
}
 
Example 3
Source File: PdfDocument.java    From olat with Apache License 2.0 5 votes vote down vote up
private String extractText(final VFSLeaf leaf) throws IOException, DocumentAccessException {
    if (log.isDebugEnabled()) {
        log.debug("readContent from pdf starts...");
    }
    PDDocument document = null;
    InputStream is = null;
    try {
        is = leaf.getInputStream();
        document = PDDocument.load(is);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (final Exception e) {
                throw new DocumentAccessException("PDF is encrypted. Can not read content file=" + leaf.getName());
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("readContent PDDocument loaded");
        }
        final PDFTextStripper stripper = new PDFTextStripper();
        return stripper.getText(document);
    } finally {
        if (document != null) {
            document.close();
        }
        if (is != null) {
            is.close();
        }
        // needed to prevent potential OutOfMemoryError
        // https://issues.apache.org/jira/browse/PDFBOX-1009
        PDFont.clearResources();
    }
}
 
Example 4
Source File: PdfPanel.java    From swift-explorer with Apache License 2.0 4 votes vote down vote up
public synchronized void setPdf(PDDocument pdf) 
  {
  	listImagePages.clear();
      if (pdf == null)
      	return ;
try 
{
	if (pdf.isEncrypted())
	{
		logger.info("Failed attempt at previewing an encrypted PDF");
		return ;
	}
	PDDocumentCatalog cat = pdf.getDocumentCatalog() ;
	@SuppressWarnings("unchecked")
	List<PDPage> pages = cat.getAllPages() ;
       if (pages != null && !pages.isEmpty()) 
       {
       	for (PDPage page : pages)
       	{
       		listImagePages.add(page.convertToImage()) ;
       		if (listImagePages.size() >= maxPageToPreview)
       			break ;
       	}
       } 
} 
catch (IOException e) 
{
	logger.error("Error occurred while opening the pdf document", e);
}
finally 
{
	if (pdf != null)
	{
		try 
		{
			pdf.close();
		} 
		catch (IOException ex) 
		{
			logger.error("Error occurred while closing the pdf document", ex);
		}
	}
}
      repaint();
  }