I am trying to read and write values from/to form fields in a specific PDF. The PDF opens in Adobe Reader and allows me to manually enter values into each of the form fields and save.
Hi Charles,
Thanks for your interest in our products and sharing the sample source code and template document with us. I tested the scenario and able to notice the same problem. For rectification, I logged this problem in our Issue Tracking System. We will further look into the details of this issue and will keep you updated via this forum thread on the status of correction.
We apologize for your inconvenience.
Thanks & Regards,
Hi Rashid,
Hi Charles,
Thanks for using our products.
I tested the scenario using your provided sample source code with another template PDF document and unable to notice any issue while accessing form fields from PDF document. Sample PDF is attached for your reference. However, this seems that there is no issue with the component, might be issue is related to PDF document.
Please feel free to contact support in case you need any further assistance.
Thanks & Regards,
Hi Rashid,
Hi Charles,
Thanks for your feedback.
I have added the three fields in an existing blank PDF document using the below code.
[C#]
//open document
FormEditor formEditor = new FormEditor(@"d:\pdffiles\sample.pdf", @"d:\pdffiles\sampleoutput.pdf");
//add field
formEditor.AddField(FieldType.Text, "textfield", 1, 100, 100, 200, 150);
formEditor.AddField(FieldType.Text, "textfield1", 1, 100, 200, 200, 250);
formEditor.AddField(FieldType.Text, "textfield2", 1, 100, 300, 200, 350);
//save updated file
formEditor.Save();
You can see that purple header is available in the sampleoutput.pdf document. However, I have filled the form fields available in sampleoutput.pdf document using below code. All three files are attached for your reference.
[C#]
//open document
Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(@"d:\pdffiles\sampleoutput.pdf", @"d:\pdffiles\output.pdf");
//Fill the fields
pdfForm.FillField("textfield", "First Field");
pdfForm.FillField("textfield1", "Second Field");
pdfForm.FillField("textfield2", "Third Field");
//save updated PDF
pdfForm.Save();
Please feel free to contact support in case you need any further assistance.
Thanks & Regards,
Rashid,
Hi Charles,
Thanks for explaining the issue.
Suppose sampleoutput.pdf document is pre-existing form. Users can edit form fields values using Adobe Acrobat, later, You can read all the fields available in PDF document with values using Aspose.Pdf for .NET as mentioned in below code.
[C#]
//open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"d:\pdffiles\sampleoutput.pdf");
//get values from all fields
foreach (Field formField in pdfDocument.Form)
{
string name = formField.PartialName;
string value = formField.Value;
}
Please feel free to contact support in case you need any further assistance.
Thanks & Regards,
Rashid,
Hi Charles,
I tested the scenario and getting the same prompt message. In fact, the problem is not related to Aspose.Pdf rather it’s related to the limitations of Adobe Reader. However, I would like to share with you that, the same issue already logged in our issue tracking system with ID: PDFNEWNET-20907. Our development team is looking into this feature. Your request has also linked to the appropriate issue and you will be updated via this forum thread once its supported.
We apologize for your inconvenience.
Thanks & Regards,
cransoft:
Trying to access these fields via Aspose.PDF does not seem to work. I have copied sample code from the forums to read the values however it is still not working. Is there anything special on the PDF end that has to be done to allow reading/writing form fields?Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form(fileName);MessageBox.Show( form.FieldNames.Length.ToString() );Results in 0.
Hi Charles,
Thanks for contacting support.
We have further investigated this issue and have found that the reason you are facing this problem is because the source PDF file is XFA form and such type of forms cannot be manipulated using Form class. We have a class named XFA present under Aspose.Pdf.InteractiveFeatures.Forms name space which provides the capability to work with such PDF documents. I will shortly share the code snippet for working with such type of documents. We are sorry for this inconvenience.
cransoft:
We need the users to be able to modify the forms in Adobe Reader and not Acrobat. This means we need to setup the PDF with some Reader Extensions to allow saving. Is this perhaps the problem?I am trying to create a form in Adobe (not programatically), allow this to be filled out by Adobe Reader and then read the form values. Is the reader extension what is causing the problem?Hi Charles,When Reader Extensions are enabled over a PDF document, it does not create any problem for Aspose.Pdf for .NET to fill form fields or retrieve information from PDF document. As I have shared earlier, the reason you are facing this problem is because the source PDF file is XFA form.
Hi Charles,
Let me explain Rashid's perspective from PDFNEWNET-20907. Sometimes earlier, one of the customers requested a feature to be included in Aspose.Pdf for .NET that one can turn on the "Extend Forms Fill-In & Save in Adobe Reader privilege" on one of the existing PDF forms. Our development team investigated this requirement in details and came to the conclusion that this functionality cannot be implemented. Please take a look over following information which we have found on one of the forums of Adobe.
Adobe LiveCycle Reader Extensions enables PDF forms designers to extend the use of Adobe Reader to allow for any advanced functionalities that they put into their forms, such as the ability to save or submit a PDF form. The ability to extend Adobe Reader this way is only possible using Adobe's products.
Unfortunately, it is not possible for any non-Adobe owned product, to either create PDFs with extended usage rights or modify the extended usage rights on existing PDFs that were created with Adobe LiveCycle or Acrobat 9.
In addition, if you modify a PDF -- in any way, shape or form, with any none-Adobe product -- that has had extended usage rights applied to it, it will invalidate the extended usage rights, resulting in an error message similar to the one listed below popping up when you open the document.
This document contained certain rights to enable special features in Adobe Reader. The document had been changed since it was created and these rights are no longer valid. Please contact the author for the original version of this document.
Hi Charles,
Thank you for being patient.
Please download and try our latest version of Aspose.Pdf for .NET v6.8 regarding accessing form fields. Also, you can use the XFA property to access XFA fields in your PDF document (as it contains XFA form fields). Aspose.Pdf DOM supports XFA forms via the XFA class.
Please see the following code segments to access/process the XFA Form Fields using Aspose.Pdf for .NET.
- The XFA property allows access to values of XFA fields, for example below code will set the value of XFA Field “name1” in your template file:
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
Document doc = new Document("E:\\Ap Data\\March2012\\test.pdf");
//set XFA field value
doc.Form.XFA["form[0].name1[0]"] = "Text Value";
doc.Save("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Document doc1 = new Document("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
//get XFA field value
Console.WriteLine(doc1.Form.XFA["form[0].name1[0]"]);
- The Template property allows access to XML which describes form template (i.e. field appearance, etc), for example below code will make “name1” field as read only:
Document doc = new Document("E:\\Ap Data\\March2012\\test.pdf");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.Form.XFA.Template.OwnerDocument.NameTable);
nsmgr.AddNamespace("tpl", doc.Form.XFA.Template.NamespaceURI);
//get field XML node via XPath
XmlNode field = doc.Form.XFA.Template.SelectSingleNode("//tpl:subform[@name='form']/tpl:subform/tpl:field[@name='name1']", nsmgr);
//set access attribute of field
XmlAttribute attr = field.OwnerDocument.CreateAttribute("access");
field.Attributes.Append(attr);
attr.Value = "readOnly";
doc.Save("E:\\Ap Data\\March2012\\Xfa_ReadonlyField.pdf");
- The Datasets property allows getting and setting field values, for example below code will set “name1” field value:
Document doc = new Document("E:\\Ap Data\\March2012\\test.pdf");
XmlNode datasets = doc.Form.XFA.Datasets;
//get field data node
XmlNode fieldNode = datasets.SelectSingleNode("//form/name1");
//set field value
fieldNode.InnerText = "Text Field Value";
doc.Save("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Document doc1 = new Document("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Console.WriteLine(doc1.Form.XFA.Datasets.SelectSingleNode("//form/name1").InnerText);
Also, the XDP property may be used to access composite XML (which contains Datasets and Template).
Please use the above code segments as per your requirement using Aspose.Pdf for .NET v6.8 and let us know if you face any issue.
Sorry for the inconvenience,
The issues you have found earlier (filed as PDFNEWNET-20907) have been fixed in Aspose.Pdf for .NET 7.2.0.
This message was posted using Notification2Forum from Downloads module by aspose.notifier.
Hi Charles,
Thanks for your patience.
We have further investigated the problem concerning the reading of form fields from test.pdf and have found that the problem is occurring because the input file does not contain AcroForm fields but they are XFA form fields. So please try using Document.Form.XFA.FieldNames
to get list of field names. Please take a look over the following code snippet which can be used to accomplish your requirement.
[C#]
FileStream fs = new FileStream("d:/pdftest/test(2).pdf", FileMode.Open, FileAccess.ReadWrite);
Document doc = new Document(fs);
string[] name = doc.Form.XFA.FieldNames;
//Assert.AreEqual(41, name.Length);
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i] + " = " + doc.Form.XFA[name[i]]);
doc.Form.XFA[name[i]] = (i + 1).ToString();
}
doc.Save();
Please note that the above code will work with the upcoming release version of Aspose.Pdf for .NET 7.3.0 which is currently under testing phase. Please be patient and wait for the new version. We are sorry for the delay and inconvenience.
The issues you have found earlier (filed as PDFNEWNET-33311) have been fixed in Aspose.Pdf for .NET 7.3.0.
This message was posted using Notification2Forum from Downloads module by aspose.notifier.