Change a field name

Try to change the name of a form field.
Used this code:

var pdf=new Document(@"C:\tmp\a.pdf");
var field=(TextBoxField)pdf.Form[1];
field.Name="gorilla";
pdf.Save(@"C:\tmp\b.pdf");

But the field name doesn’t get changed. In b.pdf, the field name is still the original name.

@ecymerman
Is it possible for you to share the document you mentioned to check the issue?

a.pdf (6.1 KB)

@ecymerman
I tried the following code

            var input = InputFolder + "field_change_name_issue.pdf";
            var output = OutputFolder + "field_change_name_issue_out.pdf";

            // Open PDF document
            using (var document = new Aspose.Pdf.Document(input))
            {
                var field = (TextBoxField)document.Form.Fields[0];
                field.Name = "gorilla";
                document.Save(output);
            }

            // Open PDF document
            using (var document = new Aspose.Pdf.Document(output))
            {
                var field = (TextBoxField)document.Form.Fields[0];
                Console.WriteLine(field.Name == "gorilla");
            }

and it returns True
Screenshot_1.png (2.5 KB)

The thing is that name that is shown in result document is a FullName property and it isn’t changeable, it’s read only

True, but when I open it in Acrobat, it still shows Text1 as the name.
Examing the field dictionary using Preflight, shows this:
image.png (3.6 KB)

It is important to change T in addition to NM, as most software (like Acrobat & iText) use T for the name.

@ecymerman
Okay, apparently there’s a way to change FullName


            using (var editor = new Aspose.Pdf.Facades.FormEditor())
            {
                // Bind PDF document
                editor.BindPdf(input);
                // Rename the field "textbox1" to "FullName"
                editor.RenameField("Text1", "gorilla");
                // Save the modified PDF document
                editor.Save(output);
            }

            // Open PDF document
            using (var document = new Aspose.Pdf.Document(output))
            {
                var field = (TextBoxField)document.Form.Fields[0];
                Console.WriteLine(field.FullName == "gorilla");
            }

In case field name is changed via FormEditor its FullName is affected and the result is as you expected
Screenshot_1.png (3.4 KB)

Thank you, that worked perfectly.

1 Like

@ecymerman
Turns out you can change field name using following code:

using (var document = new Aspose.Pdf.Document(dataDir + "a.pdf"))
{
    var field = (TextBoxField)document.Form.Fields[0];
    field.PartialName = "gorilla";
    document.Save(dataDir + "a_out.pdf");
}

// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "a_out.pdf"))
{
    var field = (TextBoxField)document.Form.Fields[0];
    Console.WriteLine(field.FullName == "gorilla");
}

The key difference is that you change PartialName, not Name/FullName