pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1642737 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/Overlay.java
Date Mon, 01 Dec 2014 19:22:51 GMT
Author: lehmi
Date: Mon Dec  1 19:22:51 2014
New Revision: 1642737

URL: http://svn.apache.org/r1642737
Log:
PDFBOX-2525: added the usage of PDDocuments as input

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=1642737&r1=1642736&r2=1642737&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 Dec 
1 19:22:51 2014
@@ -71,13 +71,28 @@ public class Overlay
     private Position position = Position.BACKGROUND;
 
     private String inputFileName = null;
+    private PDDocument inputPDFDocument = null;
+
     private String outputFilename = null;
+    
     private String defaultOverlayFilename = null;
+    private PDDocument defaultOverlay = null;
+
     private String firstPageOverlayFilename = null;
+    private PDDocument firstPageOverlay = null;
+
     private String lastPageOverlayFilename = null;
+    private PDDocument lastPageOverlay = null;
+    
     private String allPagesOverlayFilename = null;
+    private PDDocument allPagesOverlay = null;
+    
     private String oddPageOverlayFilename = null;
+    private PDDocument oddPageOverlay = null;
+    
     private String evenPageOverlayFilename = null;
+    private PDDocument evenPageOverlay = null;
+
     
     private int numberOfOverlayPages = 0;
     private boolean useAllOverlayPages = false;
@@ -86,70 +101,32 @@ public class Overlay
      * This will add overlays to a documents.
      * 
      * @param specificPageOverlayFile map of overlay files for specific pages
-     * @param useNonSeqParser indicates whether the nonsequential parser is used
+     * @param useNonSeqParser indicates whether the non-sequential parser is used
      * @throws IOException if something went wrong
+     * @throws COSVisitorException if something went wrong when savin the pdf
      */
     public void overlay(Map<Integer, String> specificPageOverlayFile, boolean useNonSeqParser)
throws IOException,
             COSVisitorException
     {
-        PDDocument sourcePDFDocument = null;
-        PDDocument defaultOverlay = null;
-        PDDocument firstPageOverlay = null;
-        PDDocument lastPageOverlay = null;
-        PDDocument allPagesOverlay = null;
-        PDDocument oddPageOverlay = null;
-        PDDocument evenPageOverlay = null;
         try
         {
-            sourcePDFDocument = loadPDF(inputFileName, useNonSeqParser);
-            if (defaultOverlayFilename != null)
-            {
-                defaultOverlay = loadPDF(defaultOverlayFilename, useNonSeqParser);
-                defaultOverlayPage = getLayoutPage(defaultOverlay);
-            }
-            if (firstPageOverlayFilename != null)
-            {
-                firstPageOverlay = loadPDF(firstPageOverlayFilename, useNonSeqParser);
-                firstPageOverlayPage = getLayoutPage(firstPageOverlay);
-            }
-            if (lastPageOverlayFilename != null)
-            {
-                lastPageOverlay = loadPDF(lastPageOverlayFilename, useNonSeqParser);
-                lastPageOverlayPage = getLayoutPage(lastPageOverlay);
-            }
-            if (oddPageOverlayFilename != null)
-            {
-                oddPageOverlay = loadPDF(oddPageOverlayFilename, useNonSeqParser);
-                oddPageOverlayPage = getLayoutPage(oddPageOverlay);
-            }
-            if (evenPageOverlayFilename != null)
-            {
-                evenPageOverlay = loadPDF(evenPageOverlayFilename, useNonSeqParser);
-                evenPageOverlayPage = getLayoutPage(evenPageOverlay);
-            }
-            if (allPagesOverlayFilename != null)
-            {
-                allPagesOverlay = loadPDF(allPagesOverlayFilename, useNonSeqParser);
-                specificPageOverlayPage = getLayoutPages(allPagesOverlay);
-                useAllOverlayPages = true;
-                numberOfOverlayPages = specificPageOverlayPage.size();
-            }
+            loadPDFs(useNonSeqParser);
             for (Map.Entry<Integer, String> e : specificPageOverlayFile.entrySet())
             {
                 PDDocument doc = loadPDF(e.getValue(), useNonSeqParser);
                 specificPageOverlay.put(e.getKey(), doc);
                 specificPageOverlayPage.put(e.getKey(), getLayoutPage(doc));
             }
-            PDDocumentCatalog pdfCatalog = sourcePDFDocument.getDocumentCatalog();
+            PDDocumentCatalog pdfCatalog = inputPDFDocument.getDocumentCatalog();
             processPages(pdfCatalog.getAllPages());
 
-            sourcePDFDocument.save(outputFilename);
+            inputPDFDocument.save(outputFilename);
         }
         finally
         {
-            if (sourcePDFDocument != null)
+            if (inputPDFDocument != null)
             {
-                sourcePDFDocument.close();
+                inputPDFDocument.close();
             }
             if (defaultOverlay != null)
             {
@@ -184,6 +161,71 @@ public class Overlay
         }
     }
 
+    private void loadPDFs(boolean useNonSeqParser) throws IOException
+    {
+        // input PDF
+        if (inputFileName != null)
+        {
+            inputPDFDocument = loadPDF(inputFileName, useNonSeqParser);
+        }
+        // default overlay PDF
+        if (defaultOverlayFilename != null)
+        {
+            defaultOverlay = loadPDF(defaultOverlayFilename, useNonSeqParser);
+        }
+        if (defaultOverlay != null)
+        {
+            defaultOverlayPage = getLayoutPage(defaultOverlay);
+        }
+        // first page overlay PDF
+        if (firstPageOverlayFilename != null)
+        {
+            firstPageOverlay = loadPDF(firstPageOverlayFilename, useNonSeqParser);
+        }
+        if (firstPageOverlay != null)
+        {
+            firstPageOverlayPage = getLayoutPage(firstPageOverlay);
+        }
+        // last page overlay PDF
+        if (lastPageOverlayFilename != null)
+        {
+            lastPageOverlay = loadPDF(lastPageOverlayFilename, useNonSeqParser);
+        }
+        if (lastPageOverlay != null)
+        {
+            lastPageOverlayPage = getLayoutPage(lastPageOverlay);
+        }
+        // odd pages overlay PDF
+        if (oddPageOverlayFilename != null)
+        {
+            oddPageOverlay = loadPDF(oddPageOverlayFilename, useNonSeqParser);
+        }
+        if (oddPageOverlay != null)
+        {
+            oddPageOverlayPage = getLayoutPage(oddPageOverlay);
+        }
+        // even pages overlay PDF
+        if (evenPageOverlayFilename != null)
+        {
+            evenPageOverlay = loadPDF(evenPageOverlayFilename, useNonSeqParser);
+        }
+        if (evenPageOverlay != null)
+        {
+            evenPageOverlayPage = getLayoutPage(evenPageOverlay);
+        }
+        // all pages overlay PDF
+        if (allPagesOverlayFilename != null)
+        {
+            allPagesOverlay = loadPDF(allPagesOverlayFilename, useNonSeqParser);
+        }
+        if (allPagesOverlay != null)
+        {
+            specificPageOverlayPage = getLayoutPages(allPagesOverlay);
+            useAllOverlayPages = true;
+            numberOfOverlayPages = specificPageOverlayPage.size();
+        }
+    }
+    
     private PDDocument loadPDF(String pdfName, boolean useNonSeqParser) throws IOException
     {
         PDDocument pdf = null;
@@ -242,7 +284,8 @@ public class Overlay
             {
                 resources = new PDResources();
             }
-            layoutPages.put(i,new LayoutPage(page.getMediaBox(), createContentStream(contents),
resources.getCOSDictionary()));
+            layoutPages.put(i,new LayoutPage(page.getMediaBox(), createContentStream(contents),

+                    resources.getCOSDictionary()));
         }
         return layoutPages;
     }
@@ -448,6 +491,16 @@ public class Overlay
     }
 
     /**
+     * Sets the PDF to be overlayed.
+     * 
+     * @param inputPDF the PDF to be overlayed
+     */
+    public void setInputPDF(PDDocument inputPDF)
+    {
+        inputPDFDocument = inputPDF;
+    }
+
+    /**
      * Returns the input file.
      * 
      * @return the input file
@@ -488,6 +541,16 @@ public class Overlay
     }
 
     /**
+     * Sets the default overlay PDF.
+     * 
+     * @param defaultOverlayPDF the default overlay PDF
+     */
+    public void setDefaultOverlayPDF(PDDocument defaultOverlayPDF)
+    {
+        defaultOverlay = defaultOverlayPDF;
+    }
+
+    /**
      * Returns the default overlay file.
      * 
      * @return the default overlay file
@@ -508,6 +571,16 @@ public class Overlay
     }
 
     /**
+     * Sets the first page overlay PDF.
+     * 
+     * @param firstPageOverlayPDF the first page overlay PDF
+     */
+    public void setFirstPageOverlayPDF(PDDocument firstPageOverlayPDF)
+    {
+        firstPageOverlay = firstPageOverlayPDF;
+    }
+
+    /**
      * Sets the last page overlay file.
      * 
      * @param lastPageOverlayFile the last page overlay file
@@ -518,6 +591,16 @@ public class Overlay
     }
 
     /**
+     * Sets the last page overlay PDF.
+     * 
+     * @param lastPageOverlayPDF the last page overlay PDF
+     */
+    public void setLastPageOverlayPDF(PDDocument lastPageOverlayPDF)
+    {
+        lastPageOverlay = lastPageOverlayPDF;
+    }
+
+    /**
      * Sets the all pages overlay file.
      * 
      * @param allPagesOverlayFile the all pages overlay file
@@ -528,6 +611,16 @@ public class Overlay
     }
 
     /**
+     * Sets the all pages overlay PDF.
+     * 
+     * @param allPagesOverlayPDF the all pages overlay PDF
+     */
+    public void setAllPagesOverlayPDF(PDDocument allPagesOverlayPDF)
+    {
+        allPagesOverlay = allPagesOverlayPDF;
+    }
+
+    /**
      * Sets the odd page overlay file.
      * 
      * @param oddPageOverlayFile the odd page overlay file
@@ -538,6 +631,16 @@ public class Overlay
     }
 
     /**
+     * Sets the odd page overlay PDF.
+     * 
+     * @param oddPageOverlayPDF the odd page overlay PDF
+     */
+    public void setOddPageOverlayPDF(PDDocument oddPageOverlayPDF)
+    {
+        oddPageOverlay = oddPageOverlayPDF;
+    }
+
+    /**
      * Sets the even page overlay file.
      * 
      * @param evenPageOverlayFile the even page overlay file
@@ -547,4 +650,13 @@ public class Overlay
         evenPageOverlayFilename = evenPageOverlayFile;
     }
 
+    /**
+     * Sets the even page overlay PDF.
+     * 
+     * @param evenPageOverlayPDF the even page overlay PDF
+     */
+    public void setEvenPageOverlayPDF(PDDocument evenPageOverlayPDF)
+    {
+        evenPageOverlay = evenPageOverlayPDF;
+    }
 }



Mime
View raw message