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 11:08:11 UTC

svn commit: r1794807 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Author: msahyoun
Date: Thu May 11 11:08:10 2017
New Revision: 1794807

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

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java?rev=1794807&r1=1794806&r2=1794807&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java Thu May 11 11:08:10 2017
@@ -92,9 +92,51 @@ 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.
      *