首页 > 解决方案 > C# 的 itextsharp:如何使用“PdfPageEventHelper”中的“OnStartPage”事件从 html 内容呈现为 pdf

问题描述

以下是html内容:

string headerData= "<table style='width: 100%;'><tbody><tr><td style='width: 50%; border: 0.1px solid #000; backgroundcolor:#f2f2f2; color: #000; height: 22px;font-weight:bold;text-align:right'>Territory:&nbsp;&nbsp;</td><td>USA</td></tr></tbody></table>"

现在我想使用 itextsharp 导出 pdf。我想在每个页面中重复标题,所以我使用“PdfPageEventHelper”中的“OnStartPage”事件。我可以编写简单的文本(如“hello Word”)或图像,但无法呈现 html 文本。

 PdfPTable table = new PdfPTable(2);
 PdfPCell cellHeaderSection = new PdfPCell(new Phrase(headerData));
 table.AddCell(cellHeaderSection); 
 table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent);

但不幸的是,它只显示 html 格式的文本,如下图所示 在此处输入图像描述

标签: c#asp.netitextpdf-generation

解决方案


推荐阅读