Author: msahyoun
Date: Thu Nov 12 17:36:13 2015
New Revision: 1714097

URL: http://svn.apache.org/viewvc?rev=1714097&view=rev
Log:
PDFBOX-2970: allow refresh of field appearances

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
    
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1714097&r1=1714096&r2=1714097&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
 Thu Nov 12 17:36:13 2015
@@ -152,18 +152,45 @@ public final class PDAcroForm implements
         }
         return fdf;
     }
-    
-    
+
     /**
      * This will flatten all form fields.
      * 
      * <p>Flattening a form field will take the current appearance and make 
that part
      * of the pages content stream. All form fields and annotations associated 
are removed.</p>
-     *  
+     * 
+     * <p>The appearances for the form fields widgets will 
<strong>not</strong> be generated<p>
+     * 
      * @throws IOException 
      */
     public void flatten() throws IOException
     {
+       List<PDField> fields = new ArrayList<PDField>();
+       for (PDField field: getFieldTree())
+       {
+               fields.add(field);
+       }
+       flatten(fields, false);
+    }
+    
+    
+    /**
+     * This will flatten the specified form fields.
+     * 
+     * <p>Flattening a form field will take the current appearance and make 
that part
+     * of the pages content stream. All form fields and annotations associated 
are removed.</p>
+     * 
+     * @param refreshAppearances if set to true the appearances for the form 
field widgets will be updated
+     * @throws IOException 
+     */
+    public void flatten(List<PDField> fields, boolean refreshAppearances) 
throws IOException
+    {
+       // construct the appearances if set
+       if (refreshAppearances)
+       {
+               refreshAppearances();
+       }
+       
         // indicates if the original content stream
         // has been wrapped in a q...Q pair.
         boolean isContentStreamWrapped = false;
@@ -173,7 +200,7 @@ public final class PDAcroForm implements
         
         // Iterate over all form fields and their widgets and create a
         // FormXObject at the page content level from that
-        for (PDField field : getFieldTree())
+        for (PDField field : fields)
         {
             for (PDAnnotationWidget widget : field.getWidgets())
             {
@@ -222,6 +249,41 @@ public final class PDAcroForm implements
     }    
 
     /**
+     * Refreshes the appearance streams and appearance dictionaries for 
+     * the widget annotations of all fields.
+     * 
+     * @throws IOException
+     */
+    public void refreshAppearances() throws IOException
+    {
+        for (PDField field : getFieldTree())
+        {
+               if (field instanceof PDTerminalField)
+               {
+                       ((PDTerminalField) field).constructAppearances();
+               }
+        }
+    }
+
+    /**
+     * Refreshes the appearance streams and appearance dictionaries for 
+     * the widget annotations of the specified fields.
+     * 
+     * @throws IOException
+     */
+    public void refreshAppearances(List<PDField> fields) throws IOException
+    {
+        for (PDField field : fields)
+        {
+               if (field instanceof PDTerminalField)
+               {
+                       ((PDTerminalField) field).constructAppearances();
+               }
+        }
+    }
+    
+    
+    /**
      * This will return all of the documents root fields.
      * 
      * A field might have children that are fields (non-terminal field) or 
does not

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java?rev=1714097&r1=1714096&r2=1714097&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
 Thu Nov 12 17:36:13 2015
@@ -271,6 +271,19 @@ public abstract class PDButton extends P
                 // TODO: implement appearance generation for radio buttons
                 throw new UnsupportedOperationException("Appearance generation 
is not implemented yet, see PDFBOX-2849");
             }
+            else 
+            {
+                PDAppearanceEntry appearanceEntry = 
widget.getAppearance().getNormalAppearance();
+                String value = getValue();
+                if (((COSDictionary) 
appearanceEntry.getCOSObject()).containsKey(value))
+                {
+                    widget.getCOSObject().setName(COSName.AS, value);
+                }
+                else
+                {
+                    widget.getCOSObject().setItem(COSName.AS, COSName.Off);
+                }
+            }
         }
     }  
 

Modified: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java?rev=1714097&r1=1714096&r2=1714097&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
 Thu Nov 12 17:36:13 2015
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
+import java.io.File;
 import java.io.IOException;
 
 import org.apache.pdfbox.cos.COSName;
@@ -77,6 +78,14 @@ public class PDAcroFormTest
         acroForm.setDefaultAppearance("/Helv 0 Tf 0 g");
         assertEquals(acroForm.getDefaultAppearance(),"/Helv 0 Tf 0 g");
     }
+    
+    @Test
+    public void testFlatten() throws IOException
+    {
+        PDDocument testPdf = PDDocument.load(new 
File("target/test-output/AlignmentTests.pdf"));
+        testPdf.getDocumentCatalog().getAcroForm().flatten();
+        testPdf.save("target/test-output/AlignmentTests-flattened.pdf"); 
+    }
 
     @After
     public void tearDown() throws IOException


Reply via email to