Rank: Member
Groups: Registered
Joined: 5/9/2019(UTC) Posts: 25  Location: Sydney Thanks: 1 times Was thanked: 1 time(s) in 1 post(s)
|
Hi Paul, I've got an issue with hybrid XFA PDFs that are processed through Pdfium. Pretty much we automate the PDF, inserting values into PdfFields, with the user also able to manually enter values. When one of these documents contains an XFA item in the InterForm Dictionary (HasXFAForm = true), there can be a problem opening the resulting file in some external PDF editors. - In Chrome the field values all appear correctly. - In Foxit the values appear in the fields, but if you click in a field the value disappears and anything you enter will override it. - In Acrobat Pro DC or Acrobat Pro 2017 the field values won't appear at all. From this SO it seems clear the problem is using XFA-aware software (Adobe) - https://stackoverflow.co...removing-the-xfa-format.I've attached a sample form that shows the problem. The 'Plaintiff' field was filled automatically in our editor, the 'Defendant' field I filled manually in our editor and the 'Case No.' field I filled manually in Foxit. As you can see if you open the form in Foxit or Adobe, only the two fields filled using our software are at fault. There is a "datasets" entry in the InterForm Dicionary XFA item which contains the "123" value, but not the other two, so it would seem Pdfium isn't updating any XFA data. From what I can tell there is nothing in Pdfium to remove (or otherwise handle) the XFA element as per the above SO question. I'm going to try to remove the XFA item from the InterForm Dictionary manually upon saving, though this seems potentially dangerous. I don't have any specific questions. Just wondering if you've come across this issue before. Or whether you have any ideas on either updating that XFA datasets item with field values or removing XFA from the form completely. Regards, Richard XfaSample.pdf (563kb) downloaded 53 time(s).
|
|
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,138
Thanks: 10 times Was thanked: 133 time(s) in 130 post(s)
|
Hi! Indeed, the XFA fields are stored elsewhere and in XML format. Currently, the Pdfium.Net SDK does not fully support XFA fields. However, there is a workaround for automatically inserting values into XFA fields. Here is an example for "Plaintiff" field Code:using (var doc = PdfDocument.Load(@"e:\44\XfaSample.pdf", new PdfForms()))
{
var field = doc.FormFill.InterForm.Fields.Find((item) => { return item.AlternateName == "Plaintiff"; });
field.Value = "TestVal";
var datasets = GetXFAStream("datasets", doc.FormFill.InterForm.Dictionary["XFA"].As<PdfTypeArray>());
var xml = new System.Xml.XmlDocument();
xml.LoadXml(datasets.DecodedText);
var nodes = xml.GetElementsByTagName(field.AlternateName);
nodes[0].InnerText = field.Value;
using (var sw = new StringWriter())
{
xml.Save(sw);
var str = sw.ToString();
datasets.SetContent(PdfCommon.DefaultAnsiEncoding.GetBytes(str), false);
}
doc.Save(@"e:\44\XfaSample2.pdf", SaveFlags.NoIncremental);
}
and GetXFAStream Code:private static PdfTypeStream GetXFAStream(string key, PdfTypeArray array)
{
if (array == null || array.Count < 2 || array.Count % 2 != 0)
return null;
for(int i=0; i< array.Count; i+=2)
{
var str = array[i].As<PdfTypeString>();
if (str == null || str.UnicodeString!= key)
continue;
var stream = array[i + 1].As<PdfTypeStream>();
if (stream == null)
return null;
return stream;
}
return null;
}
Edited by user Tuesday, February 18, 2020 10:33:41 PM(UTC)
| Reason: Not specified
|
|
|
|
|
|
Rank: Member
Groups: Registered
Joined: 5/9/2019(UTC) Posts: 25  Location: Sydney Thanks: 1 times Was thanked: 1 time(s) in 1 post(s)
|
Thanks Paul! Works well. In case anyone else has this issue, I had to make a slight modification to Paul's code. The XML created by xml.Save(sw) includes the XML declaration at the start, which raises an error message box when opening the subsequent document in Adobe. I had to do this to fix it: Code:var xmlSettings = new XmlWriterSettings {OmitXmlDeclaration = true, Indent = true};
using (var stringWriter = new StringWriter())
using (var xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
{
xml.Save(xmlWriter);
xfaDatasets.SetContent(PdfCommon.DefaultAnsiEncoding.GetBytes(stringWriter.ToString()), false);
}
|
 1 user thanked richard for this useful post.
|
|
|
|
Forum Jump
You can post new topics in this forum.
You can reply to topics in this forum.
You can delete your posts in this forum.
You can edit your posts in this forum.
You cannot create polls in this forum.
You can vote in polls in this forum.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close