Q:
public ActionResult PdfCompleted(byte[] pdf)
{
PDFNet.Initialize();
var streamedPdf = new MemoryStream(pdf);
var doc = new PDFDoc(streamedPdf);
var filter = pdftron.PDF.Convert.ToXod(doc); //, @"C:\Temp\FromMemPdf.xod");
var xodBytes = new byte[filter.Size()];
var streamedXod = new MemoryStream(xodBytes);
return new FileStreamResult(streamedXod, "application/xod");
}
The error message I get is as follows.
Uncaught Error: Invalid XOD file: Zip end header data is wrong size!
You are receiving this
error in the WebViewer because most likely, the XOD data is not streamed
correctly. In order to do so, the Filter returned by Convert.ToXod must be
consumed until no more bytes can be read. The call to Filter.Size() is not
guaranteed to be the actual size of a fully converted document. The code
snippet below demonstrates how to convert and stream PDF to XOD and feed the
data directly to WebViewer:
String file = Request["file"];
if (String.IsNullOrEmpty(file)) {
// if no parameter passed, just use
the default file
file = "newsletter.pdf";
}
file = String.Format("{0}{1}{2}",
Server.MapPath("."), "/../../TestFiles/", file);
// Clear the header and contents in order to serve xod file
instead of html.
Response.Clear();
Response.ContentType =
"application/vnd.ms-xpsdocument";
try {
PDFNet.Initialize();
// thumbnails are not used for
streaming, so generating and transfering them is a waste of resources
pdftron.PDF.Convert.XODOutputOptions
options = new pdftron.PDF.Convert.XODOutputOptions();
options.SetOutputThumbnails(false);
// point to the source file on disk
that is to be converted, and begin the conversion
Debug.WriteLine("Prepare
conversion...");
pdftron.Filters.Filter filter =
pdftron.PDF.Convert.ToXod(file, options);
// now ready to stream the document
as it is converted
pdftron.Filters.FilterReader fReader
= new pdftron.Filters.FilterReader(filter);
byte[] buffer = new byte[64 * 1024];
//64 KB chunks
int bytesRead = 0;
bytesRead = fReader.Read(buffer);
Response.BufferOutput = false;
int totalBytesSent = 0;
Debug.WriteLine("Commence
streaming...");
while (bytesRead > 0) {
// write
bytes to the response stream
Response.OutputStream.Write(buffer, 0, bytesRead); // last write will likely be less than buffer size
// write to
output how many bytes have been sent
totalBytesSent += bytesRead;
Debug.WriteLine("Server sent total " + totalBytesSent + "
bytes.");
// read next
bytes
bytesRead =
fReader.Read(buffer);
}
Debug.WriteLine("Done.");
// ensure all bytes have been sent
and stop execution
PDFNet.Terminate();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex) {
Debug.WriteLine("Error: "
+ ex.Message);
Debug.WriteLine("at " +
ex.StackTrace);
}