Author: jukka
Date: Wed Feb 10 18:15:31 2010
New Revision: 908619
URL: http://svn.apache.org/viewvc?rev=908619&view=rev
Log:
PDFBOX-526: Add ability to read encrypted file & write unencrypted files
Patch by Adam Nichols
Modified:
pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=908619&r1=908618&r2=908619&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Wed
Feb 10 18:15:31 2010
@@ -993,27 +993,40 @@
public void write(PDDocument doc) throws COSVisitorException
{
document = doc;
-
- SecurityHandler securityHandler = document.getSecurityHandler();
- if(securityHandler != null)
+
+ // if the document says we should remove encryption, then we shouldn't
encrypt
+ if(doc.isAllSecurityToBeRemoved())
{
- try
- {
- securityHandler.prepareDocumentForEncryption(document);
- this.willEncrypt = true;
- }
- catch(IOException e)
- {
- throw new COSVisitorException( e );
- }
- catch(CryptographyException e)
- {
- throw new COSVisitorException( e );
- }
+ this.willEncrypt = false;
+ // also need to get rid of the "Encrypt" in the trailer so readers
+ // don't try to decrypt a document which is not encrypted
+ COSDocument cosDoc = doc.getDocument();
+ COSDictionary trailer = cosDoc.getTrailer();
+ trailer.removeItem(COSName.getPDFName("Encrypt"));
}
else
{
- this.willEncrypt = false;
+ SecurityHandler securityHandler = document.getSecurityHandler();
+ if(securityHandler != null)
+ {
+ try
+ {
+ securityHandler.prepareDocumentForEncryption(document);
+ this.willEncrypt = true;
+ }
+ catch(IOException e)
+ {
+ throw new COSVisitorException( e );
+ }
+ catch(CryptographyException e)
+ {
+ throw new COSVisitorException( e );
+ }
+ }
+ else
+ {
+ this.willEncrypt = false;
+ }
}
COSDocument cosDoc = document.getDocument();
Modified: pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=908619&r1=908618&r2=908619&view=diff
==============================================================================
--- pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
(original)
+++ pdfbox/trunk/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Wed
Feb 10 18:15:31 2010
@@ -109,6 +109,12 @@
* which you have an object id for that matter).
*/
private Map pageMap = null;
+
+ /**
+ * This will hold a flag which tells us if we should remove all security
+ * from this documents
+ */
+ private boolean allSecurityToBeRemoved = false;
/**
* Constructor, creates a new PDF Document with no pages. You need to add
@@ -1129,4 +1135,13 @@
{
return securityHandler;
}
+
+ public boolean isAllSecurityToBeRemoved() {
+ return allSecurityToBeRemoved;
+ }
+
+ public void setAllSecurityToBeRemoved(boolean allSecurityToBeRemoved) {
+ this.allSecurityToBeRemoved = allSecurityToBeRemoved;
+ }
+
}