]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
FOP-3140: Merge form fields in external PDFs
authorSimon Steiner <ssteiner@apache.org>
Tue, 13 Jun 2023 07:25:22 +0000 (08:25 +0100)
committerSimon Steiner <ssteiner@apache.org>
Fri, 21 Jul 2023 10:10:41 +0000 (11:10 +0100)
fop-core/src/main/java/org/apache/fop/pdf/PDFDocument.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFPainter.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererConfig.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOption.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRendererOptionsConfig.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingContext.java
fop-core/src/main/java/org/apache/fop/render/pdf/PDFRenderingUtil.java
fop-core/src/test/java/org/apache/fop/apps/PDFRendererConfBuilder.java
fop-core/src/test/java/org/apache/fop/render/pdf/PDFRendererConfigParserTestCase.java

index 2e13e70152fbd09e027a9fcad1fc7b71951ba106..b6303981258bd26429e44f38a7f39cd82d1db311 100644 (file)
@@ -176,6 +176,8 @@ public class PDFDocument {
 
     private boolean mergeFontsEnabled;
 
+    private boolean mergeFormFieldsEnabled;
+
     private boolean linearizationEnabled;
 
     private boolean formXObjectEnabled;
@@ -432,7 +434,7 @@ public class PDFDocument {
      * @param obj {@link PDFObject} to add
      * @return the added {@link PDFObject} added (with its object number set)
      */
-    <T extends PDFObject> T registerTrailerObject(T obj) {
+    public <T extends PDFObject> T registerTrailerObject(T obj) {
         assignObjectNumber(obj);
         addTrailerObject(obj);
         return obj;
@@ -1187,6 +1189,14 @@ public class PDFDocument {
         }
     }
 
+    public boolean isMergeFormFieldsEnabled() {
+        return mergeFormFieldsEnabled;
+    }
+
+    public void setMergeFormFieldsEnabled(boolean mergeFormFieldsEnabled) {
+        this.mergeFormFieldsEnabled = mergeFormFieldsEnabled;
+    }
+
     private interface TrailerOutputHelper {
 
         void outputStructureTreeElements(OutputStream stream) throws IOException;
index 5e3ab010efb512ccf7d884156baa23dcfa280eaf..02c532c1c46969816befd32ba240b6c63c189f51 100644 (file)
@@ -97,6 +97,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
     private final PDFDocumentNavigationHandler documentNavigationHandler
             = new PDFDocumentNavigationHandler(this);
 
+    private Map<String, Object> usedFieldNames = new HashMap<>();
     private Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
     private Map<String, PDFReference> contents = new HashMap<String, PDFReference>();
 
@@ -377,6 +378,10 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
         return structureTreeBuilder;
     }
 
+    public Map<String, Object> getUsedFieldNames() {
+        return usedFieldNames;
+    }
+
     public Map<Integer, PDFArray> getPageNumbers() {
         return pageNumbers;
     }
index cebd083a8de562c570e9d5c0e55b5e915d2e4632..5478f44907b64c9a77b7f11688cdcd78ab2fde47 100644 (file)
@@ -245,6 +245,7 @@ public class PDFPainter extends AbstractIFPainter<PDFDocumentHandler> {
         PDFRenderingContext pdfContext = new PDFRenderingContext(
                 getUserAgent(), generator, getDocumentHandler().getCurrentPage(), getFontInfo());
         pdfContext.setMarkedContentInfo(imageMCI);
+        pdfContext.setUsedFieldNames(getDocumentHandler().getUsedFieldNames());
         pdfContext.setPageNumbers(getDocumentHandler().getPageNumbers());
         pdfContext.setPdfLogicalStructureHandler(logicalStructureHandler);
         pdfContext.setCurrentSessionStructElem((PDFStructElem) getContext().getStructureTreeElement());
index 28111ceed73c52035be05b9f94ae13252df84a60..e71072bfe113ca896e97d671e570c45cbd05b546 100644 (file)
@@ -60,6 +60,7 @@ import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST;
 import static org.apache.fop.render.pdf.PDFRendererOption.FORM_XOBJECT;
 import static org.apache.fop.render.pdf.PDFRendererOption.LINEARIZATION;
 import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FONTS;
+import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FORM_FIELDS;
 import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_UA_MODE;
@@ -150,6 +151,7 @@ public final class PDFRendererConfig implements RendererConfig {
                 parseAndPut(OUTPUT_PROFILE, cfg);
                 parseAndPut(DISABLE_SRGB_COLORSPACE, cfg);
                 parseAndPut(MERGE_FONTS, cfg);
+                parseAndPut(MERGE_FORM_FIELDS, cfg);
                 parseAndPut(LINEARIZATION, cfg);
                 parseAndPut(FORM_XOBJECT, cfg);
                 parseAndPut(VERSION, cfg);
index 677256fe45944dce08684f80c92d75de71bbe7db..97834ed2a1666ceeb287ad298958fb83089ad280 100644 (file)
@@ -87,6 +87,12 @@ public enum PDFRendererOption implements RendererConfigOption {
             return Boolean.valueOf(value);
         }
     },
+    MERGE_FORM_FIELDS("merge-form-fields", false) {
+        @Override
+        Boolean deserialize(String value) {
+            return Boolean.valueOf(value);
+        }
+    },
     LINEARIZATION("linearization", false) {
         @Override
         Boolean deserialize(String value) {
index 02cc9f57dfd319928d1083b510b8d3ed74f7dd3a..0360782ef58d92de61bdc199925d294fa136532d 100644 (file)
@@ -36,6 +36,7 @@ import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST;
 import static org.apache.fop.render.pdf.PDFRendererOption.FORM_XOBJECT;
 import static org.apache.fop.render.pdf.PDFRendererOption.LINEARIZATION;
 import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FONTS;
+import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FORM_FIELDS;
 import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_UA_MODE;
@@ -140,6 +141,10 @@ public final class PDFRendererOptionsConfig {
         return (Boolean)properties.get(MERGE_FONTS);
     }
 
+    public Boolean getMergeFormFieldsEnabled() {
+        return (Boolean)properties.get(MERGE_FORM_FIELDS);
+    }
+
     public Boolean getLinearizationEnabled() {
         return (Boolean)properties.get(LINEARIZATION);
     }
index b99bcdbfed85e98d77086e4f88e7e5f5ffa71d03..8a38d002b75c5455cdd861486ec141a5619f45b9 100644 (file)
@@ -40,6 +40,7 @@ public class PDFRenderingContext extends AbstractRenderingContext {
     private FontInfo fontInfo;
     private PDFPage page;
     private MarkedContentInfo mci;
+    private Map<String, Object> usedFieldNames;
     private Map<Integer, PDFArray> pageNumbers;
     private PDFLogicalStructureHandler pdfLogicalStructureHandler;
     private PDFStructElem currentSessionStructElem;
@@ -96,6 +97,14 @@ public class PDFRenderingContext extends AbstractRenderingContext {
         return mci;
     }
 
+    public Map<String, Object> getUsedFieldNames() {
+        return usedFieldNames;
+    }
+
+    public void setUsedFieldNames(Map<String, Object> usedFieldNames) {
+        this.usedFieldNames = usedFieldNames;
+    }
+
     public Map<Integer, PDFArray> getPageNumbers() {
         return pageNumbers;
     }
index f2cf5beff8e8ffb4fa7b2f969eb712725b11b43d..345cc424f5eeb8071508c31bbc8219997a31585e 100644 (file)
@@ -630,6 +630,7 @@ class PDFRenderingUtil {
 
         this.pdfDoc.enableAccessibility(userAgent.isAccessibilityEnabled());
         pdfDoc.setMergeFontsEnabled(rendererConfig.getMergeFontsEnabled());
+        pdfDoc.setMergeFormFieldsEnabled(rendererConfig.getMergeFormFieldsEnabled());
         pdfDoc.setLinearizationEnabled(rendererConfig.getLinearizationEnabled());
         pdfDoc.setFormXObjectEnabled(rendererConfig.getFormXObjectEnabled());
 
index d217394766d60ee9a85292fad03e319e24fb45b9..273d9763341fab2681254009e14465a28c28720f 100644 (file)
@@ -32,6 +32,7 @@ import static org.apache.fop.render.pdf.PDFEncryptionOption.USER_PASSWORD;
 import static org.apache.fop.render.pdf.PDFRendererOption.DISABLE_SRGB_COLORSPACE;
 import static org.apache.fop.render.pdf.PDFRendererOption.FILTER_LIST;
 import static org.apache.fop.render.pdf.PDFRendererOption.FORM_XOBJECT;
+import static org.apache.fop.render.pdf.PDFRendererOption.MERGE_FORM_FIELDS;
 import static org.apache.fop.render.pdf.PDFRendererOption.OUTPUT_PROFILE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_A_MODE;
 import static org.apache.fop.render.pdf.PDFRendererOption.PDF_X_MODE;
@@ -99,6 +100,11 @@ public final class PDFRendererConfBuilder extends RendererConfBuilder {
         return this;
     }
 
+    public PDFRendererConfBuilder setMergeFormFieldsEnabled(boolean b) {
+        createTextElement(MERGE_FORM_FIELDS, String.valueOf(b));
+        return this;
+    }
+
     public final class EncryptionParamsBuilder {
         private final Element el;
 
index 7b81b4db89f36b5d1054b9aee3f6e3af80ba1b04..e7d7a5c592e00961a768c097286e2bba1790be80 100644 (file)
@@ -226,4 +226,10 @@ public class PDFRendererConfigParserTestCase
     public void testErroneousPDFVersionsNotSet() throws Exception {
         pdfVersionTester("");
     }
+
+    @Test
+    public void testMergeFormFields() throws Exception {
+        parseConfig(createRenderer().setMergeFormFieldsEnabled(true));
+        assertTrue(conf.getConfigOptions().getMergeFormFieldsEnabled());
+    }
 }