Fixing broken PDF documents that fail to open.

Q:

We have an issue with processing the attached pdf file. It fails while
loading it from within our code

  Stream inputStream = File.OpenRead(FileName);
  int filesize = (int)inputStream.Length;
  m_pdfBuffer = new Byte[filesize];
  inputStream.Read(m_pdfBuffer, 0, filesize);
  inputStream.Close();
  _pdfDoc = new PDFDoc(m_pdfBuffer, m_pdfBuffer.Length); // --->
here fails
  _pagesCount = _pdfDoc.GetPagesCount();

The exception thrown is:
  Message: Object header not found
  Conditional expression: false
  Filename : XRefParser.cpp
  Function : PDFTRON::SDF::XRefParser::ParseXRef
  Linenumber : 333

It also fails while opening it with the cosedit; the same exception is
thrown.
The file can be opened with the Acrobat.
Is there any workaround to make it work?
----
A:

The sample file is corrupt (i.e. it has a bad xref table). Acrobat
attempts to dynamically to fix these files during 'file open'. If you
have Acrobat Pro you will be prompted to save the file when closing
the window (and the error will most likely go away).

Using PDFNet you could also try to fix a broken PDF document
programmatically using SDF.XRefRebuilder class (or in PDFNet 4.x this
class is replaced with Doc::FixBrokenDoc()). Please keep in mind that
if a PDF document is corrupt there are no absolute guarantees that
PDFNet (or Acrobat) will be able to fix the problem.

The following is an example of using XRefRebuilder (assuming C#):

PDFDoc doc;
try {
  doc = new PDFDoc(input_file);
  ....
}
catch (Exception e)
{
  try {
    // try rebuilding the doc
    Filter stm = StdFile(input_file, StdFile.OpenMode.e_read_mode));
    XRefRebuilder rb = new XRefRebuilder();
    doc = new PDFDoc(rb.Rebuild(stm));
  }
  catch (Exception e) {
    // Error: Document rebuild failed.
    return false;
  }
  ...
}

Q:

thanks, it helped. Now we can process those damaged pdf-s.
Is there any way to change the call
  try {
    // try rebuilding the doc
    Filter stm = StdFile(input_file, StdFile.OpenMode.e_read_mode));
    XRefRebuilder rb = new XRefRebuilder();
    doc = new PDFDoc(rb.Rebuild(stm));
  }
into creating the Filter from a memory buffer, where the memory buffer
is the buffer obtained calling File.ReadAllBytes(...)?
----
A:

You could pass pdftron.Filters.MemoryFilter instead of
pdftron.Filters.StdFile.