【问题标题】:iTextSharp creates PDF with blank pagesiTextSharp 创建带有空白页的 PDF
【发布时间】:2016-05-28 02:23:16
【问题描述】:

我刚刚将 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(); 之前,则该段落将呈现在第二页中,但文档的其余部分仍然是空白的。

更新

我已将 htmlString 变量中的 HTML 更改为 DIVTABLE 并且它起作用了。所以,现在问题变成了:我如何知道 HTML 的哪一部分导致 XMLWorker 出现错误?

【问题讨论】:

    标签: c# asp.net itextsharp xmlworker


    【解决方案1】:

    我发现 XMLWorkerHelper 在使用 DIV 宽度属性时遇到了问题(甚至设置了样式属性),不幸的是它没有抛出任何异常来帮助您解决这个问题。

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

    【讨论】:

    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2011-01-31
    • 2012-03-06
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多