问题
Project is to fill existing forms programmatically from a database/datagridview and allow the user to edit the forms afterwards as the database does not hold all values needed.
I am able to get the values, and put them into the form however:
- Values set using AcroForms do not show/print when viewing form.
- Background color change does not show when viewing form.
Both changes appear when clicking into the field.
I understand this is caused by hybrid XFA and AcroForms issues, and that pdfXFA is a paid extension for the itext library. The pdfXFA only works to flatten XFA and I don't need to do that as I need the fields to continue to be editable. I have already tried solutions proposed by the following:
- why-dont-the-itext-7-form-field-values-print
- this awesome write up on XFA values
- This tutorial including .RemoveXfaForm()
Other solutions all reference .RemoveXfaForm() which should remove all the XFA formatting, but doesn't appear to work as intended. Using GetXFA found it remained even after .RemoveXfaForm() was called.
I have tried moving the form.SetNeedAppearances(true) and form.RemoveXfaForm() to before values are set, and after values are set right before the pdf.close() event. I feel like I am just missing something idiotically simple here and could use some help:
public virtual void ManipulatePdf(string src, string dst, DataGridViewRow dataRow)
{
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dst));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
//form.RemoveXfaForm();
//form.SetNeedAppearances(true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
//visits each field, fills dependant on whether dgv column exists
foreach (string field in fields.Keys){
switch (dataGridView1.Columns.Contains(field))
{
case false:
if (field == "Date")
{
fields.TryGetValue(field, out toSet);
//this value is visible
toSet.SetValue(dateTime.ToString("D"));
}
else
{
fields.TryGetValue(field, out toSet);
//this setting does not commit, issue with hybrid XFA/AcroForm
toSet.SetValue("0").SetBackgroundColor(ColorConstants.YELLOW);
}
break;
case true:
if (dataRow.Cells[field].Value != null && !DBNull.Value.Equals(dataRow.Cells[field].Value))
{
string value = dataRow.Cells[field].Value.ToString();
fields.TryGetValue(field, out toSet);
//this value is sometimes visable on finished form
toSet.SetValue(value);
}
break;
default:
break;
}
}
//form.RemoveXfaForm();
pdf.Close();
}
I have tried the commented out recommendations in multiple combinations and at this point really just need to find out if I can get this to work or start looking for another solution, (I've already emailed itext, no response) so thank you for any insights!
来源:https://stackoverflow.com/questions/55190391/acrofield-changes-made-using-itext7-in-c-sharp-do-not-commit-are-not-visable-due