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/12/23 18:02:55 UTC

svn commit: r1647619 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java

Author: lehmi
Date: Tue Dec 23 17:02:54 2014
New Revision: 1647619

URL: http://svn.apache.org/r1647619
Log:
PDFBOX-2525: avoid floating point representation when writing matrix values

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java?rev=1647619&r1=1647618&r2=1647619&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java Tue Dec 23 17:02:54 2014
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -98,7 +99,6 @@ public class Overlay
      * This will add overlays to a documents.
      * 
      * @param specificPageOverlayFile map of overlay files for specific pages
-     * @param useNonSeqParser indicates whether the non-sequential parser is used
      * @throws IOException if something went wrong
      */
     public void overlay(Map<Integer, String> specificPageOverlayFile)
@@ -258,7 +258,6 @@ public class Overlay
     
     private HashMap<Integer,LayoutPage> getLayoutPages(PDDocument doc) throws IOException
     {
-        PDDocumentCatalog catalog = doc.getDocumentCatalog();
         int numberOfPages = doc.getNumberOfPages();
         HashMap<Integer,LayoutPage> layoutPages = new HashMap<Integer, Overlay.LayoutPage>(numberOfPages);
         for (int i=0;i<numberOfPages;i++)
@@ -442,13 +441,37 @@ public class Overlay
     {
         // 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.getName() + " Do Q\nQ\n");
+        StringBuilder overlayStream = new StringBuilder();
+        overlayStream.append("q\nq 1 0 0 1 ");
+        overlayStream.append(float2String(hShift));
+        overlayStream.append(" ");
+        overlayStream.append(float2String(vShift) );
+        overlayStream.append(" cm /");
+        overlayStream.append(xObjectId.getName());
+        overlayStream.append(" Do Q\nQ\n");
+        return createStream(overlayStream.toString());
     }
 
+    private String float2String(float floatValue)
+    {
+        // use a BigDecimal as intermediate state to avoid 
+        // a floating point string representation of the float value
+        BigDecimal value = new BigDecimal(String.valueOf(floatValue));
+        String stringValue = value.toPlainString();
+        // remove fraction digit "0" only
+        if (stringValue.indexOf('.') > -1 && !stringValue.endsWith(".0"))
+        {
+            while (stringValue.endsWith("0") && !stringValue.endsWith(".0"))
+            {
+                stringValue = stringValue.substring(0,stringValue.length()-1);
+            }
+        }
+        return stringValue;
+    }
+
+    
     private COSStream createStream(String content) throws IOException
     {
         COSStream stream = new COSStream();