You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2015/02/13 18:22:34 UTC

svn commit: r1659617 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: cos/COSDocument.java pdmodel/PDDocument.java

Author: lehmi
Date: Fri Feb 13 17:22:34 2015
New Revision: 1659617

URL: http://svn.apache.org/r1659617
Log:
PDFBOX-2099: added a getter/setter to PDDocument for the overall version of the pdf 

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1659617&r1=1659616&r2=1659617&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Fri Feb 13 17:22:34 2015
@@ -270,12 +270,27 @@ public class COSDocument extends COSBase
     }
 
     /**
-     * This will get the version of this PDF document.
+     * This will get the version extracted from the header of this PDF document.
      *
-     * @return This documents version.
+     * @return The header version.
      */
     public float getVersion()
     {
+        if (version < 0)
+        {
+            try
+            {
+                String[] headerParts = headerString.split("-");
+                if (headerParts.length == 2)
+                {
+                    version = Float. parseFloat(headerParts[1]);
+                }
+            }
+            catch ( NumberFormatException e )
+            {
+                version = -1;
+            }
+        }
         return version;
     }
 
@@ -574,6 +589,8 @@ public class COSDocument extends COSBase
     public void setHeaderString(String header)
     {
         headerString = header;
+        // reset version
+        version = -1;
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1659617&r1=1659616&r2=1659617&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Fri Feb 13 17:22:34 2015
@@ -27,6 +27,9 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
@@ -38,6 +41,7 @@ import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
 import org.apache.pdfbox.pdfparser.BaseParser;
+import org.apache.pdfbox.pdfparser.COSParser;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdfwriter.COSWriter;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
@@ -67,6 +71,8 @@ import org.apache.pdfbox.pdmodel.interac
  */
 public class PDDocument implements Closeable
 {
+    private static final Log LOG = LogFactory.getLog(PDDocument.class);
+
     private final COSDocument document;
 
     // cached values
@@ -1104,4 +1110,73 @@ public class PDDocument implements Close
     {
         documentId = docId;
     }
+    
+    /**
+     * Returns the PDF specification version this document conforms to.
+     *
+     * @return the PDF version (e.g. 1.4f)
+     */
+    public float getVersion()
+    {
+        String catalogVersion = getDocumentCatalog().getVersion();
+        float catalogVersionFloat = -1;
+        float headerVersionFloat = getDocument().getVersion();
+        if (catalogVersion != null)
+        {
+            try
+            {
+                catalogVersionFloat = Float.parseFloat(catalogVersion);
+            }
+            catch(NumberFormatException exception)
+            {
+                LOG.error("Can't extract the version number of the document catalog.", exception);
+            }
+        }
+        // there may be a second version information in the document catalog starting with 1.4
+        if (catalogVersionFloat >= 1.4f)
+        {
+            // the most recent version is the correct one
+            return Math.max(catalogVersionFloat, headerVersionFloat);
+        }
+        else
+        {
+            return headerVersionFloat;
+        }
+    }
+
+    /**
+     * Sets the PDF specification version for this document.
+     *
+     * @param newVersion the new PDF version (e.g. 1.4f)
+     * 
+     */
+    public void setVersion(float newVersion)
+    {
+        float currentVersion = getVersion();
+        // nothing to do?
+        if (newVersion == currentVersion)
+        {
+            return;
+        }
+        // the version can't be downgraded
+        if (newVersion < currentVersion)
+        {
+            LOG.error("It's not allowed to downgrade the version of a pdf.");
+            return;
+        }
+        // update the catalog version if the new version is >= 1.4
+        if (newVersion >= 1.4f)
+        {
+            getDocumentCatalog().setVersion(Float.toString(newVersion));
+            if (getDocument().getVersion() > newVersion)
+            {
+                getDocument().setVersion(newVersion);
+            }
+        }
+        else
+        {
+            // versions < 1.4f have a version header only
+            getDocument().setVersion(newVersion);
+        }
+    }
 }