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 2014/08/30 17:26:28 UTC

svn commit: r1621471 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java

Author: lehmi
Date: Sat Aug 30 15:26:28 2014
New Revision: 1621471

URL: http://svn.apache.org/r1621471
Log:
PDFBOX-2291: use PDXObjectForm to create the overlay xobject 

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java?rev=1621471&r1=1621470&r2=1621471&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java Sat Aug 30 15:26:28 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.pdfbox.util;
 
+import java.awt.geom.AffineTransform;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -28,7 +29,6 @@ import java.util.Map;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
-import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSObject;
 import org.apache.pdfbox.cos.COSStream;
@@ -39,6 +39,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
 
 /**
  * Adds an overlay to an existing PDF document.
@@ -49,7 +50,7 @@ import org.apache.pdfbox.pdmodel.common.
 public class Overlay
 {
     /**
-     * Possible loacation of the overlayed pages: foreground or background.
+     * Possible location of the overlayed pages: foreground or background.
      */
     public enum Position
     {
@@ -84,8 +85,8 @@ public class Overlay
      * @param useNonSeqParser indicates whether the nonsequential parser is used
      * @throws IOException if something went wrong
      */
-    public void overlay(Map<Integer, String> specificPageOverlayFile, boolean useNonSeqParser)
-            throws IOException, COSVisitorException
+    public void overlay(Map<Integer, String> specificPageOverlayFile, boolean useNonSeqParser) throws IOException,
+            COSVisitorException
     {
         PDDocument sourcePDFDocument = null;
         PDDocument defaultOverlay = null;
@@ -95,7 +96,7 @@ public class Overlay
         PDDocument evenPageOverlay = null;
         try
         {
-            sourcePDFDocument = PDDocument.load(inputFileName);
+            sourcePDFDocument = loadPDF(inputFileName, useNonSeqParser);
             if (defaultOverlayFilename != null)
             {
                 defaultOverlay = loadPDF(defaultOverlayFilename, useNonSeqParser);
@@ -118,14 +119,6 @@ public class Overlay
             }
             if (evenPageOverlayFilename != null)
             {
-                if (useNonSeqParser)
-                {
-                    evenPageOverlay = PDDocument.loadNonSeq(new File(evenPageOverlayFilename), null);
-                }
-                else
-                {
-                    evenPageOverlay = PDDocument.load(evenPageOverlayFilename);
-                }
                 evenPageOverlay = loadPDF(evenPageOverlayFilename, useNonSeqParser);
                 evenPageOverlayPage = getLayoutPage(evenPageOverlay);
             }
@@ -216,8 +209,7 @@ public class Overlay
         {
             resources = new PDResources();
         }
-        return new LayoutPage(page.getMediaBox(), createContentStream(contents),
-                resources.getCOSDictionary());
+        return new LayoutPage(page.getMediaBox(), createContentStream(contents), resources.getCOSDictionary());
     }
 
     private COSStream createContentStream(COSBase contents) throws IOException
@@ -323,8 +315,7 @@ public class Overlay
         }
     }
 
-    private void overlayPage(COSArray array, PDPage page, int pageNumber, int numberOfPages)
-            throws IOException
+    private void overlayPage(COSArray array, PDPage page, int pageNumber, int numberOfPages) throws IOException
     {
         LayoutPage layoutPage = null;
         if (specificPageOverlayPage.containsKey(pageNumber))
@@ -359,70 +350,31 @@ public class Overlay
                 resources = new PDResources();
                 page.setResources(resources);
             }
-            String xObjectId = createOverlayXObject(page, layoutPage,
-                    layoutPage.overlayContentStream);
+            String xObjectId = createOverlayXObject(page, layoutPage, layoutPage.overlayContentStream);
             array.add(createOverlayStream(page, layoutPage, xObjectId));
         }
     }
 
     private String createOverlayXObject(PDPage page, LayoutPage layoutPage, COSStream contentStream)
     {
+        PDXObjectForm xobjForm = new PDXObjectForm(contentStream);
+        xobjForm.setResources(new PDResources(layoutPage.overlayResources));
+        xobjForm.setFormType(1);
+        xobjForm.setBBox(layoutPage.overlayMediaBox.createRetranslatedRectangle());
+        xobjForm.setMatrix(new AffineTransform());
         PDResources resources = page.findResources();
-        // determine new ID
-        COSDictionary dict = (COSDictionary) resources.getCOSDictionary().getDictionaryObject(
-                COSName.XOBJECT);
-        if (dict == null)
-        {
-            dict = new COSDictionary();
-            resources.getCOSDictionary().setItem(COSName.XOBJECT, dict);
-        }
-        String xObjectId = getNextUniqueKey(resources.getXObjects(), XOBJECT_PREFIX);
-
-        // wrap the layout content in a BBox and add it to page
-        COSStream xobj = contentStream;
-        xobj.setItem(COSName.RESOURCES, layoutPage.overlayResources);
-        xobj.setItem(COSName.TYPE, COSName.XOBJECT);
-        xobj.setItem(COSName.SUBTYPE, COSName.FORM);
-        xobj.setInt(COSName.FORMTYPE, 1);
-        COSArray matrix = new COSArray();
-        matrix.add(COSInteger.get(1));
-        matrix.add(COSInteger.get(0));
-        matrix.add(COSInteger.get(0));
-        matrix.add(COSInteger.get(1));
-        matrix.add(COSInteger.get(0));
-        matrix.add(COSInteger.get(0));
-        xobj.setItem(COSName.MATRIX, matrix);
-        COSArray bbox = new COSArray();
-        bbox.add(COSInteger.get(0));
-        bbox.add(COSInteger.get(0));
-        bbox.add(COSInteger.get((int) layoutPage.overlayMediaBox.getWidth()));
-        bbox.add(COSInteger.get((int) layoutPage.overlayMediaBox.getHeight()));
-        xobj.setItem(COSName.BBOX, bbox);
-        dict.setItem(xObjectId, xobj);
-
-        return xObjectId;
-    }
-
-    private static String getNextUniqueKey(Map<String, ?> map, String prefix)
-    {
-        int counter = 0;
-        while (map != null && map.get(prefix + counter) != null)
-        {
-            counter++;
-        }
-        return prefix + counter;
+        return resources.addXObject(xobjForm, XOBJECT_PREFIX);
     }
 
-    private COSStream createOverlayStream(PDPage page, LayoutPage layoutPage, String xObjectId)
-            throws IOException
+    private COSStream createOverlayStream(PDPage page, LayoutPage layoutPage, String xObjectId) throws IOException
     {
         // create a new content stream that executes the XObject content
         PDRectangle pageMediaBox = page.getMediaBox();
         float scale = 1;
         float hShift = (pageMediaBox.getWidth() - layoutPage.overlayMediaBox.getWidth()) / 2.0f;
         float vShift = (pageMediaBox.getHeight() - layoutPage.overlayMediaBox.getHeight()) / 2.0f;
-        return createStream("q\nq " + scale + " 0 0 " + scale + " " + hShift + " " + vShift
-                + " cm /" + xObjectId + " Do Q\nQ\n");
+        return createStream("q\nq " + scale + " 0 0 " + scale + " " + hShift + " " + vShift + " cm /" + xObjectId
+                + " Do Q\nQ\n");
     }
 
     private COSStream createStream(String content) throws IOException