【发布时间】:2018-06-04 21:00:15
【问题描述】:
我想在 PDF 中显示带有 html 标签的一列(来自数据源,使用 GridView)。 我希望对 HTML 进行解码,以便在 PDF 中不会打印文字 html tags 。这是我的代码:
在 GridView_RowDataBound 事件中:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
{
for (int j = 0; j < 6; j++)
{
decodeHTML = HttpUtility.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
GridView1.Rows[i].Cells[j].Text = decodeHTML;
}
}
}
然后将 HTML 解码的 gridview 添加到 PDF 单元格中:
Phrase cellText = new Phrase(GridView1.Rows[i].Cells[j].Text, baseFontNormal);
iTextSharp.text.pdf.PdfPCell cell = new PdfPCell(cellText);
if (j == 3) cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
它不是以 PDF 格式显示数据,而是在 HTML 页面中(在浏览器中)显示它们。但是,仅当我删除 GridView_RowDataBound 事件时,它才会显示为 PDF 文件,但随后数据将打印文字 html 标签,我不希望这样。
【问题讨论】:
-
“不是以 PDF 格式显示数据,而是在 HTML 页面(在浏览器中)显示它们。” - 如果显示的是 html 页面而不是 pdf ,您显然返回 html 而不是 pdf。因此,请查看写入响应的代码。
-
但是当我删除(注释掉)GridView_RowDataBound 事件中的代码时,它会按预期导出为 PDF 文件。
-
嗨@mkl,我认为OP对
HtmlDecode方法的假设是错误的。 -
@mkl nope,事实并非如此,Bruno Lowagie 是正确的
-
是的,因为“它在 HTML 页面中显示它们”不是一个准确的描述,该页面是一个 pdf 页面,仅显示了一些 html 源...
标签: html asp.net pdf gridview itext