pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1824291 - in /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/PDPage.java pdmodel/interactive/annotation/AnnotationFilter.java rendering/PDFRenderer.java rendering/PageDrawer.java
Date Thu, 15 Feb 2018 09:34:16 GMT
Author: msahyoun
Date: Thu Feb 15 09:34:16 2018
New Revision: 1824291

URL: http://svn.apache.org/viewvc?rev=1824291&view=rev
Log:
PDFBOX-4094: add ability to process only specific annotations as suggested by Maxime Veron

Added:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java
  (with props)
Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java?rev=1824291&r1=1824290&r2=1824291&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java Thu Feb
15 09:34:16 2018
@@ -40,6 +40,7 @@ import org.apache.pdfbox.pdmodel.common.
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.common.PDStream;
 import org.apache.pdfbox.pdmodel.interactive.action.PDPageAdditionalActions;
+import org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 import org.apache.pdfbox.pdmodel.interactive.measurement.PDViewportDictionary;
 import org.apache.pdfbox.pdmodel.interactive.pagenavigation.PDThreadBead;
@@ -644,7 +645,7 @@ public class PDPage implements COSObject
         page.setItem(COSName.TRANS, transition);
         page.setItem(COSName.DUR, new COSFloat(duration));
     }
-
+    
     /**
      * This will return a list of the annotations for this page.
      *
@@ -655,6 +656,27 @@ public class PDPage implements COSObject
      */
     public List<PDAnnotation> getAnnotations() throws IOException
     {
+        return getAnnotations(new AnnotationFilter()
+        {
+            @Override
+            public boolean accept(PDAnnotation annotation)
+            {
+                return true;
+            }
+        });
+    }    
+
+    /**
+     * This will return a list of the annotations for this page.
+     *
+     * @param annotationFilter the annotation filter provided allowing to filter out specific
annotations
+     * @return List of the PDAnnotation objects, never null. The returned list is backed
by the
+     * annotations COSArray, so any adding or deleting in this list will change the document
too.
+     * 
+     * @throws IOException If there is an error while creating the annotation list.
+     */
+    public List<PDAnnotation> getAnnotations(AnnotationFilter annotationFilter) throws
IOException
+    {
         COSBase base = page.getDictionaryObject(COSName.ANNOTS);
         if (base instanceof COSArray)
         {
@@ -667,7 +689,11 @@ public class PDPage implements COSObject
                 {
                     continue;
                 }
-                actuals.add(PDAnnotation.createAnnotation(item));
+                PDAnnotation createdAnnotation = PDAnnotation.createAnnotation(item);
+                if (annotationFilter.accept(createdAnnotation))
+                {
+                    actuals.add(createdAnnotation);
+                }
             }
             return new COSArrayList<PDAnnotation>(actuals, annots);
         }

Added: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java?rev=1824291&view=auto
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java
(added)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java
Thu Feb 15 09:34:16 2018
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel.interactive.annotation;
+
+/**
+ * Simple interface allowing the use of an annotation filter visitor.
+ * 
+ * @author <a href="mailto:maxime.veron.pro@gmail.com">Maxime Veron</a>
+ * 
+ */
+public interface AnnotationFilter
+{
+    boolean accept(PDAnnotation annotation);
+}

Propchange: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/AnnotationFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java?rev=1824291&r1=1824290&r2=1824291&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFRenderer.java
Thu Feb 15 09:34:16 2018
@@ -27,6 +27,8 @@ import org.apache.pdfbox.pdmodel.PDResou
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.graphics.blend.BlendMode;
 import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
+import org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 
 /**
  * Renders a PDF document to an AWT BufferedImage.
@@ -38,6 +40,18 @@ public class PDFRenderer
 {
     protected final PDDocument document;
     // TODO keep rendering state such as caches here
+    
+    /**
+     * Default annotations filter, returns all annotations
+     */
+     private AnnotationFilter annotationFilter = new AnnotationFilter()
+     {
+         @Override
+         public boolean accept(PDAnnotation annotation)
+         {
+             return true;
+         }
+     };
 
     /**
      * Creates a new PDFRenderer.
@@ -47,6 +61,28 @@ public class PDFRenderer
     {
         this.document = document;
     }
+    
+    /**
+     * Return the AnnotationFilter.
+     * 
+     * @return the AnnotationFilter
+     */
+    public AnnotationFilter getAnnotationsFilter()
+    {
+        return annotationFilter;
+    }
+
+    /**
+     * Set the AnnotationFilter.
+     * 
+     * <p>Allows to only render annotation accepted by the filter.
+     * 
+     * @param annotationsFilter the AnnotationFilter
+     */
+    public void setAnnotationsFilter(AnnotationFilter annotationsFilter)
+    {
+        this.annotationFilter = annotationsFilter;
+    }
 
     /**
      * Returns the given page as an RGB image at 72 DPI
@@ -249,7 +285,9 @@ public class PDFRenderer
      */
     protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException
     {
-        return new PageDrawer(parameters);
+        PageDrawer pageDrawer = new PageDrawer(parameters);
+        pageDrawer.setAnnotationFilter(annotationFilter);
+        return pageDrawer;
     }
 
     private boolean hasBlendMode(PDPage page)

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java?rev=1824291&r1=1824290&r2=1824291&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/rendering/PageDrawer.java Thu
Feb 15 09:34:16 2018
@@ -77,6 +77,7 @@ import org.apache.pdfbox.pdmodel.graphic
 import org.apache.pdfbox.pdmodel.graphics.state.PDGraphicsState;
 import org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask;
 import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
+import org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup;
@@ -131,6 +132,18 @@ public class PageDrawer extends PDFGraph
     private final Map<PDFont, Glyph2D> fontGlyph2D = new HashMap<PDFont, Glyph2D>();
     
     private final TilingPaintFactory tilingPaintFactory = new TilingPaintFactory(this);
+    
+    /**
+    * Default annotations filter, returns all annotations
+    */
+    private AnnotationFilter annotationFilter = new AnnotationFilter()
+    {
+        @Override
+        public boolean accept(PDAnnotation annotation)
+        {
+            return true;
+        }
+    };
 
     /**
      * Constructor.
@@ -145,6 +158,28 @@ public class PageDrawer extends PDFGraph
     }
 
     /**
+     * Return the AnnotationFilter.
+     * 
+     * @return the AnnotationFilter
+     */
+    public AnnotationFilter getAnnotationFilter()
+    {
+        return annotationFilter;
+    }
+
+    /**
+     * Set the AnnotationFilter.
+     * 
+     * <p>Allows to only render annotation accepted by the filter.
+     * 
+     * @param annotationsFilter the AnnotationFilter
+     */
+    public void setAnnotationFilter(AnnotationFilter annotationFilter)
+    {
+        this.annotationFilter = annotationFilter;
+    }    
+    
+    /**
      * Returns the parent renderer.
      */
     public final PDFRenderer getRenderer()
@@ -205,7 +240,7 @@ public class PageDrawer extends PDFGraph
 
         processPage(getPage());
 
-        for (PDAnnotation annotation : getPage().getAnnotations())
+        for (PDAnnotation annotation : getPage().getAnnotations(annotationFilter))
         {
             showAnnotation(annotation);
         }



Mime
View raw message