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

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

    If the PDF document doesn't actually contain form fields but you still want to flatten other elements like markups, the following works quite well. FYI It was implemented for C#

        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();
        }
    

    This effectively locks all markups in the document and they will no longer be editable.

    pdfbox c# annotations

提交回复
热议问题