This Code Is useful to you for Create Html to pdf using ITextsharp library(5.4.1.0).
ITextSharp Liberary you can Download From nugetPackage also.
Here, I describe How to down load yor Html Page and Convert to Pdf.
ITextSharp Liberary you can Download From nugetPackage also.
Here, I describe How to down load yor Html Page and Convert to Pdf.
public void ConvertHTMLToPDF() { string HTMLCode = new WebClient().DownloadString("Your Page Url");//Here you add your page url. string filePath = Server.MapPath("~") + "/App_Data/" + DateTime.Now.Ticks + ".pdf"; System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(HTMLCode); Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f); HTMLWorker parser = new HTMLWorker(doc); PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create)); doc.Open(); try { parser.Parse(reader); } catch (Exception ex) { //Print Error In You Pdf File Paragraph paragraph = new Paragraph("Error!" + ex.Message); Chunk text = paragraph.Chunks[0] as Chunk; if (text != null) { text.Font.Color = BaseColor.RED; } doc.Add(paragraph); } finally { doc.Close(); } }Note: SomeTime this code not working whenever you are using inline stylesheet or inline stylesheet in your code.
No comments:
Post a Comment