gpt4 book ai didi

.net - ITextSharp HTML 转 PDF?

转载 作者:行者123 更新时间:2023-12-03 05:38:43 25 4
gpt4 key购买 nike

我想知道ITextSharp是否具有将HTML转换为PDF的功能。我要转换的所有内容都只是纯文本,但不幸的是,关于 ITextSharp 的文档很少甚至没有,因此我无法确定这对我来说是否是一个可行的解决方案。

如果它不能做到这一点,有人可以向我指出一些好的、免费的 .net 库,可以将简单的纯文本 HTML 文档并将其转换为 pdf 吗?

蒂亚。

最佳答案

几周前我遇到了同样的问题,这是我发现的结果。此方法将 HTML 快速转储为 PDF。该文档很可能需要进行一些格式调整。

private MemoryStream createPDF(string html)
{
MemoryStream msOutput = new MemoryStream();
TextReader reader = new StringReader(html);

// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);

// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);

// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();

// step 5: parse the html into the document
worker.Parse(reader);

// step 6: close the document and the worker
worker.EndDocument();
worker.Close();
document.Close();

return msOutput;
}

关于.net - ITextSharp HTML 转 PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2822843/

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