Q: I encountered a problem when handling digitally signed PDF
documents (CMS/PKCS #7). I would like to provide functions to preview
and/or print such documents but each time I try to do it I get the
following error:
Exception:
Message: PDF startxref not found. The file is not a valid PDF
document.
There is no problem to open/print such documents using Adobe Acrobat
Reader
starting from version 7.0.9.
-----
A: The most likely problem is that the application/library you used
to sign PDF is saving corrupt PDF documents (i.e. PDF documents with
incorrect cross reference tables).
To verify if this is the case, you can try to open the document in
Acrobat Professional. Acrobat attempts to dynamically fix these files
during 'file open'. If you have Acrobat Pro you will be prompted to
save the file when closing the document (and the error will most
likely go away).
Using PDFNet you can also make an attempt to fix broken PDF documents
using pdftron::SDF::Doc::FixBrokenDoc(). Please keep in mind that if a
PDF document is corrupt there is no absolute guarantee that PDFNet (or
Acrobat) will be able to fix the problem. So the best option is to fix
the bug in the PDF generator or to switch to another PDF library.
A sample use case for FixBrokenDoc is as follows:
// Assuming C# pseudocode
PDFDoc doc;
try { // try to open the document...
doc = new PDFDoc(input_file);
doc.InitSecurityHandler();
....
}
catch (Exception e) {
try { // try to fix the document
StdFile file = new StdFile(input_file,
StdFile.OpenMode.e_read_mode));
doc = new PDFDoc(pdftron.SDF.Doc.FixBrokenDoc(file));
}
catch (Exception e) {
// Error: Document rebuild failed.
return false;
}
}