Similar to this post [https://forum.aspose.com/t/97951] I need to populate an existing PDF template with RTF/HTML content.
While I have already discovered that FillField simply puts the RTF/HTML into the field as raw text, I am unable to cast the Field as a RichTextBoxField as the Form doesn’t seem to exist in the document as I am populating it. I get this as an error:
Form field not found : form1[0].AttributesAndBehavioursEvaluation[0].OWValsComment01[0]
My method of populating the form is spinning through the fields of a DataRow and seeing if the ColumnName is contained in a FieldName; if it is, I make the value of the FieldName equal to the value of the DataColumn. Clearly I have named my database fields the same as my form fields:
private FileInfo BuildPdfTemplateFile(int ProjectReviewRequestID, DataTable dt, bool editable)
{
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(HttpContext.Current.Server.MapPath(“~/App_References/Aspose.Total.lic”));
DataRow dr = dt.Rows[0];
string templateFilePath = HttpContext.Current.Server.MapPath(“~/Forms/ProjectReview/ProjectReview_A.pdf”);
FileInfo newPDF = new FileInfo(Path.GetTempFileName());
File.Copy(templateFilePath, newPDF.ToString(), true);
File.SetAttributes(newPDF.FullName, FileAttributes.Normal);
FileStream fs = new FileStream(newPDF.ToString(), FileMode.Open, FileAccess.ReadWrite);
Aspose.Pdf.Document templateFile = new Aspose.Pdf.Document(fs);
Aspose.Pdf.Facades.Form newForm = new Aspose.Pdf.Facades.Form(templateFile);
string SubmitButtonName = “”;
string SubmitLabelName = “”;
foreach (DataColumn dc in dt.Columns)
{
foreach (string fieldName in newForm.FieldNames)
{
if (fieldName.Contains(dc.ColumnName))
{
switch (dc.ColumnName)
{
case “StartDate”:
case “EndDate”:
case “DeliveryDate”:
if (!string.IsNullOrEmpty(dr[dc.ColumnName].ToString()))
newForm.FillField(fieldName, Convert.ToDateTime(dr[dc.ColumnName].ToString()).ToString(“d MMM yyyy”));
break;
default:
if (!string.IsNullOrEmpty(dr[dc.ColumnName].ToString()) && IsDataColumnNumeric(dc))
{
if (dc.ColumnName.ToString().StartsWith(“OWVal”))
if (fieldName.Contains(“.field[” + dr[dc.ColumnName].ToString() + “]”))
{
string indexString = “field[” + dr[dc.ColumnName].ToString() + “]”;
newForm.FillField(fieldName.Replace(indexString, “”), dr[dc.ColumnName].ToString());
}
if (dc.ColumnName.ToString().StartsWith(“Skills”))
if (fieldName.Contains(“.field[” + dr[dc.ColumnName].ToString() + “]”))
{
string indexString = “field[” + dr[dc.ColumnName].ToString() + “]”;
newForm.FillField(fieldName.Replace(indexString, “”), dr[dc.ColumnName].ToString());
}
if (!dc.ColumnName.ToString().StartsWith(“OWVal”) && !dc.ColumnName.ToString().StartsWith(“Skills”))
newForm.FillField(fieldName, dr[dc.ColumnName].ToString());
}
else if (dr[dc.ColumnName].ToString().StartsWith(“<body xfa”))
{
RichTextBoxField rtfField = (RichTextBoxField)newForm.Document.Form[fieldName];
rtfField.RValue = dr[dc.ColumnName].ToString();
}
else
newForm.FillField(fieldName, dr[dc.ColumnName].ToString());
break;
}
}
if (fieldName.Contains(“EmailSubmitButton”))
{
SubmitButtonName = fieldName;
}
if (fieldName.Contains(“EmailSubmitLabel”))
{
SubmitLabelName = fieldName;
}
}
}
if (!editable)
{
newForm.FlattenField(SubmitButtonName);
}
newForm.Save(fs);
fs.Close();
return newForm;
}
The bolded code is where I tried to cast the Field as a RichTextBoxField and that generates the error reported above.
Suggestions? Questions?
John Anderson