Adding existing page form fields to new document.

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Moderator: Alex

Post Reply
IntegraHarlan
Posts: 78
Joined: Fri Jan 24, 2020 3:37 am

Adding existing page form fields to new document.

Post by IntegraHarlan »

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.

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);
					
		...
		...
	}
Alex
Site Admin
Posts: 1986
Joined: Thu Jul 10, 2008 2:21 pm

Re: Adding existing page form fields to new document.

Post by Alex »

Hi Harlan,

If you need to copy page with interactive fields from one PDF document to another, you need to use Vintasoft.Imaging.Pdf.Processing.PdfDocumentCopyCommand class. Please see example here: https://www.vintasoft.com/docs/vsimagin ... mmand.html

Best regards, Alexander
IntegraHarlan
Posts: 78
Joined: Fri Jan 24, 2020 3:37 am

Re: Adding existing page form fields to new document.

Post by IntegraHarlan »

Hi Alex, Thanks you for your reply.

If I understand your response correctly, If I want to copy a page from one document to another document and add the form fields, I will need to get the page from the document I am copying the page from and create a new document then add the page?
Then use the copy document class to copy the newly created document to the document I want to add the page to?

Thanks
Alex
Site Admin
Posts: 1986
Joined: Thu Jul 10, 2008 2:21 pm

Re: Adding existing page form fields to new document.

Post by Alex »

Hi Harlan,

No, you do not need to create temporary document.

You need to do the following steps:
  • Open source PDF document.
  • For each unnecessary page in source PDF document:
    • Delete interactive fields, which are located on unnecessary page.
    • Delete unnecessary page.
  • Copy source PDF document (with 1 page) to the destination PDF document using Vintasoft.Imaging.Pdf.Processing.PdfDocumentCopyCommand class.
  • Close source PDF document without saving.
Best regards, Alexander
Post Reply