Hi All,
I have many PDF forms that my little C# program fills with data retrieved from SQL database and let user print the form.
The issue is that I have many users still use Acrobat 8.nn (cannot update to newer version), and Acrobat 8.nn cannot open the target PDF file.
Below is my code.
public byte[] CreateNewPdf(string pdfFileRelativePath, Guid corrRequestId)
{
_log.DebugFormat("CreateNewPdf(pdfFileRelativePath={0}", pdfFileRelativePath);
// Retrieve case data using a sproc with Correspondence Id as parameter
var cadenceDataDt = _dataRetrieval.CreatePdfCorrDataTable(corrRequestId, null);
// Return an empty file if there is no data.
// Todo: Should this be an exception instead?
if (cadenceDataDt.Rows.Count == 0) return new byte[0];
// Set the Aspose license.
var pdfLicense = new License();
pdfLicense.SetLicense(@"Aspose.Pdf.lic");
var isHCFA = pdfFileRelativePath.Contains("HCFA");
var invoiceServiceList = new List();
if (isHCFA)
{
_log.Debug("Identified a HCFA.");
invoiceServiceList =
_dataRetrieval.GetInvoiceServices(cadenceDataDt.Rows[0][_invoiceNumberField].ToString(),
_organizationService);
pdfFileRelativePath = GetHcfaPageCountTemplate(pdfFileRelativePath, invoiceServiceList.Count);
}
// Get a list of defined field names from the PDF form
var pdfFullPath = GetFullPathToPdfFile(pdfFileRelativePath);
_log.DebugFormat("Creating form from path: {0}", pdfFullPath);
var pdfForm = new Form(pdfFullPath);
var allfields = pdfForm.FieldNames;
// If sproc field name matches Pdf Form field name, update Pdf field with sproc field value
foreach (var fieldName in allfields)
{
if (!cadenceDataDt.Columns.Contains(fieldName)) continue;
var fieldValueInDb = cadenceDataDt.Rows[0][fieldName].ToString(); //takes care of DBNull
pdfForm.FillField(fieldName, fieldValueInDb, true);
}
if (isHCFA && invoiceServiceList.Any())
{
// Add Service fields for invoices
// Need to update sproc field names with row number in order to match Pdf Service form fields
// so keep track of record count and add to end of sproc field name
var count = 0;
// If sproc field name + count = Pdf Form field name, update with sproc value
foreach (var s in invoiceServiceList)
{
count = ++count;
var serviceData = _dataRetrieval.ServiceDataTable(s.Id);
foreach (DataColumn dc in serviceData.Columns)
{
var fieldNameWithRow = string.Format("{0}{1}", dc.ColumnName, count);
if (!allfields.Contains(fieldNameWithRow)) continue;
var fieldValueInDb = serviceData.Rows[0][dc.ColumnName].ToString(); //takes care of DBNull
pdfForm.FillField(fieldNameWithRow, fieldValueInDb, true);
}
}
// Some fields are different on the HCFA for different clients
// Determine field differences based on HCFA template name and update accordingly
var HCFATemplate = cadenceDataDt.Rows[0]["TemplateName"].ToString();
// OWCP HCFA
if (HCFATemplate.Contains("OWCP"))
{
UpdateHCFAOWCPFields(invoiceServiceList.Count, pdfForm, cadenceDataDt);
}
}
******************* Save it to a file at this point will not be able to open by Acrobat 8.n
// At this point, the pdf form is filled with data. Save it to a memory stream.
var tempOutputStream = new MemoryStream();
pdfForm.Save(tempOutputStream);
return tempOutputStream.ToArray();
}