Rename fields in pdf document

Hello,

I am trying to rename fields in a pdfdocument using the following C#
code:

byte[] file2 = RenameFields(File.ReadAllBytes(@"C:\Temp
\renameTest.pdf"), "_testing");
File.WriteAllBytes(@"C:\Temp\renamed.pdf", file2);

public byte[] RenameFields(byte[] source, string postfix)
    {
        using (PDFDoc PDFDocument = new PDFDoc(source, source.Length))
        {
            byte[] finalPdf = null;
            int finalSize = 0;

            PDFDocument.InitSecurityHandler();
            int pageCount = PDFDocument.GetPageCount();

            for (int pageNo = 1; pageNo <= pageCount; pageNo++)
            {
                pdftron.PDF.Page page = PDFDocument.GetPage(pageNo);
                if (page != null)
                {
                    //Get number of controls on the page
                    int fieldCount = page.GetNumAnnots();

                    //loop through all those fields...
                    for (int loop = 0; loop < fieldCount; loop++)
                    {
                        Annot annot = page.GetAnnot(loop);

                        if (!annot.IsValid())
                            continue;

                        //if it is valid widget control
                        if (annot.GetType() == Annot.Type.e_Widget)
                        {
                            pdftron.PDF.Annots.Widget w = new
pdftron.PDF.Annots.Widget(annot);
                            Field field = w.GetField();

                            if (field != null)
                            {
                               field.Rename(field.GetName() +
postfix);
                            }
                        }
                    }
                }
            }
            PDFDocument.RefreshFieldAppearances();

            PDFDocument.Save(ref finalPdf, ref finalSize,
pdftron.SDF.SDFDoc.SaveOptions.e_linearized);
            return finalPdf;
        }
    }

Question I have is, above code renames all the fields but it doesn’t
retain value of the fields. Data in the each field is lost. Can you
let me know the proper way to rename all fields in the PDF with their
values intact.

Thanks

Thanks for reporting this issue. It was fixed and will be included as part of the next update (v.5.8).

An interim solution is to copy the old value explicitly:

Obj val = field.GetValue();

field.Rename(…);

field.GetSDFObj.Put(“V”, val);