FOP-2944: Only add annotations in root as input pdf does

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop-pdf-images/trunk@1878741 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java b/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
index 81caf32..f513b6b 100644
--- a/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
+++ b/src/java/org/apache/fop/render/pdf/pdfbox/PDFBoxAdapter.java
@@ -654,7 +654,7 @@
             throw new IOException("Illegal PDF. Page not part of parent page node.");
         }
 
-        Set<COSObject> fields = copyAnnotations(page);
+        Set<COSObject> fields = copyAnnotations(page, srcAcroForm);
 
         boolean formAlreadyCopied = getCachedClone(srcAcroForm) != null;
         PDFRoot catalog = this.pdfDoc.getRoot();
@@ -690,7 +690,7 @@
         }
     }
 
-    private Set<COSObject> copyAnnotations(PDPage page) throws IOException {
+    private Set<COSObject> copyAnnotations(PDPage page, PDAcroForm srcAcroForm) throws IOException {
         COSArray annots = (COSArray) page.getCOSObject().getDictionaryObject(COSName.ANNOTS);
         Set<COSObject> fields = Collections.emptySet();
         if (annots != null) {
@@ -700,9 +700,7 @@
                 exclude.add(COSName.P);
                 if (annot1 instanceof COSObject) {
                     COSObject annot = (COSObject) annot1;
-                    getField(annot, fields);
-
-
+                    getField(annot, fields, srcAcroForm);
                     if (((COSDictionary) annot.getObject()).getItem(COSName.STRUCT_PARENT) != null) {
                         exclude.add(COSName.PARENT);
                     }
@@ -731,14 +729,21 @@
         }
     }
 
-    private COSDictionary getField(COSObject fieldObject, Set<COSObject> fields) {
+    private COSDictionary getField(COSObject fieldObject, Set<COSObject> fields, PDAcroForm srcAcroForm) {
         COSDictionary field = (COSDictionary) fieldObject.getObject();
         COSObject parent;
         while ((parent = getParent(field)) != null) {
             fieldObject = parent;
             field = (COSDictionary) fieldObject.getObject();
         }
-        fields.add(fieldObject);
+        if (srcAcroForm != null) {
+            COSArray srcFields = (COSArray) srcAcroForm.getCOSObject().getDictionaryObject(COSName.FIELDS);
+            if (srcFields.toList().contains(fieldObject)) {
+                fields.add(fieldObject);
+            }
+        } else {
+            fields.add(fieldObject);
+        }
         return field;
     }
 
diff --git a/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java b/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
index 4b9d5e6..29e961f 100644
--- a/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
+++ b/test/java/org/apache/fop/render/pdf/PDFBoxAdapterTestCase.java
@@ -300,6 +300,27 @@
     }
 
     @Test
+    public void testAnnotFields() throws Exception {
+        PDFDocument pdfdoc = new PDFDocument("");
+        PDFPage pdfpage = getPDFPage(pdfdoc);
+        pdfpage.setDocument(pdfdoc);
+        pdfpage.setObjectNumber(1);
+        PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), new HashMap<Integer, PDFArray>());
+        PDDocument doc = PDDocument.load(new File(ACCESSIBLERADIOBUTTONS));
+        COSArray fields = (COSArray)
+                doc.getDocumentCatalog().getAcroForm().getCOSObject().getDictionaryObject(COSName.FIELDS);
+        fields.remove(0);
+        PDPage page = doc.getPage(0);
+        AffineTransform at = new AffineTransform();
+        Rectangle r = new Rectangle(0, 1650, 842000, 595000);
+        adapter.createStreamFromPDFBoxPage(doc, page, "key", at, null, r);
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        pdfdoc.outputTrailer(os);
+        Assert.assertTrue(os.toString("UTF-8").contains("/Fields []"));
+        doc.close();
+    }
+
+    @Test
     public void testLink() throws Exception {
         PDFDocument pdfdoc = new PDFDocument("");
         PDFPage pdfpage = getPDFPage(pdfdoc);