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

后端 未结 11 2205
难免孤独
难免孤独 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:07

    I don't have enough points to comment but SJohnson's response of setting the field to read only worked perfectly for me. I am using something like this with PDFBox:

    private void setFieldValueAndFlatten(PDAcroForm form, String fieldName, String fieldValue) throws IOException {
        PDField field = form.getField(fieldName);
        if(field != null){
            field.setValue(fieldValue);
            field.setReadonly(true);
        }
    }
    

    This will write your field value and then when you open the PDF after saving it will have your value and not be editable.

提交回复
热议问题