Adding existing page form fields to new document.
Posted: Mon Oct 11, 2021 10:05 pm
Hi,
I am trying to create a new document using a page from an existing document and add the form fields from the page to the Interactive form.
When I add the page, I would expect that the form fields would be added to the Interactive form of the new document. This is not happening.
I tried to manually add the form fields, but that does not work either.
I must be doing this wrong, but I am not able to figure out the correct way to add the field. Any help would be appreciated.
I am trying to create a new document using a page from an existing document and add the form fields from the page to the Interactive form.
When I add the page, I would expect that the form fields would be added to the Interactive form of the new document. This is not happening.
I tried to manually add the form fields, but that does not work either.
I must be doing this wrong, but I am not able to figure out the correct way to add the field. Any help would be appreciated.
Code: Select all
using (PdfDocument pageDocument = new PdfDocument())
{
// create interactive form in PDF document
pageDocument.InteractiveForm = new PdfDocumentInteractiveForm(pageDocument);
// Get the first page from the source document.
PdfPage sourcePage = sourceDocument.Pages[0];
// Get the list of interactive form fields.
PdfInteractiveFormField[] fieldList = sourceDocument.InteractiveForm.GetFieldsLocatedOnPage(sourcePage);
// Add the first page of the newly created document. I would expect that the form fields would be added.
pageDocument.Pages.Add(sourcePage);
// Try to add the form fields to the Interactive form
foreach(PdfInteractiveFormField formField in fieldList)
{
// Remove field from the source document.
sourceDocument.InteractiveForm.RemoveField(formField);
// Error when adding the field.
pageDocument.InteractiveForm.AddField(formField, pageDocument.Pages.Last());
}
// Remove the first page from the source document.
sourceDocument.Pages.Remove(sourcePage);
...
...
}