You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2017/05/11 12:10:39 UTC

svn commit: r1794813 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Author: msahyoun
Date: Thu May 11 12:10:38 2017
New Revision: 1794813

URL: http://svn.apache.org/viewvc?rev=1794813&view=rev
Log:
PDFBOX-3732: match Adobe Reader behaviour in copying field level resources to AcroForm level

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1794813&r1=1794812&r2=1794813&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Thu May 11 12:10:38 2017
@@ -92,9 +92,50 @@ class AppearanceGeneratorHelper
     AppearanceGeneratorHelper(PDVariableText field) throws IOException
     {
         this.field = field;
+        validateAndEnsureAcroFormResources();
+        
         this.defaultAppearance = field.getDefaultAppearanceString();
     }
     
+    /*
+     * Adobe Reader/Acrobat are adding resources which are at the field/widget level
+     * to the AcroForm level. 
+     */
+    private void validateAndEnsureAcroFormResources() {
+        // add font resources which might be available at the field 
+        // level but are not at the AcroForm level to the AcroForm
+        // to match Adobe Reader/Acrobat behavior        
+        if (field.getAcroForm().getDefaultResources() == null)
+        {
+            return;
+        }
+        
+        PDResources acroFormResources = field.getAcroForm().getDefaultResources();
+        
+        for (PDAnnotationWidget widget : field.getWidgets())
+        {
+            if (widget.getNormalAppearanceStream() != null && widget.getNormalAppearanceStream().getResources() != null)
+            {
+                PDResources widgetResources = widget.getNormalAppearanceStream().getResources();
+                for (COSName fontResourceName : widgetResources.getFontNames())
+                {
+                    try
+                    {
+                        if (acroFormResources.getFont(fontResourceName) == null)
+                        {
+                            LOG.debug("Adding font resource " + fontResourceName + " from widget to AcroForm");
+                            acroFormResources.put(fontResourceName, widgetResources.getFont(fontResourceName));
+                        }
+                    }
+                    catch (IOException e)
+                    {
+                        LOG.warn("Unable to match field level font with AcroForm font");
+                    }
+                }
+            }
+        }
+    }
+    
     /**
      * This is the public method for setting the appearance stream.
      *