Hi.
I'm evaluating QuickPDF and some other libraries for my company. I'm trying to fill a form with data, but can't find a fully functioning way to go.
This code snip has two ways of iterating the fields - with LoopXML defined, the source data is iterated and the value of the form field with the matching name is set. If LoopXML isn't defined, it iterates through the fields in the form and tries to find the matching name in the XML source, read it and set the value.
int nMainDocID = -1;
XmlDocument xml_doc = new XmlDocument(); xml_doc.LoadXml(sResponse);
foreach ( XmlNode page in xml_doc.SelectNodes("//Report/Page")) { int ec = QP.LoadFromFile(sReportFilename + ".pdf", ""); if (ec != 1) { Response.Write("<br>Report can not be loaded!"); return; } int nCurrentDocID = QP.SelectedDocument(); #if LoopXML foreach (XmlNode n in page) { ec = QP.SetFormFieldValueByTitle(n.Name, n.InnerText); } int nFC = QP.FormFieldCount(); for (int i = 0; i < nFC; i++) { QP.UpdateAndFlattenFormField(i); } #else int nFailCount = 0; int nFC = QP.FormFieldCount(); for (int i = 0; i < nFC; i++) { string sField = QP.GetFormFieldTitle(i);
if (sField == null || sField.Trim() == "") { QP.FlattenFormField(i); // "Remove" field nFailCount++; continue; } XmlNode n = page.SelectSingleNode("./" + sField); if (n != null) { QP.SetFormFieldValue(i, n.InnerText); QP.UpdateAndFlattenFormField(i); } else { QP.FlattenFormField(i); // "Remove" field } } #endif if (nMainDocID == -1) { nMainDocID = nCurrentDocID; } else { QP.SelectDocument(nMainDocID); QP.MergeDocument(nCurrentDocID); } }
Response.ContentType = "application/pdf"; Response.AddHeader("Content-disposition", "attachment; filename=" + GetQueryParam("RPT") + ".pdf"); Response.BinaryWrite((byte [])QP.SaveToVariant()); Response.End();
Neither of these ways work all the way...
The first one successfully sets all field data, but there's no way of flattening the fields. The loop after setting the data, that attempts to flatten the fields, fails. Probably for the same reason the second way fails; half way through the fields (110 of 218) the field names comes up as empty, and there's no name to find in the source data .
The PDF with the form: http://www.filedropper.com/fk3059 - http://www.filedropper.com/fk3059
Any ideas...?
Another unrelated question is if there's a better way to duplicate the original document. This source document should be repeated for each Page-tag in the source. The only way I found to do that was to actually load the document from the file again, which seems unnecessary... This is also one of the reasons I need to flatten the fields (to avoid duplicates).
Grateful for rapid replies..
Regards, Clas
|