iTextSharp使用空白页面创建PDF

Jul*_*ira 7 c# asp.net itextsharp xmlworker

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

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

UPDATE

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

Jul*_*ira 4

我发现 XMLWorkerHelper 在DIV宽度属性(甚至在样式属性上设置)方面遇到了问题,不幸的是它不会抛出任何异常来帮助您解决此问题。

我从 iTextSharp 的开发人员那里找到了这个答案,它说尚不支持将表格居中,所以我假设这也不支持。