pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gbaill...@apache.org
Subject svn commit: r1468195 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/pdfa/ pdfbox/src/main/java/org/apache/pdfbox/cos/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/
Date Mon, 15 Apr 2013 19:18:53 GMT
Author: gbailleul
Date: Mon Apr 15 19:18:53 2013
New Revision: 1468195

URL: http://svn.apache.org/r1468195
Log:
PDFBOX-1564: Extending COSName to produce PDF/A with correct OutputIntents
https://issues.apache.org/jira/browse/PDFBOX-1564

Creation of the PDOutputIntent class and modification of related classes
New common COSName added
Simple example to create a PDF/A added to pdfbox-example

Added:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDOutputIntent.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java?rev=1468195&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdfa/CreatePDFA.java Mon
Apr 15 19:18:53 2013
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.examples.pdfa;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import org.apache.jempbox.xmp.XMPMetadata;
+import org.apache.jempbox.xmp.pdfa.XMPSchemaPDFAId;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDMetadata;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
+import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
+
+
+/**
+ * This is an example that creates a simple PDF/A document.
+ *
+ */
+public class CreatePDFA
+{
+    /**
+     * Constructor.
+     */
+    public CreatePDFA()
+    {
+        super();
+    }
+
+    /**
+     * Create a simple PDF/A document.
+     * 
+     * This example is based on HelloWorld example.
+     * 
+     * As it is a simple case, to conform the PDF/A norm, are added :
+     * - the font used in the document
+     * - a light xmp block with only PDF identification schema (the only mandatory)
+     * - an output intent
+     *
+     * @param file The file to write the PDF to.
+     * @param message The message to write in the file.
+     *
+     * @throws Exception If something bad occurs
+     */
+    public void doIt( String file, String message) throws Exception
+    {
+        // the document
+        PDDocument doc = null;
+        try
+        {
+            doc = new PDDocument();
+
+            PDPage page = new PDPage();
+            doc.addPage( page );
+
+            // load the font from pdfbox.jar
+            InputStream fontStream = CreatePDFA.class.getResourceAsStream("/org/apache/pdfbox/resources/ttf/ArialMT.ttf");
+            PDFont font = PDTrueTypeFont.loadTTF(doc, fontStream);
+            
+            // create a page with the message where needed
+            PDPageContentStream contentStream = new PDPageContentStream(doc, page);
+            contentStream.beginText();
+            contentStream.setFont( font, 12 );
+            contentStream.moveTextPositionByAmount( 100, 700 );
+            contentStream.drawString( message );
+            contentStream.endText();
+            contentStream.saveGraphicsState();
+            contentStream.close();
+            
+            PDDocumentCatalog cat = doc.getDocumentCatalog();
+            PDMetadata metadata = new PDMetadata(doc);
+            cat.setMetadata(metadata);
+
+            // jempbox version
+            XMPMetadata xmp = new XMPMetadata();
+            XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
+            xmp.addSchema(pdfaid);
+            pdfaid.setConformance("B");
+            pdfaid.setPart(1);
+            pdfaid.setAbout("");
+            metadata.importXMPMetadata(xmp);
+    
+            
+            // retrieve icc
+            // this file cannot be added in PDFBox, it must be downloaded
+            // its localization is http://www.color.org/sRGB_IEC61966-2-1_black_scaled.icc
+            // UNIX command to retrieve :
+            // wget -O target/sRGB_IEC61966-2-1_black_scaled.icc http://www.color.org/sRGB_IEC61966-2-1_black_scaled.icc
+            InputStream colorProfile = new FileInputStream("target/sRGB_IEC61966-2-1_black_scaled.icc");
+            // create output intent
+            PDOutputIntent oi = new PDOutputIntent(doc, colorProfile); 
+            oi.setInfo("sRGB IEC61966-2.1"); 
+            oi.setOutputCondition("sRGB IEC61966-2.1"); 
+            oi.setOutputConditionIdentifier("sRGB IEC61966-2.1"); 
+            oi.setRegistryName("http://www.color.org"); 
+            cat.addOutputIntent(oi);
+            
+            doc.save( file );
+           
+        }
+        finally
+        {
+            if( doc != null )
+            {
+                doc.close();
+            }
+        }
+    }
+
+    /**
+     * This will create a hello world PDF/A document.
+     * <br />
+     * see usage() for commandline
+     *
+     * @param args Command line arguments.
+     */
+    public static void main(String[] args)
+    {
+        CreatePDFA app = new CreatePDFA();
+        try
+        {
+            if( args.length != 2 )
+            {
+                app.usage();
+            }
+            else
+            {
+                app.doIt( args[0], args[1] );
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * This will print out a message telling how to use this example.
+     */
+    private void usage()
+    {
+        System.err.println( "usage: " + this.getClass().getName() + " <output-file>
<Message>" );
+    }
+}

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=1468195&r1=1468194&r2=1468195&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Mon Apr 15 19:18:53
2013
@@ -397,6 +397,11 @@ public final class COSName extends COSBa
     public static final COSName DESTS = new COSName( "Dests" );
 
     /**
+     * A common COSName value.
+     */
+    public static final COSName DEST_OUTPUT_PROFILE = new COSName( "DestOutputProfile" );

+    
+    /**
     * A common COSName value.
     */
     public static final COSName DEVICECMYK = new COSName( "DeviceCMYK" );
@@ -697,9 +702,12 @@ public final class COSName extends COSBa
      * A common COSName value.
      */
     public static final COSName GAMMA = new COSName( "Gamma" );
+
     /**
      * A common COSName value.
      */
+    public static final COSName GTS_PDFA1 = new COSName ("GTS_PDFA1");
+    
     public static final COSName H = new COSName( "H" );
     /**
     * A common COSName value.
@@ -1014,6 +1022,7 @@ public final class COSName extends COSBa
      * A common COSName value.
      */
     public static final COSName OS = new COSName( "OS" );
+
     /**
      * A common COSName value.
      */
@@ -1022,6 +1031,26 @@ public final class COSName extends COSBa
     /**
      * A common COSName value.
      */
+    public static final COSName OUTPUT_INTENT = new COSName( "OutputIntent" ); 
+
+    /**
+     * A common COSName value.
+     */
+    public static final COSName OUTPUT_INTENTS = new COSName( "OutputIntents" ); 
+
+    /**
+     * A common COSName value.
+     */
+    public static final COSName OUTPUT_CONDITION = new COSName("OutputCondition"); 
+    
+    /**
+     * A common COSName value.
+     */
+    public static final COSName OUTPUT_CONDITION_IDENTIFIER = new COSName("OutputConditionIdentifier");

+
+    /**
+     * A common COSName value.
+     */
     public static final COSName OPEN_ACTION = new COSName("OpenAction");
 
     /** A common COSName value. */
@@ -1160,6 +1189,12 @@ public final class COSName extends COSBa
      * A common COSName value.
      */
     public static final COSName REGISTRY = new COSName( "Registry" );
+
+    /**
+     * A common COSName value.
+     */
+    public static final COSName REGISTRY_NAME = new COSName("RegistryName");
+
     /**
     * A common COSName value.
     */

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java?rev=1468195&r1=1468194&r2=1468195&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocumentCatalog.java Mon
Apr 15 19:18:53 2013
@@ -32,6 +32,7 @@ import org.apache.pdfbox.pdmodel.common.
 import org.apache.pdfbox.pdmodel.common.PDPageLabels;
 import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo;
 import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot;
+import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
 import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
 import org.apache.pdfbox.pdmodel.interactive.action.PDDocumentCatalogAdditionalActions;
@@ -444,6 +445,56 @@ public class PDDocumentCatalog implement
     }
 
     /**
+     * Get the list of OutputIntents defined in the document.
+     * 
+     * @return The list of PDOoutputIntent
+     */
+    public List<PDOutputIntent> getOutputIntent () {
+        List<PDOutputIntent> retval = new ArrayList<PDOutputIntent>();
+        COSArray array = (COSArray)root.getItem(COSName.OUTPUT_INTENTS);
+        if (array!=null) {
+            for (COSBase cosBase : array)
+            {
+                PDOutputIntent oi = new PDOutputIntent((COSStream)cosBase);
+                retval.add(oi);
+            }
+        }
+        return retval;
+    }
+
+    /**
+     * Add an OutputIntent to the list.
+     * 
+     * If there is not OutputIntent, the list is created and the first
+     * element added.
+     * 
+     * @param outputIntent the OutputIntent to add.
+     */
+    public void addOutputIntent (PDOutputIntent outputIntent) {
+        COSArray array = (COSArray)root.getItem(COSName.OUTPUT_INTENTS);
+        if (array==null) {
+            array = new COSArray();
+            root.setItem(COSName.OUTPUT_INTENTS, array);
+        }
+        array.add(outputIntent.getCOSObject());
+    }
+
+    /**
+     * Replace the list of OutputIntents of the document.
+     * 
+     * @param outputIntents the list of OutputIntents, if the list is empty all
+     * OutputIntents are removed.
+     */
+    public void setOutputIntents (List<PDOutputIntent> outputIntents) {
+        COSArray array = new COSArray();
+        for (PDOutputIntent intent : outputIntents)
+        {
+            array.add(intent.getCOSObject());
+        }
+        root.setItem(COSName.OUTPUT_INTENTS, array);
+    }
+    
+    /**
      * Set the page display mode, see the PAGE_MODE_XXX constants.
      * @return A string representing the page mode.
      */

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDOutputIntent.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDOutputIntent.java?rev=1468195&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDOutputIntent.java
(added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDOutputIntent.java
Mon Apr 15 19:18:53 2013
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.graphics.color;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+
+public class PDOutputIntent implements COSObjectable { 
+    
+    private COSDictionary dictionary; 
+    
+    public PDOutputIntent(PDDocument doc, InputStream colorProfile) throws Exception{ 
+        dictionary = new COSDictionary(); 
+        dictionary.setItem(COSName.TYPE, COSName.OUTPUT_INTENT); 
+        dictionary.setItem(COSName.S, COSName.GTS_PDFA1); 
+        PDStream destOutputIntent = configureOutputProfile(doc, colorProfile);
+        dictionary.setItem(COSName.DEST_OUTPUT_PROFILE, destOutputIntent); 
+    } 
+
+    public PDOutputIntent (COSDictionary dictionary) {
+        this.dictionary = dictionary;
+    }
+    
+    public COSBase getCOSObject() { 
+        return dictionary; 
+    } 
+
+    public COSStream getDestOutputIntent () {
+        return (COSStream)dictionary.getItem(COSName.DEST_OUTPUT_PROFILE);
+    }
+    
+    public String getInfo () {
+        return dictionary.getString(COSName.INFO);
+    }
+    
+    public void setInfo( String value ) 
+    { 
+        dictionary.setString(COSName.INFO, value); 
+    } 
+
+    public String getOutputCondition () {
+        return dictionary.getString(COSName.OUTPUT_CONDITION);
+    }
+    
+    public void setOutputCondition( String value ) 
+    { 
+        dictionary.setString(COSName.OUTPUT_CONDITION, value); 
+    } 
+
+    public String getOutputConditionIdentifier () {
+        return dictionary.getString(COSName.OUTPUT_CONDITION_IDENTIFIER);
+    }
+    
+    public void setOutputConditionIdentifier( String value ) 
+    { 
+        dictionary.setString(COSName.OUTPUT_CONDITION_IDENTIFIER, value); 
+    } 
+
+    public String getRegistryName () {
+        return dictionary.getString(COSName.REGISTRY_NAME);
+    }
+    
+    public void setRegistryName( String value ) 
+    { 
+        dictionary.setString(COSName.REGISTRY_NAME, value); 
+    } 
+    
+    private PDStream configureOutputProfile (PDDocument doc, InputStream colorProfile) throws
IOException {
+        PDStream stream = new PDStream(doc,colorProfile, false); 
+        stream.getStream().setFilters(COSName.FLATE_DECODE); 
+        stream.getStream().setInt( COSName.LENGTH, stream.getByteArray().length ); 
+        stream.getStream().setInt(COSName.N, 3); 
+        stream.addCompression(); 
+        return stream;
+    }
+}  



Mime
View raw message