Csharp/C#教程:iTextSharp使用空白页面创建PDF分享


iTextSharp使用空白页面创建PDF

我刚刚将iTextSharp XMLWorker nuget包(及其依赖项)添加到我的项目中,我正在尝试将HTML从字符串转换为PDF文件,即使没有抛出exception,也会生成PDF文件两个空白页面。 为什么?

以前版本的代码只使用了带有HTMLWorker和ParseList方法的iTextSharp 5.5.8.0,然后我切换到

这是我正在使用的代码:

public void ExportToPdf() { string htmlString = ""; Document document = new Document(PageSize.A4, 40, 40, 40, 40); var memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); document.Open(); htmlString = sbBodyMail.ToString(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, new StringReader(htmlString)); document.Close(); DownloadFile(memoryStream); } public void DownloadFile(MemoryStream memoryStream) { //Clears all content output from Buffer Stream Response.ClearContent(); //Clears all headers from Buffer Stream Response.ClearHeaders(); //Adds an HTTP header to the output stream Response.AddHeader("Content-Disposition", "attachment;filename=Report_Diagnosis.pdf"); //Gets or Sets the HTTP MIME type of the output stream Response.ContentType = "application/pdf"; //Writes the content of the specified file directory to an HTTP response output stream as a file block Response.BinaryWrite(memoryStream.ToArray()); //Response.Write(doc); //sends all currently buffered output to the client Response.Flush(); //Clears all content output from Buffer Stream Response.Clear(); } 

如果我放置document.Add(new Paragraph("Just a test")); 就在document.Close();之前document.Close(); 段落在第二页中呈现,但文档的其余部分仍为空白。

UPDATE

我已经将htmlString变量中的HTML更改为DIVTABLE并且它有效。 所以,现在问题变成了:我怎么知道HTML的哪一部分在XMLWorker中导致了一些错误?

我已经发现XMLWorkerHelper在使用DIV width属性时遇到了问题(甚至在样式属性上设置),不幸的是它没有抛出任何exception来帮助你。

我从iTextSharp的开发人员那里得到了这个答案,他说,目前还不支持以表格为中心,所以我假设这也不受支持。

上述就是C#学习教程:iTextSharp使用空白页面创建PDF分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/951209.html

(0)
上一篇 2021年11月18日
下一篇 2021年11月18日

精彩推荐