pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1625097 - in /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox: OverlayPDF.java util/Overlay.java
Date Mon, 15 Sep 2014 17:57:42 GMT
Author: lehmi
Date: Mon Sep 15 17:57:41 2014
New Revision: 1625097

URL: http://svn.apache.org/r1625097
Log:
PDFBOX-2314: added backward compatibility between Overlay and OverlaPDF as proposed by Laurent
Yaish

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/OverlayPDF.java
    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/OverlayPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/OverlayPDF.java?rev=1625097&r1=1625096&r2=1625097&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/OverlayPDF.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/OverlayPDF.java Mon Sep 15
17:57:41 2014
@@ -43,6 +43,7 @@ public class OverlayPDF 
     private static final String FIRST = "-first";
     private static final String LAST = "-last";
     private static final String PAGE = "-page";
+    private static final String USEALLPAGES = "-useAllPages";
     private static final String NONSEQ = "-nonSeq";
 
     /**
@@ -104,6 +105,11 @@ public class OverlayPDF 
                 overlayer.setLastPageOverlayFile(args[i + 1].trim());
                 i += 1;
             } 
+            else if (arg.equals(USEALLPAGES) && ((i + 1) < args.length)) 
+            {
+                overlayer.setAllPagesOverlayFile(args[i + 1].trim());
+                i += 1;
+            } 
             else if (arg.equals(PAGE) && ((i + 2) < args.length) && (isInteger(args[i
+ 1].trim()))) 
             {
                 specificPageOverlayFile.put(Integer.parseInt(args[i + 1].trim()), args[i
+ 2].trim());
@@ -149,6 +155,8 @@ public class OverlayPDF 
         message.append("  -even <evenPageOverlay.pdf>                        overlay
file used for even pages\n");
         message.append("  -first <firstPageOverlay.pdf>                      overlay
file used for the first page\n");
         message.append("  -last <lastPageOverlay.pdf>                        overlay
file used for the last page\n");
+        message.append("  -useAllPages <allPagesOverlay.pdf>                 overlay
file used for overlay, all pages"
+                + " are used by simply repeating them\n");
         message.append("  -page <pageNumber> <specificPageOverlay.pdf>      
overlay file used for " +
                 "the given page number, may occur more than once\n");
         message.append("  -position foreground|background                    where to put
the overlay " +

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=1625097&r1=1625096&r2=1625097&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 Mon Sep 15
17:57:41 2014
@@ -75,8 +75,12 @@ public class Overlay
     private String defaultOverlayFilename = null;
     private String firstPageOverlayFilename = null;
     private String lastPageOverlayFilename = null;
+    private String allPagesOverlayFilename = null;
     private String oddPageOverlayFilename = null;
     private String evenPageOverlayFilename = null;
+    
+    private int numberOfOverlayPages = 0;
+    private boolean useAllOverlayPages = false;
 
     /**
      * This will add overlays to a documents.
@@ -92,6 +96,7 @@ public class Overlay
         PDDocument defaultOverlay = null;
         PDDocument firstPageOverlay = null;
         PDDocument lastPageOverlay = null;
+        PDDocument allPagesOverlay = null;
         PDDocument oddPageOverlay = null;
         PDDocument evenPageOverlay = null;
         try
@@ -122,6 +127,13 @@ public class Overlay
                 evenPageOverlay = loadPDF(evenPageOverlayFilename, useNonSeqParser);
                 evenPageOverlayPage = getLayoutPage(evenPageOverlay);
             }
+            if (allPagesOverlayFilename != null)
+            {
+                allPagesOverlay = loadPDF(allPagesOverlayFilename, useNonSeqParser);
+                specificPageOverlayPage = getLayoutPages(allPagesOverlay);
+                useAllOverlayPages = true;
+                numberOfOverlayPages = specificPageOverlayPage.size();
+            }
             for (Map.Entry<Integer, String> e : specificPageOverlayFile.entrySet())
             {
                 PDDocument doc = loadPDF(e.getValue(), useNonSeqParser);
@@ -151,6 +163,10 @@ public class Overlay
             {
                 lastPageOverlay.close();
             }
+            if (allPagesOverlay != null)
+            {
+                allPagesOverlay.close();
+            }
             if (oddPageOverlay != null)
             {
                 oddPageOverlay.close();
@@ -212,6 +228,25 @@ public class Overlay
         return new LayoutPage(page.getMediaBox(), createContentStream(contents), resources.getCOSDictionary());
     }
 
+    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++)
+        {
+            PDPage page = (PDPage) catalog.getAllPages().get(i);
+            COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
+            PDResources resources = page.findResources();
+            if (resources == null)
+            {
+                resources = new PDResources();
+            }
+            layoutPages.put(i,new LayoutPage(page.getMediaBox(), createContentStream(contents),
resources.getCOSDictionary()));
+        }
+        return layoutPages;
+    }
+
     private COSStream createContentStream(COSBase contents) throws IOException
     {
         List<COSStream> contentStreams = createContentStreamList(contents);
@@ -318,7 +353,7 @@ public class Overlay
     private void overlayPage(COSArray array, PDPage page, int pageNumber, int numberOfPages)
throws IOException
     {
         LayoutPage layoutPage = null;
-        if (specificPageOverlayPage.containsKey(pageNumber))
+        if (!useAllOverlayPages && specificPageOverlayPage.containsKey(pageNumber))
         {
             layoutPage = specificPageOverlayPage.get(pageNumber);
         }
@@ -342,6 +377,11 @@ public class Overlay
         {
             layoutPage = defaultOverlayPage;
         }
+        else if (useAllOverlayPages)
+        {
+            int usePageNum = pageNumber % numberOfOverlayPages;
+            layoutPage = specificPageOverlayPage.get(usePageNum);
+        }
         if (layoutPage != null)
         {
             PDResources resources = page.findResources();
@@ -478,6 +518,16 @@ public class Overlay
     }
 
     /**
+     * Sets the all pages overlay file.
+     * 
+     * @param allPagesOverlayFile the all pages overlay file
+     */
+    public void setAllPagesOverlayFile(String allPagesOverlayFile)
+    {
+        allPagesOverlayFilename = allPagesOverlayFile;
+    }
+
+    /**
      * Sets the odd page overlay file.
      * 
      * @param oddPageOverlayFile the odd page overlay file



Mime
View raw message