It has the ability to convert HTML file to pdf.
Required namespace for conversions:
using iTextSharp.text; using iTextSharp.text.pdf;
and for the conversion and download file:
// Create a byte array that will eventually hold our final PDF Byte[] bytes; // Boilerplate iTextSharp setup here // Create a stream that we can write to, in this case a MemoryStream using (var ms = new MemoryStream()) { // Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF using (var doc = new Document()) { // Create a writer that bound to our PDF abstraction and our stream using (var writer = PdfWriter.GetInstance(doc, ms)) { // Open the document for writing doc.Open(); string finalHtml = string.Empty; // Read your html by database or file here and store it into finalHtml eg a string // XMLWorker also reads from a TextReader and not directly from a string using (var srHtml = new StringReader(finalHtml)) { // Parse the HTML iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml); } doc.Close(); } } // After all of the PDF "stuff" above is done and closed but **before** we // close the MemoryStream, grab all of the active bytes from the stream bytes = ms.ToArray(); } // Clear the response Response.Clear(); MemoryStream mstream = new MemoryStream(bytes); // Define response content type Response.ContentType = "application/pdf"; // Give the name of file of pdf and add in to header Response.AddHeader("content-disposition", "attachment;filename=invoice.pdf"); Response.Buffer = true; mstream.WriteTo(Response.OutputStream); Response.End();
Nitin Singh May 31 '15 at 8:20 2015-05-31 08:20
source share