Print Page | Close Window

"Empty" fields in form.

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2647
Printed Date: 22 Nov 24 at 1:17PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: "Empty" fields in form.
Posted By: ClasG
Subject: "Empty" fields in form.
Date Posted: 21 May 13 at 3:22PM
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 Shocked.

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



Replies:
Posted By: AndrewC
Date Posted: 23 May 13 at 5:47AM
Clas,

You should flatten the formfields backwards because FormFieldCount is decremented after a field is flattened.  You will find that your code will start to work a little better.

Andrew.


Posted By: ClasG
Date Posted: 23 May 13 at 7:22AM
Now that you say it, it's obvious... Embarrassed

Great, thanks Andrew.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk