Author: msahyoun Date: Fri Sep 9 10:34:47 2016 New Revision: 1759980 URL: http://svn.apache.org/viewvc?rev=1759980&view=rev Log: PDFBOX-3479: remove AP entry for widgets with empty /Rect entry
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.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=1759980&r1=1759979&r2=1759980&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 Fri Sep 9 10:34:47 2016 @@ -106,6 +106,14 @@ class AppearanceGeneratorHelper for (PDAnnotationWidget widget : field.getWidgets()) { + PDRectangle rect = widget.getRectangle(); + if (rect == null) + { + widget.getCOSObject().removeItem(COSName.AP); + LOG.warn("widget of field " + field.getFullyQualifiedName() + " has no rectangle, no appearance stream created"); + continue; + } + PDFormFieldAdditionalActions actions = field.getActions(); // in case all tests fail the field will be formatted by acrobat @@ -130,13 +138,6 @@ class AppearanceGeneratorHelper } else { - PDRectangle rect = widget.getRectangle(); - if (rect == null) - { - LOG.warn("widget of field " + field.getFullyQualifiedName() + " has no rectangle, no appearance stream created"); - continue; - } - appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument()); // Calculate the entries for the bounding box and the transformation matrix Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java?rev=1759980&r1=1759979&r2=1759980&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java (original) +++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java Fri Sep 9 10:34:47 2016 @@ -24,6 +24,7 @@ import junit.framework.TestSuite; import org.apache.pdfbox.cos.COSName; import org.apache.pdfbox.cos.COSString; import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; /** * This will test the form fields in PDFBox. @@ -186,6 +187,43 @@ public class TestFields extends TestCase } finally { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will test the handling of a widget with a missing (required) /Rect entry. + * + * @throws IOException If there is an error loading the form or the field. + */ + public void testWidgetMissingRect() throws IOException + { + PDDocument doc = null; + + try + { + doc = PDDocument.load(new File(PATH_OF_PDF)); + + PDAcroForm form = doc.getDocumentCatalog().getAcroForm(); + + PDTextField textField = (PDTextField)form.getField("TextField-DefaultValue"); + PDAnnotationWidget widget = textField.getWidgets().get(0); + + // initially there is an Appearance Entry in the form + assertNotNull(widget.getCOSObject().getDictionaryObject(COSName.AP)); + widget.getCOSObject().removeItem(COSName.RECT); + textField.setValue("field value"); + + // There shall be no appearance entry if there is no /Rect to + // behave as Adobe Acrobat does + assertNull(widget.getCOSObject().getDictionaryObject(COSName.AP)); + + } + finally + { if( doc != null ) { doc.close();