We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FillField with Rich Text?

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

Hi John,


Thanks for your inquiry. Can you please share your source document here? So we will test the scenario at our end and will prvoide you more information accordingly.

Sorry for the inconvenience faced.

Best Regards,

Here you go.

Any progress?

Hi John,


Sorry for the delayed response. Can you please use Aspose.Pdf.Document class to populate the XFA form. As while using following code I’m able to loop through the form fields and populate form. Hopefully it will solve the issue.

Document pdfDocument = new Document(myDir + “ProjectReview_A.pdf”);

foreach (string field in pdfDocument.Form.XFA.FieldNames)
{
Console.WriteLine(field);
}

pdfDocument.Form.XFA[“form1[0].AttributesAndBehavioursEvaluation[0].OWValsComment01[0]”] = “ABCDEFGHIJKLMNOPQRSTU\nABCDEFGHIJKLMNOPQRSTU\nABCDEFGHIJKLMNOPQRSTU”;
pdfDocument.Save(myDir + “ProjectReview_A_out.pdf”);

Please feel free to contact us for any further assistance.

Best Regards,

No luck.


A little background on our process.

When a project comes to a close, staff members assigned to the project have a project review form filled out (this PDF). My code populates the PDF form with what data is known about the project and sends the PDF to selected Reviewers to fill out. They do so, and then submit the PDF-generated XML via email to an Exchange mail box that is monitored by an SSIS package that extracts the html/text from the fields and writes the raw html code to the appropriate field in the SQL database/table.

During the project review process, the process administrators want to be able to make edits to the reviews (make date corrections, add/remove review contributors, etc.) and then want to be able to re-create the PDF form from the captured/edited data.

Using the exact same code used to generate the original PDF, I get the latest version of the data from the database and re-populate the form and give it to them as a download.

Every field except those that are RTF-enabled work just fine. RTF-enabled fields end up with the raw HTML/RTF in them.

Attached find two PDFs:
  • ProjectReview_DXX01001_AndersonJohn_Submit.pdf
    • An Acrobat-saved version of the PDF after being filled out
  • ProjectReview_DXX01001_AndersonJohn_View.pdf
    • An ASPOSE.Pdf-populated version using the submitted data captured in the database using the XFA method as described above.
new XFA code in bold:

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))
{
if (dc.ColumnName.StartsWith(“OWVals”))
{ }
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"))
{
templateFile.Form.XFA[fieldName] = 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);
}

templateFile.Save();
newForm.Save(fs);
fs.Close();
return newPDF;


Thoughts? Suggestions?

John

No further suggestions or tips?

Hi John,


Sorry for the inconvenience faced. After initial investigation, I’ve logged as investigation ticket as PDFNEWNET-35389 in our issue tracking system for further investigation and resolution. We will keep you updated regarding issue status via this forum thread.

Best Regards,