ASP TransferHelper (RTF2PDF) |
[Top] [Chapter] [Previous] [Next] |
If you create the PDF in memory (as described here) your web service can be negatively affected in case the web user simply clicks the reload button several times. We created a download class which handles this case better. It first creates a temporary file and then writes small chunks of this file to the Response - until it detects that the client is not connected anymore or all data has been sent. As advantage over TransmitFile() we here have the possibility to react on an aborted transmission and delete the temporary file when it is not needed anymore. Also see our test server at http://www.rtf-net.com.
///---------------------------------------------------------------------------- /// The TransferHelper class is used inside the PageLoad event. Simply provide /// the Response and the RTF2PDF object to the constructor. /// Call Send( format, filename ) to send the data. The following format names /// are supported: rtf, htm, pdf and wpt ///----------------------------------------------------------------------------
public class TransferHelper : IDisposable { // We need this to prepare the response private HttpResponse Response; // We work with this engine private RTF2PDF pdfcontrol; // To transfer we are using a temporary file - this is the generic ".TMP" placeholder private string TempFile = ""; // and this the file with content and extension private string TempFileSend = ""; // Constructor, simply assign the engine and response public TransferHelper(HttpResponse aResponse, RTF2PDF aRTF2PDF) { Response = aResponse; pdfcontrol = aRTF2PDF; } // Dispose - delete the temporary files public void Dispose() { if (TempFileSend!="") { System.IO.File.Delete(TempFileSend); TempFileSend = ""; } if (TempFile!="") { System.IO.File.Delete(TempFile); TempFile = ""; } } // --------------------------------------------------------- // Now send a file - this is called from within PageLoad // --------------------------------------------------------- public bool Send(string format, string filename) { // Client is not there or file was already sent if (!Response.IsClientConnected) { Response.Close(); return true; // ok } // We need a temporary file if (TempFile=="") TempFile = System.IO.Path.GetTempFileName(); bool ok = true; // RTF Format if (format=="rtf") { TempFileSend = System.IO.Path.ChangeExtension(TempFile, ".RTF"); // Tables must be measured to be exported to Word pdfcontrol.Memo.ReformatAll(false,false); if (!Response.IsClientConnected) TempFileSend = ""; else if(!pdfcontrol.Memo.SaveToFile(TempFileSend, false, "")) { System.IO.File.Delete(TempFileSend); TempFileSend = ""; ok = false; } else { Response.Clear(); Response.ContentType = "application/rtf"; Response.AddHeader("Content-Type", "application/rtf"); Response.AddHeader("Content-Disposition","inline;filename=" + filename + ".rtf"); } } // HTML Format else if (format=="htm") { TempFileSend = System.IO.Path.ChangeExtension(TempFile, ".HTM"); if(!pdfcontrol.Memo.SaveToFile(TempFileSend, false, "")) { System.IO.File.Delete(TempFileSend); TempFileSend = ""; ok = false; } } // WPT (WPTools) Format else if (format=="wpt") { TempFileSend = System.IO.Path.ChangeExtension(TempFile, ".WPT"); if(!pdfcontrol.Memo.SaveToFile(TempFileSend, false, "")) { System.IO.File.Delete(TempFileSend); TempFileSend = ""; ok = false; } else { Response.Clear(); Response.ContentType = "application/wpt"; Response.AddHeader("Content-Type", "application/wpt"); Response.AddHeader("Content-Disposition","inline;filename=" + filename + ".wpt"); } } // PDF Format else if (format=="pdf") { TempFileSend = System.IO.Path.ChangeExtension(TempFile, ".PDF"); pdfcontrol.PdfCreator.PDFFile = TempFileSend; pdfcontrol.PdfCreator.FontMode = 0; pdfcontrol.Memo.ReformatAll(false,false); if (!Response.IsClientConnected) TempFileSend = ""; else if(!pdfcontrol.PdfCreator.Print()) { System.IO.File.Delete(TempFileSend); TempFileSend = ""; ok = false; } else { Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Type", "application/pdf"); Response.AddHeader("Content-Disposition","inline;filename=" + filename + ".pdf"); } } // Unknown format else { TempFileSend = ""; ok = false; } // --------- now send and while doing so check IsClientConnected ---------- if (TempFileSend!="") { const int PartSize = 8192; byte[] buffer = new byte[PartSize]; System.IO.FileStream Stream = new System.IO.FileStream(TempFileSend, System.IO.FileMode.Open); using(Stream) { int l; long len = Stream.Length; while ((len>0)&& Response.IsClientConnected) { l = Stream.Read(buffer, 0, PartSize); Response.OutputStream.Write(buffer, 0, l); Response.Flush(); len -= l; } } Response.Close(); } return ok; } }
You can call this method at the end of your PageLoad handler.
This demo simply takes a file (must be in directory c:\doc) and converts to PDF:
RTF2PDF wpdllint1 = new RTF2PDF(); try { string path = Server.MapPath(".") + "\\"; wpdllint1.SetLicense("@FILE@"+password, "C:\\Windows\\rtflic.dat",0); WPDynamic.IWPEditor Memo = wpdllint1.Memo; WPDynamic.IWPTextCursor TextCursor = Memo.TextCursor; //------------------------------------------------------------------------- string afile = System.IO.Path.GetFileName(Request.QueryString.Get("file")); string afilepath = "c:\\doc\\" + afile;
// Try to load - if fails use "Memo" to display the error message if (!Memo.LoadFromFile(afilepath,false,"")) { Memo.Clear(false, false); TextCursor.CPPosition = 0; TextCursor.InputText("Cannot open file \"" + afile +"\""); Response.Write( wpdllint1.Memo.SaveToString(false, "HTML") ); } else { // Optional: Add a footer --------------------------------------------- // Insert right tab for page numbering --------------------------------- TextCursor.InputFooter(0,"",""); TextCursor.InputTabstop(true, (int)(29.7/2.54*1440), 1,0); TextCursor.InputText("Page "); TextCursor.InputFieldObject("PAGE","","1"); TextCursor.InputText("/"); TextCursor.InputFieldObject("NUMPAGES","","1"); TextCursor.GotoBody(); // and use our helper class to export as PDF --------------------------- TransferHelper transfer = new TransferHelper(Response, wpdllint1); using(transfer) { transfer.Send("pdf",afile); } } } finally { wpdllint1.Dispose(); }
|