We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy form fields from one PDF to another

We deal with alot of PDFs where legal disclosures are constantly edited. These forms also have form fields on them. When we recieve a new form from our legal department they do not have form fields on them, but the layout of the form is exactly the same. We would like to automatically copy all of the form fields from the old document to the new one in an automated fashion. Can we do this with the Aspose.Pdf.Kit? If so I would love to see some code examples on how to accomplish this.

Dear ChadBoyer,

Thanks for considering Aspose.Pdf.Kit.

Honestly, there isn't any interface to meet your requirement. However, you can copy simple field through the following workaround:

string path = TestPath + @"gemini_6947\";
Form myForm = new Form(path + "FieldCopy.in.pdf");
String fieldName = "Button1";
//get field type
FieldType myType = myForm.GetFieldType(fieldName);
//get field value
String myValue = myForm.GetField(fieldName);
//get field appearance
FormFieldFacade myFacade = myForm.GetFieldFacade(fieldName);
myForm.Save();

FormEditor editor = new FormEditor(path + "Empty.pdf", path + "FieldCopy.out.pdf");
//create a new field using information above
editor.AddField(myType, fieldName, myValue, myFacade.PageNumber, myFacade.Box.Left, myFacade.Box.Bottom, myFacade.Box.Left + myFacade.Box.Width, myFacade.Box.Bottom + myFacade.Box.Height);
editor.Save();

However, this can only work with AcroForm Field. And information such as color, border ... will be lost. We are still working to develop new interface for this feature, but it won't be finished in a short time. Sorry for the inconvenience.

Best Regards.

Thank you for your example. The only problem with it is I don't have a property named "PageNumber" exposed on the FormFieldFascade class. I am using 3.3.0.0.

Dear ChadBoyer,

Sorry for my carelessness. That property is only included in the testing version, and it will be provided in the coming new release. As for your problem, please replace it with the actual page number in which page the field is located, such as 1, 2 or 3.

FormEditor.LocalCopyField( ) is already provided to copy field within a PDF document. We decide to provide new functions for remote field copying. Before that, please try the workaround.

Any other question is also welcomed. Best regards.

Chad and I work together. We have tried another approach to copy form fields from one PDF to another. I am having some issues though. I have attached some sample PDFs that work with the code I have pasted below.

Issue 1 - Textbox values:
The textboxes copy to the right place in the new PDF and even the size if fine, but when I try to type in the field all of the input stacks on top of each other within the textbox.

Issue 2 – RadioButtonList position:
When I copy over the RadioButtonList form control it creates the correct number of radio button items but it does not position them correctly on the screen. The first thing I noticed is that the entire list of radio buttons is treated as one form field name and is only represented once in the Form.FieldNames collection. This also means that I can only get to the FormFieldFacade of the container control and not the individual parts/radio button items. I did see the RadioHoriz and RadioGap on the FormEditor object from which I am using to add the field to the new PDF, but my problem is that the spacing on the individual radio button items is not consistent it needs to draw both across and down i.e. 2 columns and 3 rows. If I could somehow get to the individual radio button items in the Form and then be able to add a list of radio button items contained within a RadioButtonList field/control then I think this would solve this issue.

Issue 3 – RadioButtonList type/style:
The style of my radio button list items is a check instead of a circle but I cannot figure out how to set this.

string copyFieldsFromPdf = @"C:\Temp\FormFields\CopyFieldsFromPdf.pdf";
string copyToPdf = @"C:\Temp\FormFields\CopyToPdf.pdf";
string outputPdf = @"C:\Temp\FormFields\OutputPdf.pdf";

PdfFileInfo orriginalPdfFileInfo = new PdfFileInfo(copyFieldsFromPdf);
int orriginalNumberOfPages = orriginalPdfFileInfo.NumberofPages;

PdfFileInfo emptyPdfFileInfo = new PdfFileInfo(copyToPdf);
int emptyNumberOfPages = emptyPdfFileInfo.NumberofPages;

PdfFileEditor pdfFileEditor = new PdfFileEditor();
MemoryStream[] orriginalPdfPageStreams = pdfFileEditor.SplitToPages(copyFieldsFromPdf);

FormEditor formEditor = new FormEditor(copyToPdf, outputPdf);

for (int i = 0; i < orriginalNumberOfPages && i < emptyNumberOfPages; i++)
{
Aspose.Pdf.Kit.Form pdfForm = new Aspose.Pdf.Kit.Form(orriginalPdfPageStreams[i]);

foreach (string fieldName in pdfForm.FieldsNames)
{
    string fullName = pdfForm.GetFullFieldName(fieldName);
    FormFieldFacade facade = pdfForm.GetFieldFacade(fullName);

    float lowLeftX = facade.Box.Left;
    float lowLeftY = facade.Box.Bottom;
    float upRightX = facade.Box.Left + facade.Box.Width;
    float upRightY = facade.Box.Top;
    int pageNumber = i + 1;

    formEditor.Facade = facade;
    //formEditor.RadioHoriz;
    //formEditor.RadioGap
    formEditor.Items = facade.Items;
    formEditor.AddField(pdfForm.GetFieldType(fieldName), fieldName, pageNumber, lowLeftX, lowLeftY, upRightX, upRightY);
}

}

formEditor.Save();

Thanks,
Ryan

Dear Ryan,

What I introduced before is just a workaround, it only works for simple field creation. That is, the newly created field can only be specified with the location and an initial value. Those three issues cannot be satisfied by the workaround method.

Please describe your requirement in detail, so as we can develop new interface for field copying across different documents.

Best regards.

The driving factor behind our pursuing this functionality is that we have PDF forms that we use Acrobat Professional to draw a bunch of form fields on. We then receive updated forms on a regular basis where the text in PDF changes but the location of the form fields stays the same. These updated forms do not have form fields on them so we have to get the form fields from one of the old forms that we have already drawn the form fields on then copy them to the new form. Acrobat Professional only allows us to select a page worth of form fields at a time and many of the forms have multiple pages. This becomes a very time consuming process switching back and forth between the forms to copy over all of the form fields.

We would like to see the Aspose.Pdf.Kit control expose a method where we provide an old form with form fields on it and a new form that does not have form fields on it and it would then copy over every form field as it is on the old form to the new form. This would include radio button lists their display type and location of each individual radio button list item. To keep it simple this method would place an exact copy of all form fields in the old PDF on the new PDF. Keep in mind the new PDF already has text, images, etc. just no form fields.

Dear Ryan,

Thanks for your detailed description. We will investigate into it and try to support this feature :) But I am afraid the ETA will be one month.

Best regards.

This is a feature that is worth the wait for us!

Thanks,
Ryan

I was wondering if there had been any progress made on this feature?

Ryan

Hello Ryan,

Our development team is working hard to get this issue resolved, but as Luke have mentioned, the ETA for the resolution of this problem is on month. Your patience in this regard is highly appreciated.

Dear Ryan,

Thanks for your patience. Actually, this feature has been finished. It will be included in the next release after more careful testing.

If you need the function urgently, please let us know. We can provide you a trial version before the public release.

Best regards.

We would be very excited to test it out for you and provide feedback before you release the feature. We have quite a few PDF forms that we would use this on right away. How would we go about getting a test trial version?

Ryan

Dear ryan,

Our Product Manager will upload the trial version here soon. Please download it and test it.

FYI:
1. XfaForm documents are not supported.
2. Radio Button is different with other form types, we are looking for some easy ways to copy. Hope it will be included in the next coming formal release.
3. Three methods are introduced:
public void OuterCopyField(String srcFileName, String fieldName)
public void OuterCopyField(String srcFileName, String fieldName, int pageNum)
public void OuterCopyField(String srcFileName, String fieldName, int pageNum, float abscissa, float ordinate)
You can just copy the field to its original page & position, or you can specify its new page & position.

Best regards.

Hi,

The attachment includes a Beta dll(3.3.1.2) and the API documentation, please download and try it.

Thanks,

I tried the new CopyOuterField method on one of our pdf files. It seemed to copy the fields, but I was unable to type in the textboxes. Also when I would go to edit the form in Adobe Acrobat Professional and go to the properties of one of the textboxes Acrobat would crash. I have included a sample project and the PDF files I was working with so you can test it.

Thanks,
Ryan

Dear ryan,

From your code, I learned your requirement, but you misunderstood the usage of the method. As a new method, FormEditor.CopyOuterField() lacks of detailed API instruction.
1) With CopyOuterField(String srcFileName, String fieldName), a field will be copied from the original page & position to the same page & position in the output document.
2) With CopyOuterField(String srcFileName, String fieldName, int pageNum), a field will be copied from the original page & position to another page with the same position in the output document.
3) With CopyOuterField(String srcFileName, String fieldName, int pageNum, float abscissa, float ordinate), a field will be copied from the original page & position to the output document with specified page number and position.

So you needn't split original document to obtain each field's page number. I changed your button1_Click( ) functional coldes into the following lines:

FormEditor formEditor = new FormEditor(copyToPdfOrriginal , copyToPdfWorking );
Aspose.Pdf.Kit.Form pdfForm = new Aspose.Pdf.Kit.Form(copyFromPdf );
foreach (string fieldName in pdfForm.FieldsNames)
{
if (pdfForm.GetFieldType(fieldName) != FieldType.Radio)
{
formEditor.CopyOuterField(copyFromPdf , fieldName);
}
}
pdfForm.Save();
formEditor.Save();

I ran the example and get the required output, except that some check boxes are automatically selected. This will be fixed soon, and it will be included in the next release version, together with the support for radio button. Please try again and feel free to contact us when you still have problems.

Best regards.

Thank you for sending the updated code. I ran the new code and I was able to type in the text fields this time. I did some further testing and I ran into another situation that does not appear to work based on the code you sent me. Basically we have some PDF forms that have a form field with the same name on it multiple times. The situation is our forms may ask for a person's home address or some other piece of information multiple time on multiple pages of the document. If the form fields are the same name then when they fill out one field it will automatically populate the other fields of the same name throughout the document. I noticed in your sample that when I call CopyOuterField(String srcFileName, String fieldName) that it only copies the first form field of the matching name in the document. I had noticed this limitation before and this is why I was splitting the pages of the PDF document. I don't think we would ever put a form field of the same name twice on the same page. Would it be possible for you to make the CopyOuterField(String srcFileName, String fieldName) find all instances of a form field with the matching name and copy it to the corrisponding pages?

Thanks,
Ryan

Dear ryan,

As you known, a field can only have one unique field name in a common document . Frankly, your requirement can't be satisfied with the current CopyOuterField() methods. However, we are still considering to support this feature. In order to support duplicated field name, we have to cost more time, maybe another week. If possible, please share your PDF forms (with duplicated fields) to accelerate our developing, thanks :)

Best regards.

I have attached a form that is a good example of multiple form fields with the same name. The form field cltName and AdvisorName are found multiple times in the document and cltName appears twice on page 2 of the document. I am not sure of all of the rules that go into form fields but the one thing I do know that that we often see form field names duplicated when we look at account forms from our various vendors. Also when drawing form fields in Adobe Acrobat Professional there doesn't seem to be any indication that this is something that should not be done.

Thank you for considering this feature as I think it will be a welcome feature by many developers.

Ryan