Hi,
In our application, we handle PDF editable forms. We store the PDF form fields values in a database and while loading the form in our application we fill the form fields with the stored values.
We use Aspose.PDF for loading the PDFs with stored values. Below is a code piece that fills the PDF form fields with stored values.
PdfContentEditor contentEditor = new PdfContentEditor();
contentEditor.BindPdf(memoryStream_Of_PDFFormTemplate);
contentEditor.ReplaceTextStrategy.IsRegularExpressionUsed = true;
contentEditor.ReplaceTextStrategy.ReplaceScope = ReplaceTextStrategy.Scope.REPLACE_ALL;
// PDFDataValue is collection of PDF form fields and values.
// And below loop replaces text on the form template.
foreach (formData in PDFDataValue)
contentEditor.ReplaceText(formData.Name, formData.Value);
//save output PDF file
contentEditor.Save(memoryStream_Of_ToBeLoadPDF);
contentEditor.Close();
var pdfFormDoc = new Aspose.Pdf.Document(replaceFinal);
// Below loop is to fill the form fields.
foreach (var field in pdfFormDoc.Form)
{
var pdfField = ((PdfForms.Field)field);
var fieldName = pdfField.FullName;
if (field is PdfForms.ButtonField) { } // Not doing anything with PushButtons.
else if (field is PdfForms.TextBoxField || field is PdfForms.RichTextBoxField)
{
foreach (var data in PDFDataValue.Where(e => e.Name == fieldName))
{
txtField.Value = data.Value;
break;
}
}
else if (field is PdfForms.RadioButtonField)
{
foreach (var data in PDFDataValue.Where(e => e.Name == fieldName))
{
var rd = pdfField as PdfForms.RadioButtonField;
foreach (PdfForms.Option o in rd.Options)
if (o.Name == data.Value)
o.Selected = true;
break;
}
}
else if (field is PdfForms.ListBoxField)
{
foreach (var data in PDFDataValue.Where(e => e.Name == fieldName))
{
var lst = pdfField as PdfForms.ListBoxField;
List idList = new List();
foreach (PdfForms.Option o in lst.Options)
if (data.Value.Contains(o.Value))
idList.Add(o.Index);
lst.SelectedItems = idList.ToArray();
break;
}
}
else
{
foreach (var data in PDFDataValue.Where(e => e.Name == fieldName))
{
((PdfForms.Field)pdfFormDoc.Form[fieldName]).Value = data.Value;
break;
}
}
}
pdfFormDoc.Save(outputFinal);
replaceFinal.Close();
One of our PDF form has around 250 fields in it. While processing this form, the foreach loop to fill PDF template fields in above piece of code consumes more than 100 seconds (about 2 minutes).
Can you please suggest a better and faster approach to achieve this?
Currently we use Aspose.PDF 7.2, and I have tried out with the latest 9.3.0 version also, it also takes same amount of time.
Due to our organizaiton policies, I cannot provide the PDF form we are processing, but you can create a PDF form with 250 or more fields on it and test with that one.
Thanking you.