pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r908619 - in /pdfbox/trunk/src/main/java/org/apache/pdfbox: pdfwriter/COSWriter.java pdmodel/PDDocument.java
Date Wed, 10 Feb 2010 18:15:43 GMT
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;
+    }
+
 }



Mime
View raw message