Is it possible to modify PDF Form Field Names?

前端 未结 9 2171
一个人的身影
一个人的身影 2021-02-06 06:05

Here\'s the situation. I have a PDF with automatically generated pdf form field names. The problem is that these names are not very user friendly. They look something like :

9条回答
  •  误落风尘
    2021-02-06 06:39

    This worked for me using iText 7:

    PdfReader reader = new PdfReader("Source.pdf");
    
    using (FileStream fs = new FileStream("Dest.pdf", FileMode.Create))
    {
        using (var pdfDoc = new PdfDocument(reader, new PdfWriter(fs)))
        {
            PdfAcroForm pdfForm = PdfAcroForm.GetAcroForm(pdfDoc, true);
            pdfDoc.GetWriter().SetCloseStream(true);
            
            var Fields = pdfForm.GetFormFields().Select(x => x.Key).ToArray();
            
            for (int i = 0; i < Fields.Length; i++)
            {
                pdfForm.RenameField(Fields[i], "new_" + Fields[i]);
            }                   
        }
    }
    

提交回复
热议问题