protected void Page_Load(object sender, EventArgs e) { //String file = Request["file"]; String file = "C:\\temp\\d4e32ffd-89e0-44a1-8cd4-f95bb13dc1ba.xod"; 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); // 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."); HttpContext.Current.ApplicationInstance.CompleteRequest(); // ensure all bytes have been sent and stop execution } catch (HttpException ex) { // if the page is left part way through streaming Debug.WriteLine("Error: " + ex.Message); } catch (Exception ex) { Debug.WriteLine("Error: " + ex.Message); Debug.WriteLine("at " + ex.StackTrace); Response.StatusCode = 500; } }
public void ProcessRequest(HttpContext context) { PDFNet.Initialize("CT Orthopaedic Specialists(ct-ortho.com):ENTCPU:1::W:AMS(20160714):DF4FD2223CBF58B9128E100F400DD2BC2BFD701DC22C3C2E6D83F6B6F5C7"); string filePathPdf = "C:\\PostOffice\\Content\\WebViewer\\Sample1.pdf"; string filePathXod = "C:\\PostOffice\\Content\\WebViewer\\Sample1.xod"; pdftron.PDF.Convert.ToXod(filePathPdf, filePathXod); String file = filePathPdf; //file = String.Format("{0}{1}{2}", Server.MapPath("."), "/../../TestFiles/", file); // Clear the header and contents in order to serve xod file instead of html. context.Response.Clear(); context.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); context.Response.BufferOutput = false; int totalBytesSent = 0; Debug.WriteLine("Commence streaming..."); while (bytesRead > 0) { // write bytes to the response stream context.Response.BinaryWrite(buffer); // 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); } }