You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2018/06/07 17:27:14 UTC

svn commit: r1833130 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation: PDAnnotationText.java handlers/PDTextAppearanceHandler.java

Author: tilman
Date: Thu Jun  7 17:27:14 2018
New Revision: 1833130

URL: http://svn.apache.org/viewvc?rev=1833130&view=rev
Log:
PDFBOX-3353: create handler for text annotation and support Note

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationText.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationText.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationText.java?rev=1833130&r1=1833129&r2=1833130&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationText.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotationText.java Thu Jun  7 17:27:14 2018
@@ -18,6 +18,8 @@ package org.apache.pdfbox.pdmodel.intera
 
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDAppearanceHandler;
+import org.apache.pdfbox.pdmodel.interactive.annotation.handlers.PDTextAppearanceHandler;
 
 /**
  * This is the class that represents a text annotation.
@@ -26,6 +28,7 @@ import org.apache.pdfbox.cos.COSName;
  */
 public class PDAnnotationText extends PDAnnotationMarkup
 {
+    private PDAppearanceHandler customAppearanceHandler;
 
     /*
      * The various values of the Text as defined in the PDF 1.7 reference Table 172
@@ -171,4 +174,27 @@ public class PDAnnotationText extends PD
         this.getCOSObject().setString(COSName.STATE_MODEL, stateModel);
     }
 
+    /**
+     * Set a custom appearance handler for generating the annotations appearance streams.
+     * 
+     * @param appearanceHandler
+     */
+    public void setCustomAppearanceHandler(PDAppearanceHandler appearanceHandler)
+    {
+        customAppearanceHandler = appearanceHandler;
+    }
+
+    @Override
+    public void constructAppearances()
+    {
+        if (customAppearanceHandler == null)
+        {
+            PDTextAppearanceHandler appearanceHandler = new PDTextAppearanceHandler(this);
+            appearanceHandler.generateAppearanceStreams();
+        }
+        else
+        {
+            customAppearanceHandler.generateAppearanceStreams();
+        }
+    }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java?rev=1833130&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java Thu Jun  7 17:27:14 2018
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2018 The Apache Software Foundation.
+ *
+ * Licensed 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.handlers;
+
+import java.io.IOException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.pdfbox.pdmodel.PDAppearanceContentStream;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationText;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
+
+/**
+ *
+ * @author Tilman Hausherr
+ */
+public class PDTextAppearanceHandler extends PDAbstractAppearanceHandler
+{
+    private static final Log LOG = LogFactory.getLog(PDTextAppearanceHandler.class);
+
+    public PDTextAppearanceHandler(PDAnnotation annotation)
+    {
+        super(annotation);
+    }
+
+    @Override
+    public void generateAppearanceStreams()
+    {
+        generateNormalAppearance();
+        generateRolloverAppearance();
+        generateDownAppearance();
+    }
+
+    @Override
+    public void generateNormalAppearance()
+    {
+        PDAnnotationText annotation = (PDAnnotationText) getAnnotation();
+        if (!PDAnnotationText.NAME_NOTE.equals(annotation.getName()))
+        {
+            //TODO Comment, Key, Help, NewParagraph, Paragraph, Insert
+            return;
+        }
+
+        try (PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream())
+        {
+            boolean hasBackground = contentStream.setNonStrokingColorOnDemand(getColor());
+            setOpacity(contentStream, annotation.getConstantOpacity());
+            
+            //TODO find out what Adobe chooses if color is missing
+
+            PDRectangle rect = getRectangle();
+            PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
+            PDRectangle bbox = rect.createRetranslatedRectangle();
+            appearanceStream.setBBox(bbox);
+
+            switch (annotation.getName())
+            {
+                case PDAnnotationText.NAME_NOTE:
+                    drawNote(contentStream, bbox, hasBackground);
+                    break;
+
+                default:
+                    break;
+            }
+
+        }
+        catch (IOException e)
+        {
+            LOG.error(e);
+        }
+
+    }
+
+    private void drawNote(final PDAppearanceContentStream contentStream, PDRectangle bbox, boolean hasBackground)
+            throws IOException
+    {
+        contentStream.setLineJoinStyle(1); // round edge
+        contentStream.addRect(1, 1, bbox.getWidth() - 2,  bbox.getHeight() - 2);
+        contentStream.moveTo(bbox.getWidth() / 4,         bbox.getHeight() / 7 * 2);
+        contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 2);
+        contentStream.moveTo(bbox.getWidth() / 4,         bbox.getHeight() / 7 * 3);
+        contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 3);
+        contentStream.moveTo(bbox.getWidth() / 4,         bbox.getHeight() / 7 * 4);
+        contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 4);
+        contentStream.moveTo(bbox.getWidth() / 4,         bbox.getHeight() / 7 * 5);
+        contentStream.lineTo(bbox.getWidth() * 3 / 4 - 1, bbox.getHeight() / 7 * 5);
+        contentStream.drawShape(1, true, hasBackground);
+    }
+
+    @Override
+    public void generateRolloverAppearance()
+    {
+        // No rollover appearance generated
+    }
+
+    @Override
+    public void generateDownAppearance()
+    {
+        // No down appearance generated
+    }
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDTextAppearanceHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native