There was an error opening this document. the file is damaged and could not be repaired

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();
}
And I created this simple function to prove that Apose.pdf somehow messed up the file that caused the Acrobat 8.nn could not open the file with the error above.

This function simply open a PDF file and write it to the dqtest.pdf file. The original pdf still can be opened by Acrobat 8.nn.

The test program is compiled using the latest version of apose.pdf.dll and the older versions. None works!

Do the samething with iTextSharp works just fine.

public void CreateNewPdf2(string pdfFileRelativePath)
{
var pdfFullPath = GetFullPathToPdfFile(pdfFileRelativePath);
Aspose.Pdf.Facades.Form inputForm = new Form(pdfFullPath);
inputForm.Save("c:\\temp\\dqtest.pdf");
}


Here is the link to the PDF file:

http://1drv.ms/1GjdMT7

Hi Denis,

Thanks for your inquiry. I have tested the scenario with Adobe Reader 8 and managed to observe the reported error. For further investigation, I have logged an issue in our issue tracking system as PDFNEWNET-38864 and also linked your request to it. We will keep you updated via this thread regarding the issue status.

Please feel free to contact us for any further assistance.

Best Regards