Rename form field names on pdf pages that are cloned from existing document

We have a pdf document coded with Adobe Acrobat DC with form fields that will be used as a base template. We would like to clone certain pages of it multiple times based on the data we are going to fill them.

The form field names on the pages are created to a convention. For example, a field names that ends with a period following by number (fieldName.0) corresponds to data from first row.

We were able to add multiple pages but need help on retrieving form fields from added page and retrieve/rename field names on those pages.

Below is a sample code snippet:

        var sourcePdf = "Test.pdf";
        var pdfTemplatePath = Path.GetDirectoryName(sourcePdf); 

        var testFile = Path.Combine(pdfTemplatePath, sourcePdf);

        var doc = new Document(testFile);
        // Page to be copied - Page 4
        var pageToAdd = doc.Pages[4];

        var newpdf = Path.Combine(pdfTemplatePath, "TestNew.pdf");
        // Add as many times as needed
        var numPages = 2;
        for (int i = 0; i < numPages; i++)
        {
            doc.Pages.Add(pageToAdd);
        }

        // TODO: Retrieve form fields that are ending with .0 on the added page
        // and rename them by incrementing by 1
        // Example: If tempalte page (Page 4) has fieldName.0 then added page to have fieldName.1

        doc.Save(newpdf);

Appreciate your timely response.

@hemanthtaduri

Thank you for contacting support.

We would like to suggest you to avoid using period because renaming a field with period in its name causes problem, which is already logged in our issue management system as PDFNET-39366. You may consider different naming convention in your base template to avoid this issue. Below code snippet is a demonstration how form fields can be renamed with Aspose.PDF for .NET API.

    FileStream Stream = new FileStream(dataDir + "input.pdf", FileMode.Open);
    Form form = new Aspose.Pdf.Facades.Form(Stream);

    string[] allfields  = form.FieldNames;

    for (int counter = 0; counter < form.FieldNames.Length; counter++)
    {
        form.RenameField(allfields[counter], "TEST " + (counter+1));
    }
    form.Save(dataDir + "Test_18.10.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you for response @Farhan.Raza

We have lot of existing pdfs that are already created to the format mentioned earlier (dot sequences: field.0, field.1…field.n) and we have data generated and saved to this format so that the stamping process is convenient without any code modifications.

Is there any alternate approach or helper classes or utilities that are in pipeline to be developed that may help to increment the dotsequence numbers only upon adding the template page multiple times.

Thank you.

@hemanthtaduri

We are afraid any workaround for this problem may not be shared until the issue is investigated further. We will let you know as soon as some significant updates will be available in this regard. We appreciate your patience and comprehension in this regard.