PDFBox: How to “flatten” a PDF-form?

后端 未结 11 2185
难免孤独
难免孤独 2020-12-09 06:32

How do I \"flatten\" a PDF-form (remove the form-field but keep the text of the field) with PDFBox?

Same question was answered here:

a quick

11条回答
  •  一生所求
    2020-12-09 07:01

    With PDFBox 2 it's now possible to "flatten" a PDF-form easily by calling the flatten method on a PDAcroForm object. See Javadoc: PDAcroForm.flatten().

    Simplified code with an example call of this method:

    //Load the document
    PDDocument pDDocument = PDDocument.load(new File("E:\\Form-Test.pdf"));    
    PDAcroForm pDAcroForm = pDDocument.getDocumentCatalog().getAcroForm();
    
    //Fill the document
    ...
    
    //Flatten the document
    pDAcroForm.flatten();
    
    //Save the document
    pDDocument.save("E:\\Form-Test-Result.pdf");
    pDDocument.close();
    

    Note: dynamic XFA forms cannot be flatten.

    For migration from PDFBox 1.* to 2.0, take a look at the official migration guide.

提交回复
热议问题