iTextSharp使用空白页面创建PDF

问题描述 投票:6回答:1

我刚刚将iTextSharp XMLWorker nuget包(及其依赖项)添加到我的项目中,并且我正在尝试将HTML从字符串转换为PDF文件,即使没有抛出异常,也会生成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();,则段落将在第二页中呈现,但文档的其余部分仍为空白。

UPDATE

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

c# asp.net itextsharp xmlworker
1个回答
4
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.