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.
Unfortunately, activation email could not send to your email. Please try again.

Best practice - automated populating of PDF forms

Thread ID:

Created:

Updated:

Platform:

Replies:

82246 Jun 5,2009 09:12 AM Jun 16,2009 06:49 AM Windows Forms 7
loading
Tags: PDF
Thomas Gagne
Asked On June 5, 2009 09:12 AM

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.

Administrator [Syncfusion]
Replied On June 5, 2009 09:18 AM

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

Thomas Gagne
Replied On June 9, 2009 11:39 AM

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\r\naddress 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.

Thomas Gagne
Replied On June 9, 2009 12:13 PM

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.

Administrator [Syncfusion]
Replied On June 9, 2009 12:18 PM

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\r\naddress 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

Administrator [Syncfusion]
Replied On June 9, 2009 12:31 PM

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

Thomas Gagne
Replied On June 9, 2009 12:45 PM

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\r\naddress line two";
break;
default:
break;
}
}
}

return loadedDocument;
}

Bhuvaneswari P [Syncfusion]
Replied On June 16, 2009 06:49 AM

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

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;