Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-3924

How to flatten pdf which contains acroform fields

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Critical
    • Resolution: Duplicate
    • Affects Version/s: 2.0.7
    • Fix Version/s: None
    • Component/s: AcroForm
    • Labels:
      None
    • Environment:
      Windows 10

      Description

      After set the value in the acroForm fields, i want to flatten the pdf. so that there should not be any form fields in my output pdf and when i am writing acroForm.flatten() it's not working.
      Here is my java code:

      package com.sdrc.image;
      
      import java.io.File;
      import java.util.List;
      
      import org.apache.pdfbox.pdmodel.PDDocument;
      import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
      import org.apache.pdfbox.pdmodel.interactive.form.PDField;
      
      public class PDFBoxLanguageTest {
      	public static void main(String[] args) throws Exception {
      		
      	    String formTemplate = "C:/Users/XXXX/Downloads/CPIS_test_language.pdf";
                  PDDocument pdfDocument = PDDocument.load(new File(formTemplate));
                  PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
                  acroForm.setNeedAppearances(true); //need this line for Hindi
              
                  if (acroForm != null) {
                      // Get field names
                      List<PDField> fieldList = acroForm.getFields();
                      for (PDField pdField : fieldList) {
                  	    if (pdField.getFullyQualifiedName().equals("Text1")){
                  		// Here i am passing a text in "English"
          			acroForm.getField(pdField.getFullyQualifiedName()).setValue("Welcome");
                              //pdField.setReadOnly(true);// not working
                              //pdField.getCOSObject().setInt("FF", 1);// not working
                  	    } else if (pdField.getFullyQualifiedName().equals("Text3")) {
                  		// Here i am passing a text in "English-Hindi"
          			acroForm.getField(pdField.getFullyQualifiedName()).setValue("Welcome-नमस्ते");	
                              //pdField.setReadOnly(true);// not working
                              //pdField.getCOSObject().setInt("FF", 1);// not working
                  	    } else {
                  		// Here i am passing a text in "Hindi"
          			acroForm.getField(pdField.getFullyQualifiedName()).setValue("नमस्ते");
                  	        //pdField.setReadOnly(true);// not working
                              //pdField.getCOSObject().setInt("FF", 1);// not working
                          }
      	        }
                  }
                  // acroForm.flatten(); // not working
                  pdfDocument.save("C:/Users/XXXX/Downloads/CPIS_test_languageE.pdf");
                  // acroForm.flatten();  // not working
                  pdfDocument.close();
      
                  System.out.println("Done");
                  System.out.println("============================================");
      	}
      }
      

        Attachments

        1. CPIS_test_language.pdf
          265 kB
          Subrata Kumar Pradhan

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                subrata1945 Subrata Kumar Pradhan
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: