Frequently Asked Question:
Multi-page PDF form not printing values
Question
I am using the following code with the DLL version to fill PDF fields from a database and print the values. However, even though my last field has a value in it and the iresult for the setformfieldValueByTitle == 1 indicating that the value is being filled into the correct field, nothing prints on the 2nd page. Why not?
// Load the document that you want to print into memory
iResult = qp.LoadFromFile(fileName.c_str());
iFieldCount = qp.FormFieldCount();
for (int i = 0; i < iFieldCount; i++) {
FormField *fCurrField = new FormField();
fCurrField = (FormField*)lCurrFields->LFormFields->Items[i];
if (fCurrField->getFieldName() != "") {
sCurrField = fCurrField->getFieldName();
sCurrValue = fCurrField->getValue().operator AnsiString();
iResult = qp.SetFormFieldValueByTitle
(fCurrField->getFieldName().c_str(),
sCurrValue.c_str());
}
qp.UpdateAppearanceStream(i);
}
//AnsiString DestFileName = "c:\\temp\\test.pdf";
AnsiString PrinterName = qp.GetDefaultPrinterName().c_str();
int StartPage = 1; // Set first page number
// int EndPage = 1;
int EndPage = qp.PageCount(); // Set last page number
// Set the paramters for the PrintOptions function
int PageScaling = 1;
int AutoRotateCenter = 0;
AnsiString Title = fileName;
// Assign the options for printing to an int variable
int Options = qp.PrintOptions(PageScaling, AutoRotateCenter,
Title.c_str());
// Print the document with the required paramters and options specified
qp.PrintDocument(PrinterName.c_str(), StartPage, EndPage, Options);
Answer
Many of the form field functions use 1 based indexing. So changing the loop to the following should work a little better.
for (int i = 1; i <= iFieldCount; i++)