We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Best practice - automated populating of PDF forms

What are the best practices surround the automated creation of PDF forms? Specifically, given a PDF template, what is the best way to iterate through its dynamic content and fill each with the correct value?

I'm thinking PDF input fields might be used, with font, size, style, and everything else. I'd like the program to open the PDF, and for each field have enough information from the PDF input field itself to know what data it needs to find from the application's dictionary.

I'm too ignorant at this point to know what kind of information a field can hold, how elaborate it can be, how elaborate it should be, or even whether the field can specify for the application even which class it should use.

But I don't expect we're the first group to look for something like this, or develop something like this. Any help would be appreciated.

7 Replies

AD Administrator Syncfusion Team June 5, 2009 01:18 PM UTC

Hi Thomas,

Thank you for your inquiry.

An Essential Studio Product Manager is already working on your inquiry, as per our email exchanges. I will email you our response on Monday.

Thank you!
Sandra


TG Thomas Gagne June 9, 2009 03:39 PM UTC

Here's an excerpt from the code I have working. I have some questions below it:

using Syncfusion.Core;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Interactive;

...


foreach (PdfLoadedTextBoxField field in document.Form.Fields)
{
switch (field.Name)
{
case "accountNumber":
field.Text = "an account number";
break;
case "formattedAddress":
field.Text = "address line one\raddress line two";
break;
default:
break;
}
}

While iterating I'd prefer if I could use the more generic PdfField rather than PdfLoadedTextBoxField, but I need to know if the field has the "text" property. I could simply make sure all fields are PdfLoadedTextBoxFields, but I can't be sure of that. Is there a way to look at the field to see if it has the text property then only set it if it does? I realize some casting might be involved, but I need to discriminate between the two types.


TG Thomas Gagne June 9, 2009 04:13 PM UTC

Another problem I noticed is creating a document from scratch, by merging other documents, then trying to update the fields.

To create a new (empty) document I use "var document = new PdfDocument();" It takes no arguments. Into it I merge three documents. One of them has fields.

Once the entire document is merged I want to iterate through its fields, but PdfDocuments don't have fields--only PdfLoadedDocuments do, but the latter doesn't have a constructor that takes 0 arguments.


AD Administrator Syncfusion Team June 9, 2009 04:18 PM UTC

Hi Thomas,

Thanks for your interest in Syncfusion Products.

Could you please try with the below code-snippet for iterating through the loaded form fields.

[C#]

PdfLoadedDocument pdfDocument = new PdfLoadedDocument(fileName);

PdfLoadedForm pdfForm = pdfDocument.Form;

foreach (PdfLoadedField field in pdfDocument.Form.Fields)
{
if (field is PdfLoadedTextBoxField)
{
PdfLoadedTextBoxField textField = (field as PdfLoadedTextBoxField);
switch (field.Name)
{
case "accountNumber":
textField.Text = "an account number";
break;
case "formattedAddress":
textField.Text = "address line one\raddress line two";
break;
default:
textField.Text = "Text Field";
break;
}
}
}

pdfDocument.Save("sample.pdf");


Please try this and let us know if you have any questions.

Best Regards,
Thiru


AD Administrator Syncfusion Team June 9, 2009 04:31 PM UTC

Hi Thomas,

Could you please try with the below code-snippet to merge the existing documents to newly created pdf document.


[C#]

string[] pdfFiles = { "file1.pdf", "file2.pdf", "file3.pdf" };

//Create a new Pdf Document
PdfDocument myNewPdfDoc = new PdfDocument();

//Merge the existing Pdf to newly created document
PdfDocument.Merge(myNewPdfDoc, pdfFiles);

//Save the pdf document.
myNewPdfDoc.Save("output.pdf");

myNewPdfDoc.Close(true);

Please try this and let us know if you have any questions.

Best Regards,
Thiru


TG Thomas Gagne June 9, 2009 04:45 PM UTC

Yes, the "field is" and "field as" tests worked well. More readable code. Thank you.

As to the other suggestion, the problem isn't merging documents (I don't know how many there will be--their names come from the DB). The problem is that after merging them I don't have a document with form fields I can access.

Or at least, with form fields I can see.

I tried saving the PdfDocument to a MemoryStream, then reading it back into a PdfLoadedDocument. The 3rd page has the form on it. When I tried iterating through the new document's form fields, it can't find any--or at least the for loop doesn't find any.

protected PdfLoadedDocument CompleteFormFields(PdfDocument document, DataRow address)
{
var memoryStream = new MemoryStream();
document.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

var loadedDocument = new PdfLoadedDocument(memoryStream);

foreach (PdfLoadedField field in loadedDocument.Form.Fields)
{
PdfLoadedTextBoxField textField;

if (field is PdfLoadedTextBoxField)
{
textField = field as PdfLoadedTextBoxField;

switch (field.Name)
{
case "accountNumber":
textField.Text = "an account number";
break;
case "formattedAddress":
textField.Text = "address line one\raddress line two";
break;
default:
break;
}
}
}

return loadedDocument;
}


BP Bhuvaneswari P Syncfusion Team June 16, 2009 10:49 AM UTC

Hi Thomas,

I am afraid that I am unable to reproduce the reported issue. It works as expected. After merge the document with form fields, I could access the form fields in the output document.

Here is the sample which demonstrates the same:
http://files.syncfusion.com/samples/pdf.Windows/PDF_FormMerge_82246.zip

Please try to reproduce the issue in that sample and send us the same. This would be very helpful for us to investigate the issue further.

Best Regards,
Bhuvana

Loader.
Live Chat Icon For mobile
Up arrow icon