簡體   English   中英

iTextSharp創建空白頁面的PDF

[英]iTextSharp creates PDF with blank pages

我剛剛將iTextSharp XMLWorker nuget包(及其依賴項)添加到了我的項目,並且我試圖將HTML從字符串轉換為PDF文件,即使沒有引發異常,該PDF文件也是通過以下方式生成的:兩個空白頁。 為什么?

以前的代碼版本僅將iTextSharp 5.5.8.0和HTMLWorker和ParseList方法一起使用,然后切換到

這是我正在使用的代碼:

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(); 該段落顯示在第二頁中,但文檔的其余部分仍為空白。

更新

我已經將htmlString變量中的HTML更改為一個DIV和一個TABLE並且它起作用了。 因此,現在的問題變成了:我怎么知道HTML的哪一部分在XMLWorker中引起了一些錯誤?

我發現XMLWorkerHelper在DIV width屬性(甚至在style屬性上設置)上遇到了麻煩,不幸的是,它沒有拋出任何異常來幫助您。

我從iTextSharp的開發人員那里找到了這個答案,它說尚不支持將表格居中,因此我假設也不支持此功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM