Is it possible to modify PDF Form Field Names?

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

    Although you can't rename fields using javascript you can add new fields and delete existing fields. You can also cut and paste between documents. So...

    One. Develop renaming script, e.g.:

        var doc = app.activeDocs[0];
        var fnames = new Array();
        for ( var i = 0; i < doc.numFields - 1; i++) {      
            fnames[i] = doc.getNthFieldName(i);
        }
        for (var i = 0; i < doc.numFields - 1; i++){        
            var f = doc.getField(fnames[i] + ".0");
            var nfn = fnames[i].replace("1","2");
            var rb = doc.addField(nfn,"radiobutton",0,f.rect)
            for ( var j = 1; j < 9; j++){//Add the other 8
                f = doc.getField(fnames[i] + "." + j);
                doc.addField(nfn,"radiobutton",0,f.rect)
            }
            rb.setExportValues([1,2,3,4,5,6,7,8,9]);
            rb.borderStyle = f.borderStyle;
            rb.strokeColor = f.strokeColor;
            rb.fillColor = f.fillColor;
            doc.removeField(fnames[i]);
            console.println(fnames[i] + " to " + nfn);
        }
    

    Notes:
    Renaming fields may change order of fields for getNthFieldName, so get these first.
    A group of radiobuttons is one field (getField("Groupname")), to get the n'th in the same group use getField("Groupname.n"), you need these for the position rectangle. Properties which apply to all can be set enmase.
    addField parameters are: field name, field type, page, position rectangle)
    In the example there are 9 radiobuttons in each group

    Two. Cut and paste the fields you want to rename to a blank pdf (I've assumed one page above). Run the script. Cut and paste back.

    You may need to change app.activeDocs[0] to app.activeDocs[1] or replace "doc" with "this" if keeping both documents open when running the script

    0 讨论(0)
  • 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]);
            }                   
        }
    }
    
    0 讨论(0)
  • 2021-02-06 06:41

    The full name of an AcroForm field isn't explicitly stored within a field. It's actually derived from a hierarchy of fields, with a dot delimited list of ancestors appearing on the left.

    Simply renaming a field from 'topmostSubform[0].Page1[0].Website_Address[0]' to 'WebsiteAddress' is therefore unlikely to produce a correct result.

    You'll find section 8.6.2 'Field Dictionaries' of the PDF reference provides a good explanation of how field naming works ;-)

    Basically, each field in an AcroForm is define by a dictionary, which may contain certain optional entries pertaining to a field's name.

    • Key '/T' specifies the partial name. In your question, 'topmostSubform[0]', 'Page1[0]', and Website_Address[0], all represent partial names.

    • Key '/TU' specifies an alternative 'user-friendly' name for fields, which can be used in place of the actual field name for identifying fields in a user interface.

    Instead of renaming the field in question, think about adding a /TU entry!

    The example below uses ABCpdf to iterate through all the fields in an AcroForm and insert an alternate name into a field based on its partial name.

    VBScript:

    Set theDoc = CreateObject("ABCpdf7.Doc")
    theDoc.Read "myForm.pdf"
    
    Dim theFieldIDs, theList
    theFieldIDs = theDoc.GetInfo(theDoc.Root, "Field IDs")
    theList = Split(theFieldIDs, ",")
    
    For Each fieldID In theList
        thePartialName = theDoc.GetInfo(fieldID, "/T:text")
        theDoc.SetInfo fieldID, "/TU:text", thePartialName
    Next
    
    theDoc.Save "output.pdf"
    theDoc.Clear
    

    Changing "/TU:text" to "/T:text" will set a field's partial name.

    Examples written in C# and VB.NET of the functions used can be found here: Doc.GetInfo, Doc.SetInfo. See also the documentation on Object Paths.

    0 讨论(0)
提交回复
热议问题