在ASP.NET中将html导出为pdf

use*_*256 2 c# asp.net pdf-generation export-to-pdf

我正在尝试使用ASP.NET(C#)将检索到的数据从SQL导出为PDF.备注:

  1. 我没有使用gridview.
  2. 我使用HTML表格和asp标签设计了页面的格式.
    • 用于格式化布局的HTML表格和用于显示SQL中所选数据的值的asp标签.

如何使用ASP.NET将HTML表格转换为PDF?

谁能帮我?谢谢.

小智 5

您可以尝试使用itextsharp(链接)

例:

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();
Run Code Online (Sandbox Code Playgroud)