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 2010/12/02 19:05:42 UTC

svn commit: r1041509 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java

Author: lehmi
Date: Thu Dec  2 18:05:42 2010
New Revision: 1041509

URL: http://svn.apache.org/viewvc?rev=1041509&view=rev
Log:
PDFBOX-858: try to use the document information if there aren't any metadata within the catalogue

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java?rev=1041509&r1=1041508&r2=1041509&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java Thu Dec  2 18:05:42 2010
@@ -24,6 +24,7 @@ import org.apache.jempbox.xmp.XMPSchemaP
 import org.apache.pdfbox.exceptions.InvalidPasswordException;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.PDDocumentInformation;
 import org.apache.pdfbox.pdmodel.common.PDMetadata;
 
 import java.text.DateFormat;
@@ -66,7 +67,6 @@ public class ExtractMetadata
             try
             {
                 document = PDDocument.load( args[0] );
-                PDDocumentCatalog catalog = document.getDocumentCatalog();
                 if (document.isEncrypted()) 
                 {
                     try
@@ -82,33 +82,47 @@ public class ExtractMetadata
                         e.printStackTrace();
                     }
                 }
+                PDDocumentCatalog catalog = document.getDocumentCatalog();
                 PDMetadata meta = catalog.getMetadata();
-                XMPMetadata metadata = meta.exportXMPMetadata();
-
-                XMPSchemaDublinCore dc = metadata.getDublinCoreSchema();
-                if (dc != null)
+                if ( meta != null)
                 {
-                    display("Title:", dc.getTitle());
-                    display("Description:", dc.getDescription());
-                    list("Creators: ", dc.getCreators());
-                    list("Dates:", dc.getDates());
-                }
-
-                XMPSchemaPDF pdf = metadata.getPDFSchema();
-                if (pdf != null)
-                {
-                    display("Keywords:", pdf.getKeywords());
-                    display("PDF Version:", pdf.getPDFVersion());
-                    display("PDF Producer:", pdf.getProducer());
+                    XMPMetadata metadata = meta.exportXMPMetadata();
+    
+                    XMPSchemaDublinCore dc = metadata.getDublinCoreSchema();
+                    if (dc != null)
+                    {
+                        display("Title:", dc.getTitle());
+                        display("Description:", dc.getDescription());
+                        list("Creators: ", dc.getCreators());
+                        list("Dates:", dc.getDates());
+                    }
+    
+                    XMPSchemaPDF pdf = metadata.getPDFSchema();
+                    if (pdf != null)
+                    {
+                        display("Keywords:", pdf.getKeywords());
+                        display("PDF Version:", pdf.getPDFVersion());
+                        display("PDF Producer:", pdf.getProducer());
+                    }
+    
+                    XMPSchemaBasic basic = metadata.getBasicSchema();
+                    if (basic != null)
+                    {
+                        display("Create Date:", basic.getCreateDate());
+                        display("Modify Date:", basic.getModifyDate());
+                        display("Creator Tool:", basic.getCreatorTool());
+                    }
                 }
-
-                XMPSchemaBasic basic = metadata.getBasicSchema();
-                if (basic != null)
+                else
                 {
-                    display("Create Date:", basic.getCreateDate());
-                    display("Modify Date:", basic.getModifyDate());
-                    display("Creator Tool:", basic.getCreatorTool());
+                    // The pdf doesn't contain any metadata, try to use the document information instead
+                    PDDocumentInformation information = document.getDocumentInformation();
+                    if ( information != null)
+                    {
+                        showDocumentInformation(information);
+                    }
                 }
+                
             }
             finally
             {
@@ -120,6 +134,15 @@ public class ExtractMetadata
         }
     }
 
+    private static void showDocumentInformation(PDDocumentInformation information)
+    {
+        display("Title:", information.getTitle());
+        display("Subject:", information.getSubject());
+        display("Author:", information.getAuthor());
+        display("Creator:", information.getCreator());
+        display("Producer:", information.getProducer());
+    }
+    
     private static void list(String title, List list)
     {
         if (list == null)