Java PDFBox:如何;“展平”;PDF格式?

Java PDFBox:如何;“展平”;PDF格式?,java,pdfbox,pdf-form,c#,annotations,Java,Pdfbox,Pdf Form,C#,Annotations,如何使用PDFBox“展平”PDF表单(删除表单字段,但保留字段文本) 一种快速的方法是从acrofrom中删除字段 为此,您只需要获取文档目录,然后是acroform 然后从此窗体中删除所有字段 图形表示与注释链接并保持不变 文件 所以我写了这段代码: import java.io.File; import java.util.ArrayList; import java.util.List; import org.apache.pdfbox.pdmodel.PDDocument; imp

如何使用PDFBox“展平”PDF表单(删除表单字段,但保留字段文本)

一种快速的方法是从acrofrom中删除字段

为此,您只需要获取文档目录,然后是acroform 然后从此窗体中删除所有字段

图形表示与注释链接并保持不变 文件

所以我写了这段代码:

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

public class PdfBoxTest {
    public void test() throws Exception {
        PDDocument pdDoc = PDDocument.load(new File("E:\\Form-Test.pdf"));
        PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
        PDAcroForm acroForm = pdCatalog.getAcroForm();

        if (acroForm == null) {
            System.out.println("No form-field --> stop");
            return;
        }

        @SuppressWarnings("unchecked")
        List<PDField> fields = acroForm.getFields();

        // set the text in the form-field <-- does work
        for (PDField field : fields) {
            if (field.getFullyQualifiedName().equals("formfield1")) {
                field.setValue("Test-String");
            }
        }

        // remove form-field but keep text ???
        // acroForm.getFields().clear();         <-- does not work
        // acroForm.setFields(null);             <-- does not work
        // acroForm.setFields(new ArrayList());  <-- does not work
        // ???

        pdDoc.save("E:\\Form-Test-Result.pdf");
        pdDoc.close();
    }
}
导入java.io.File;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.pdfbox.pdmodel.PDDocument;
导入org.apache.pdfbox.pdmodel.PDDocumentCatalog;
导入org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
导入org.apache.pdfbox.pdmodel.interactive.form.PDField;
公共类PdfBoxTest{
public void test()引发异常{
PDDocument pdDoc=PDDocument.load(新文件(“E:\\Form Test.pdf”);
PDDocumentCatalog pdCatalog=pdDoc.getDocumentCatalog();
PDAcroForm acroForm=pdCatalog.getAcroForm();
if(acroForm==null){
System.out.println(“无表单字段-->停止”);
返回;
}
@抑制警告(“未选中”)
列表字段=acroForm.getFields();

//在表单字段中设置文本这是Thomas的答案,来自PDFBox邮件列表:

您需要在COSDictionary上获取字段。请尝试此操作 代码


setReadOnly对我有效,如下所示-

   @SuppressWarnings("unchecked")
    List<PDField> fields = acroForm.getFields();
    for (PDField field : fields) {
        if (field.getFullyQualifiedName().equals("formfield1")) {
            field.setReadOnly(true);
        }
    }
@SuppressWarnings(“未选中”)
列表字段=acroForm.getFields();
用于(PDField:字段){
if(field.getFullyQualifiedName().equals(“formfield1”)){
field.setReadOnly(true);
}
}

这肯定有效-我遇到了这个问题,调试了一整晚,但最终找到了解决方法:)

这是假设您有能力以某种方式编辑PDF/对PDF有某种控制

首先,使用Acrobat Pro编辑表单。将其设置为隐藏和只读

然后需要使用两个库:PDFBox和PDFClown

PDFBox删除告诉Adobe Reader它是一个表单的内容;PDFClown删除实际字段。必须先完成PDFClown,然后再完成PDFBox(按此顺序。另一种方法不起作用)

单字段示例代码:

// PDF Clown code
File file = new File("Some file path"); 
Document document = file.getDocument();
Form form = file.getDocument.getForm();
Fields fields = form.getFields();
Field field = fields.get("some_field_name");

PageStamper stamper = new PageStamper(); 
FieldWidgets widgets = field.getWidgets();
Widget widget = widgets.get(0); // Generally is 0.. experiment to figure out
stamper.setPage(widget.getPage());

// Write text using text form field position as pivot.
PrimitiveComposer composer = stamper.getForeground();
Font font = font.get(document, "some_path"); 
composer.setFont(font, 10); 
double xCoordinate = widget.getBox().getX();
double yCoordinate = widget.getBox().getY(); 
composer.showText("text i want to display", new Point2D.Double(xCoordinate, yCoordinate)); 

// Actually delete the form field!
field.delete();
stamper.flush(); 

// Create new buffer to output to... 
Buffer buffer = new Buffer();
file.save(buffer, SerializationModeEnum.Standard); 
byte[] bytes = buffer.toByteArray(); 

// PDFBox code
InputStream pdfInput = new ByteArrayInputStream(bytes);
PDDocument pdfDocument = PDDocument.load(pdfInput);

// Tell Adobe we don't have forms anymore.
PDDocumentCatalog pdCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = pdCatalog.getAcroForm();
COSDictionary acroFormDict = acroForm.getDictionary();
COSArray cosFields = (COSArray) acroFormDict.getDictionaryObject("Fields");
cosFields.clear();

// Phew. Finally.
pdfDocument.save("Some file path");

可能有一些打字错误,但这应该足以说明要点:)

这是我在综合了我能找到的关于这个主题的所有答案后得出的代码。这可以处理文本框、组合框、列表、复选框和收音机的扁平化:

public static void flattenPDF (PDDocument doc) throws IOException {

    //
    //  find the fields and their kids (widgets) on the input document
    //  (each child widget represents an appearance of the field data on the page, there may be multiple appearances)
    //
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDAcroForm form = catalog.getAcroForm();
    List<PDField> tmpfields = form.getFields();
    PDResources formresources = form.getDefaultResources();
    Map formfonts = formresources.getFonts();
    PDAnnotation ann;

    //
    // for each input document page convert the field annotations on the page into
    // content stream
    //
    List<PDPage> pages = catalog.getAllPages();
    Iterator<PDPage> pageiterator = pages.iterator();
    while (pageiterator.hasNext()) {
        //
        // get next page from input document
        //
        PDPage page = pageiterator.next();

        //
        // add the fonts from the input form to this pages resources
        // so the field values will display in the proper font
        //
        PDResources pageResources = page.getResources();
        Map pageFonts = pageResources.getFonts();
        pageFonts.putAll(formfonts);
        pageResources.setFonts(pageFonts);

        //
        // Create a content stream for the page for appending
        //
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);

        //
        // Find the appearance widgets for all fields on the input page and insert them into content stream of the page
        //
        for (PDField tmpfield : tmpfields) {
            List widgets = tmpfield.getKids();
            if(widgets == null) {
                widgets = new ArrayList();
                widgets.add(tmpfield.getWidget());
            }
            Iterator<COSObjectable> widgetiterator = widgets.iterator();
            while (widgetiterator.hasNext()) {
                COSObjectable next = widgetiterator.next();
                if (next instanceof PDField) {
                    PDField foundfield = (PDField) next;
                    ann = foundfield.getWidget();
                } else {
                    ann = (PDAnnotation) next;
                }
                if (ann.getPage().equals(page)) {
                    COSDictionary dict = ann.getDictionary();
                    if (dict != null) {
                        if(tmpfield instanceof PDVariableText || tmpfield instanceof PDPushButton) {
                            COSDictionary ap = (COSDictionary) dict.getDictionaryObject("AP");
                            if (ap != null) {

                                contentStream.appendRawCommands("q\n");
                                COSArray rectarray = (COSArray) dict.getDictionaryObject("Rect");
                                if (rectarray != null) {
                                    float[] rect = rectarray.toFloatArray();
                                    String s = " 1 0 0 1  " + Float.toString(rect[0]) + " " + Float.toString(rect[1]) + " cm\n";

                                    contentStream.appendRawCommands(s);
                                }
                                COSStream stream = (COSStream) ap.getDictionaryObject("N");
                                if (stream != null) {
                                    InputStream ioStream = stream.getUnfilteredStream();
                                    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                    byte[] buffer = new byte[4096];
                                    int amountRead = 0;
                                    while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                        byteArray.write(buffer, 0, amountRead);
                                    }

                                    contentStream.appendRawCommands(byteArray.toString() + "\n");
                                }

                                contentStream.appendRawCommands("Q\n");
                            }
                        } else if (tmpfield instanceof PDChoiceButton) {
                            COSDictionary ap = (COSDictionary) dict.getDictionaryObject("AP");
                            if(ap != null) {
                                contentStream.appendRawCommands("q\n");
                                COSArray rectarray = (COSArray) dict.getDictionaryObject("Rect");
                                if (rectarray != null) {
                                    float[] rect = rectarray.toFloatArray();
                                    String s = " 1 0 0 1  " + Float.toString(rect[0]) + " " + Float.toString(rect[1]) + " cm\n";

                                    contentStream.appendRawCommands(s);
                                }

                                COSName cbValue = (COSName) dict.getDictionaryObject(COSName.AS);
                                COSDictionary d = (COSDictionary) ap.getDictionaryObject(COSName.D);
                                if (d != null) {
                                    COSStream stream = (COSStream) d.getDictionaryObject(cbValue);
                                    if(stream != null) {
                                        InputStream ioStream = stream.getUnfilteredStream();
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                        byte[] buffer = new byte[4096];
                                        int amountRead = 0;
                                        while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                            byteArray.write(buffer, 0, amountRead);
                                        }

                                        if (!(tmpfield instanceof PDCheckbox)){
                                            contentStream.appendRawCommands(byteArray.toString() + "\n");
                                        }
                                    }
                                }

                                COSDictionary n = (COSDictionary) ap.getDictionaryObject(COSName.N);
                                if (n != null) {
                                    COSStream stream = (COSStream) n.getDictionaryObject(cbValue);
                                    if(stream != null) {
                                        InputStream ioStream = stream.getUnfilteredStream();
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                        byte[] buffer = new byte[4096];
                                        int amountRead = 0;
                                        while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                            byteArray.write(buffer, 0, amountRead);
                                        }

                                        contentStream.appendRawCommands(byteArray.toString() + "\n");
                                    }
                                }

                                contentStream.appendRawCommands("Q\n");
                            }
                        }
                    }
                }
            }
        }

        // delete any field widget annotations and write it all to the page
        // leave other annotations on the page
        COSArrayList newanns = new COSArrayList();
        List anns = page.getAnnotations();
        ListIterator annotiterator = anns.listIterator();
        while (annotiterator.hasNext()) {
            COSObjectable next = (COSObjectable) annotiterator.next();
            if (!(next instanceof PDAnnotationWidget)) {
                newanns.add(next);
            }
        }

        page.setAnnotations(newanns);
        contentStream.close();
    }

    //
    // Delete all fields from the form and their widgets (kids)
    //
    for (PDField tmpfield : tmpfields) {
        List kids = tmpfield.getKids();
        if(kids != null) kids.clear();
    }

    tmpfields.clear();

    // Tell Adobe we don't have forms anymore.
    PDDocumentCatalog pdCatalog = doc.getDocumentCatalog();
    PDAcroForm acroForm = pdCatalog.getAcroForm();
    COSDictionary acroFormDict = acroForm.getDictionary();
    COSArray cosFields = (COSArray) acroFormDict.getDictionaryObject("Fields");
    cosFields.clear();
}
publicstaticpdf(pddocumentdoc)抛出IOException{
//
//在输入文档中查找字段及其子项(小部件)
//(每个子窗口小部件表示页面上字段数据的外观,可能有多个外观)
//
PDDocumentCatalog=doc.getDocumentCatalog();
PDAcroForm form=catalog.getAcroForm();
List tmpfields=form.getFields();
PDResources formresources=form.getDefaultResources();
Map formfonts=formresources.getFonts();
pdann;
//
//对于每个输入文档页面,将页面上的字段注释转换为
//内容流
//
列表页面=catalog.getAllPages();
迭代器pageiterator=pages.Iterator();
while(pageiterator.hasNext()){
//
//从输入文档获取下一页
//
PDPage=pageiterator.next();
//
//将输入表单中的字体添加到此页面资源
//因此,字段值将以正确的字体显示
//
PDResources pageResources=page.getResources();
Map pageFonts=pageResources.getFonts();
pageFonts.putAll(formfonts);
设置字体(pageFonts);
//
//为页面创建内容流以进行附加
//
PDPageContentStream contentStream=新的PDPageContentStream(doc,page,true,true);
//
//查找输入页面上所有字段的外观小部件,并将它们插入页面的内容流中
//
用于(PDField tmpfield:tmpfields){
List widgets=tmpfield.getKids();
if(widgets==null){
widgets=newarraylist();
add(tmpfield.getWidget());
}
Iterator widgetiterator=widgets.Iterator();
while(widgetiterator.hasNext()){
CoObjectable next=widgetiterator.next();
如果(下一个实例为PDField){
PDField foundfield=(PDField)next;
ann=foundfield.getWidget();
}否则{
ann=(PDAnnotation)next;
}
如果(ann.getPage().equals(page)){
COSDictionary dict=ann.getDictionary();
if(dict!=null){
if(PDVariableText的tmpfield instanceof PDVariableText | | tmpfield instanceof PdButton){
COSDictionary ap=(COSDictionary)dict.getDictionaryObject(“ap”);
如果(ap!=null){
contentStream.appendRawCommands(“q\n”);
COSArray rectary=(COSArray)dict.getDictionaryObject(“Rect”);
if(rectary!=null){
float[]rect=rectary.toFloatArray();
字符串s=“1 0 0 1”+Float.toString(rect[0])+”“+Float.toString(rect[1])+“cm\n”;
contentStream.appendraw命令;
}
costream=(costream)ap.getDictionaryObject(“N”);
if(流!=null){
InputStream ioStream=stream.getUnfilteredStream();
public static void flattenPDF (PDDocument doc) throws IOException {

    //
    //  find the fields and their kids (widgets) on the input document
    //  (each child widget represents an appearance of the field data on the page, there may be multiple appearances)
    //
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDAcroForm form = catalog.getAcroForm();
    List<PDField> tmpfields = form.getFields();
    PDResources formresources = form.getDefaultResources();
    Map formfonts = formresources.getFonts();
    PDAnnotation ann;

    //
    // for each input document page convert the field annotations on the page into
    // content stream
    //
    List<PDPage> pages = catalog.getAllPages();
    Iterator<PDPage> pageiterator = pages.iterator();
    while (pageiterator.hasNext()) {
        //
        // get next page from input document
        //
        PDPage page = pageiterator.next();

        //
        // add the fonts from the input form to this pages resources
        // so the field values will display in the proper font
        //
        PDResources pageResources = page.getResources();
        Map pageFonts = pageResources.getFonts();
        pageFonts.putAll(formfonts);
        pageResources.setFonts(pageFonts);

        //
        // Create a content stream for the page for appending
        //
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);

        //
        // Find the appearance widgets for all fields on the input page and insert them into content stream of the page
        //
        for (PDField tmpfield : tmpfields) {
            List widgets = tmpfield.getKids();
            if(widgets == null) {
                widgets = new ArrayList();
                widgets.add(tmpfield.getWidget());
            }
            Iterator<COSObjectable> widgetiterator = widgets.iterator();
            while (widgetiterator.hasNext()) {
                COSObjectable next = widgetiterator.next();
                if (next instanceof PDField) {
                    PDField foundfield = (PDField) next;
                    ann = foundfield.getWidget();
                } else {
                    ann = (PDAnnotation) next;
                }
                if (ann.getPage().equals(page)) {
                    COSDictionary dict = ann.getDictionary();
                    if (dict != null) {
                        if(tmpfield instanceof PDVariableText || tmpfield instanceof PDPushButton) {
                            COSDictionary ap = (COSDictionary) dict.getDictionaryObject("AP");
                            if (ap != null) {

                                contentStream.appendRawCommands("q\n");
                                COSArray rectarray = (COSArray) dict.getDictionaryObject("Rect");
                                if (rectarray != null) {
                                    float[] rect = rectarray.toFloatArray();
                                    String s = " 1 0 0 1  " + Float.toString(rect[0]) + " " + Float.toString(rect[1]) + " cm\n";

                                    contentStream.appendRawCommands(s);
                                }
                                COSStream stream = (COSStream) ap.getDictionaryObject("N");
                                if (stream != null) {
                                    InputStream ioStream = stream.getUnfilteredStream();
                                    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                    byte[] buffer = new byte[4096];
                                    int amountRead = 0;
                                    while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                        byteArray.write(buffer, 0, amountRead);
                                    }

                                    contentStream.appendRawCommands(byteArray.toString() + "\n");
                                }

                                contentStream.appendRawCommands("Q\n");
                            }
                        } else if (tmpfield instanceof PDChoiceButton) {
                            COSDictionary ap = (COSDictionary) dict.getDictionaryObject("AP");
                            if(ap != null) {
                                contentStream.appendRawCommands("q\n");
                                COSArray rectarray = (COSArray) dict.getDictionaryObject("Rect");
                                if (rectarray != null) {
                                    float[] rect = rectarray.toFloatArray();
                                    String s = " 1 0 0 1  " + Float.toString(rect[0]) + " " + Float.toString(rect[1]) + " cm\n";

                                    contentStream.appendRawCommands(s);
                                }

                                COSName cbValue = (COSName) dict.getDictionaryObject(COSName.AS);
                                COSDictionary d = (COSDictionary) ap.getDictionaryObject(COSName.D);
                                if (d != null) {
                                    COSStream stream = (COSStream) d.getDictionaryObject(cbValue);
                                    if(stream != null) {
                                        InputStream ioStream = stream.getUnfilteredStream();
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                        byte[] buffer = new byte[4096];
                                        int amountRead = 0;
                                        while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                            byteArray.write(buffer, 0, amountRead);
                                        }

                                        if (!(tmpfield instanceof PDCheckbox)){
                                            contentStream.appendRawCommands(byteArray.toString() + "\n");
                                        }
                                    }
                                }

                                COSDictionary n = (COSDictionary) ap.getDictionaryObject(COSName.N);
                                if (n != null) {
                                    COSStream stream = (COSStream) n.getDictionaryObject(cbValue);
                                    if(stream != null) {
                                        InputStream ioStream = stream.getUnfilteredStream();
                                        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
                                        byte[] buffer = new byte[4096];
                                        int amountRead = 0;
                                        while ((amountRead = ioStream.read(buffer, 0, buffer.length)) != -1) {
                                            byteArray.write(buffer, 0, amountRead);
                                        }

                                        contentStream.appendRawCommands(byteArray.toString() + "\n");
                                    }
                                }

                                contentStream.appendRawCommands("Q\n");
                            }
                        }
                    }
                }
            }
        }

        // delete any field widget annotations and write it all to the page
        // leave other annotations on the page
        COSArrayList newanns = new COSArrayList();
        List anns = page.getAnnotations();
        ListIterator annotiterator = anns.listIterator();
        while (annotiterator.hasNext()) {
            COSObjectable next = (COSObjectable) annotiterator.next();
            if (!(next instanceof PDAnnotationWidget)) {
                newanns.add(next);
            }
        }

        page.setAnnotations(newanns);
        contentStream.close();
    }

    //
    // Delete all fields from the form and their widgets (kids)
    //
    for (PDField tmpfield : tmpfields) {
        List kids = tmpfield.getKids();
        if(kids != null) kids.clear();
    }

    tmpfields.clear();

    // Tell Adobe we don't have forms anymore.
    PDDocumentCatalog pdCatalog = doc.getDocumentCatalog();
    PDAcroForm acroForm = pdCatalog.getAcroForm();
    COSDictionary acroFormDict = acroForm.getDictionary();
    COSArray cosFields = (COSArray) acroFormDict.getDictionaryObject("Fields");
    cosFields.clear();
}
void saveFieldValue( PDField field ) throws IOException
{
    PDDocument document = getDocument( field );
    // see PDField.getWidget()
    for( PDAnnotationWidget widget : getWidgets( field ) )
    {
        PDPage parentPage = getPage( widget );

        try (PDPageContentStream contentStream = new PDPageContentStream( document, parentPage, true, true ))
        {
            writeContent( contentStream, widget );
        }
    }
}

void writeContent( PDPageContentStream contentStream, PDAnnotationWidget widget )
        throws IOException
{
    PDAppearanceStream appearanceStream = getAppearanceStream( widget );
    PDXObject xobject = new PDXObjectForm( appearanceStream.getStream() );
    AffineTransform transformation = getPositioningTransformation( widget.getRectangle() );

    contentStream.drawXObject( xobject, transformation );
}
void removeWidget( PDAnnotationWidget widget ) throws IOException
{
    PDPage widgetPage = getPage( widget );
    List<PDAnnotation> annotations = widgetPage.getAnnotations();
    PDAnnotation deleteCandidate = getMatchingCOSObjectable( annotations, widget );
    if( deleteCandidate != null && annotations.remove( deleteCandidate ) )
        widgetPage.setAnnotations( annotations );
}
void removeFormfield( PDField field ) throws IOException
{
    PDAcroForm acroForm = field.getAcroForm();
    List<PDField> acroFields = acroForm.getFields();
    List<PDField> removeCandidates = getFields( acroFields, field.getPartialName() );
    if( removeAll( acroFields, removeCandidates ) )
        acroForm.setFields( acroFields );
}
 public static void makeAllWidgetsReadOnly(PDDocument pdDoc) throws IOException {

    PDDocumentCatalog catalog = pdDoc.getDocumentCatalog();

    PDAcroForm form = catalog.getAcroForm();

    List<PDField> acroFormFields = form.getFields();

    System.out.println(String.format("found %d acroFrom fields", acroFormFields.size()));

    for(PDField field: acroFormFields) {
        makeAcroFieldReadOnly(field);
    }
}

private static void makeAcroFieldReadOnly(PDField field) {

    field.getDictionary().setInt("Ff",1);

}
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);
    }
}
File myFile = new File("myFile.pdf");
PDDocument pdDoc = PDDocument.load(myFile);
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();

// set the NeedAppearances flag to false
pdAcroForm.setNeedAppearances(false);


field.setValue("new-value");

pdAcroForm.flatten();
pdDoc.save("myFlattenedFile.pdf");
pdDoc.close();
// correct the missing page link for the annotations
// Add the missing resources to the form
//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();
private static void flattenPDF(String src, String dst) throws IOException {
    PDDocument doc = PDDocument.load(new File(src));

    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDAcroForm acroForm = catalog.getAcroForm();
    PDResources resources = new PDResources();
    acroForm.setDefaultResources(resources);

    List<PDField> fields = new ArrayList<>(acroForm.getFields());
    processFields(fields, resources);
    acroForm.flatten();

    doc.save(dst);
    doc.close();
}

private static void processFields(List<PDField> fields, PDResources resources) {
    fields.stream().forEach(f -> {
        f.setReadOnly(true);
        COSDictionary cosObject = f.getCOSObject();
        String value = cosObject.getString(COSName.DV) == null ?
                       cosObject.getString(COSName.V) : cosObject.getString(COSName.DV);
        System.out.println("Setting " + f.getFullyQualifiedName() + ": " + value);
        try {
            f.setValue(value);
        } catch (IOException e) {
            if (e.getMessage().matches("Could not find font: /.*")) {
                String fontName = e.getMessage().replaceAll("^[^/]*/", "");
                System.out.println("Adding fallback font for: " + fontName);
                resources.put(COSName.getPDFName(fontName), PDType1Font.HELVETICA);
                try {
                    f.setValue(value);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            } else {
                e.printStackTrace();
            }
        }
        if (f instanceof PDNonTerminalField) {
            processFields(((PDNonTerminalField) f).getChildren(), resources);
        }
    });
}
    public static void FlattenPdf(string fileName)
            {
                PDDocument doc = PDDocument.load(new java.io.File(fileName));
    
                java.util.List annots = doc.getPage(0).getAnnotations();
                for (int i = 0; i < annots.size(); ++i)
                {
                    PDAnnotation annot = (PDAnnotation)annots.get(i);
                    annot.setLocked(true);
                    annot.setReadOnly(true);
                    annot.setNoRotate(true);
                }
    
                doc.save(fileName);
                doc.close();
    }