C# – Export html to pdf in ASP.NET

asp.netcexport-to-pdfpdf-generation

I am trying to export retrieved data from SQL into PDF using ASP.NET (C#).
Remarks:

  1. I am not using a gridview.
  2. I designed the format of the page by using an HTML table and asp labels.
    • HTML table to format the layout and asp labels to show the values of my selected data from SQL.

How can I convert an HTML table to PDF using ASP.NET?

Can anyone help me? Thanks.

Best Answer

You can try to use itextsharp (link)

Example:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html;

// step 1 -- get html content
string htmlContent = ... // you html code (for example table from your page)

Document document = new Document();

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.GetInstance(document, new FileStream("c:\\Chap0101.pdf", FileMode.Create));

// step 3: we open the document
document.Open();

// step 4: we add a paragraph to the document
//document.Add(new Paragraph(htmlContent.ToString()));

System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(new StringReader(htmlContent));

HtmlParser.Parse(document, _xmlr);

// step 5: we close the document
document.Close();
Related Topic