Is it possible to modify PDF Form Field Names?

前端 未结 9 2173
一个人的身影
一个人的身影 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:31

    I had the problem yesterday and after trying out answers in the forum and others but made no headway. My code looked like this.

    // Open up the file and read the fields on it.
    var pdfReader = new PdfReader(PATH_TO_PDF);
    var fs = new FileStream(pdfFilename, FileMode.Create, FileAccess.ReadWrite)
    var stamper = new PdfStamper(pdfReader, fs);
    var pdfFields = stamper.AcroFields;
    
    //I thought this next line of code I commented out will do it
    //pdfFields.RenameField("currentFieldName", "newFieldName");
    
    // It did for some fields, but returned false for others.
    // Then I looked at the AcroFields.RenameField method in itextSharp source and noticed some restrictions. You may want to do the same.
    //  So I replaced that line  pdfFields.RenameField(currentFieldName, newFieldName); with these 5 lines
    
    AcroFields.Item item = pdfFields.Fields[currentFieldName];
    PdfString ss = new PdfString(newFieldName, PdfObject.TEXT_UNICODE);
    item.WriteToAll(PdfName.T, ss, AcroFields.Item.WRITE_VALUE | AcroFields.Item.WRITE_MERGED);
    item.MarkUsed(pdfFields, AcroFields.Item.WRITE_VALUE);
    pdfFields.Fields[newFieldName] = item;
    

    And that did the job

提交回复
热议问题