pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1838745 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java
Date Thu, 23 Aug 2018 17:53:44 GMT
Author: tilman
Date: Thu Aug 23 17:53:44 2018
New Revision: 1838745

URL: http://svn.apache.org/viewvc?rev=1838745&view=rev
Log:
PDFBOX-4298: touch only pages that will be overlayed; refactor; improve javadoc

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java?rev=1838745&r1=1838744&r2=1838745&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Overlay.java Thu Aug 23 17:53:44
2018
@@ -94,8 +94,9 @@ public class Overlay
     /**
      * This will add overlays to a documents.
      * 
-     * @param specificPageOverlayFile map of overlay files for specific pages
-     * 
+     * @param specificPageOverlayFile map of overlay files for specific pages.
+     * The page numbers are 1-based.
+     *
      * @return the resulting pdf, which has to be saved and closed be the caller
      *  
      * @throws IOException if something went wrong
@@ -330,33 +331,39 @@ public class Overlay
 
     private void processPages(PDDocument document) throws IOException
     {
-        int pageCount = 0;
+        int pageCounter = 0;
         for (PDPage page : document.getPages())
         {
+            pageCounter++;
             COSDictionary pageDictionary = page.getCOSObject();
-            COSBase contents = pageDictionary.getDictionaryObject(COSName.CONTENTS);
-            COSArray contentArray = new COSArray();
+            COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
+            COSArray newContentArray = new COSArray();
+            LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
+            if (layoutPage == null)
+            {
+                continue;
+            }
             switch (position)
             {
-            case FOREGROUND:
-                // save state
-                contentArray.add(createStream("q\n"));
-                addOriginalContent(contents, contentArray);
-                // restore state
-                contentArray.add(createStream("Q\n"));
-                // overlay content
-                overlayPage(contentArray, page, pageCount + 1, document.getNumberOfPages());
-                break;
-            case BACKGROUND:
-                // overlay content
-                overlayPage(contentArray, page, pageCount + 1, document.getNumberOfPages());
-                addOriginalContent(contents, contentArray);
-                break;
-            default:
-                throw new IOException("Unknown type of position:" + position);
+                case FOREGROUND:
+                    // save state
+                    newContentArray.add(createStream("q\n"));
+                    addOriginalContent(originalContent, newContentArray);
+                    // restore state
+                    newContentArray.add(createStream("Q\n"));
+                    // overlay content last
+                    overlayPage(page, layoutPage, newContentArray);
+                    break;
+                case BACKGROUND:
+                    // overlay content first
+                    overlayPage(page, layoutPage, newContentArray);
+
+                    addOriginalContent(originalContent, newContentArray);
+                    break;
+                default:
+                    throw new IOException("Unknown type of position:" + position);
             }
-            pageDictionary.setItem(COSName.CONTENTS, contentArray);
-            pageCount++;
+            pageDictionary.setItem(COSName.CONTENTS, newContentArray);
         }
     }
 
@@ -377,9 +384,22 @@ public class Overlay
         }
     }
 
-    private void overlayPage(COSArray array, PDPage page, int pageNumber, int numberOfPages)
+    private void overlayPage(PDPage page, LayoutPage layoutPage, COSArray array)
             throws IOException
     {
+        PDResources resources = page.getResources();
+        if (resources == null)
+        {
+            resources = new PDResources();
+            page.setResources(resources);
+        }
+        COSName xObjectId = createOverlayXObject(page, layoutPage,
+                layoutPage.overlayContentStream);
+        array.add(createOverlayStream(page, layoutPage, xObjectId));
+    }
+
+    private LayoutPage getLayoutPage(int pageNumber, int numberOfPages)
+    {
         LayoutPage layoutPage = null;
         if (!useAllOverlayPages && specificPageOverlayPage.containsKey(pageNumber))
         {
@@ -410,18 +430,7 @@ public class Overlay
             int usePageNum = (pageNumber -1 ) % numberOfOverlayPages;
             layoutPage = specificPageOverlayPage.get(usePageNum);
         }
-        if (layoutPage != null)
-        {
-            PDResources resources = page.getResources();
-            if (resources == null)
-            {
-                resources = new PDResources();
-                page.setResources(resources);
-            }
-            COSName xObjectId = createOverlayXObject(page, layoutPage,
-                    layoutPage.overlayContentStream);
-            array.add(createOverlayStream(page, layoutPage, xObjectId));
-        }
+        return layoutPage;
     }
 
     private COSName createOverlayXObject(PDPage page, LayoutPage layoutPage, COSStream contentStream)



Mime
View raw message