gpt4 book ai didi

c# - 在 ASP.NET 中将 html 导出为 pdf

转载 作者:行者123 更新时间:2023-11-30 20:00:55 24 4
gpt4 key购买 nike

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

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

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

谁能帮帮我?谢谢。

最佳答案

你可以尝试使用 itextsharp ( link )

例子:

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();

关于c# - 在 ASP.NET 中将 html 导出为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19877873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com