【问题标题】:link coming twice while exporting to pdf using itextsharp使用 itextsharp 导出为 pdf 时链接出现两次
【发布时间】:2015-11-19 06:18:41
【问题描述】:

我的 asp 绑定域:

<asp:BoundField DataField = "SiteUrl" HtmlEncode="false" HeaderText = "Team Site URL" SortExpression = "SiteUrl" ></asp:BoundField>

我的 itextsharpcode

for (int i = 0; i < dtUIExport.Rows.Count; i++)
        {
            for (int j = 0; j < dtUIExport.Columns.Count; j++)
            {
                if (j == 1)
                { continue; }

                    string cellText = Server.HtmlDecode(dtUIExport.Rows[i][j].ToString());
                    //  cellText = Server.HtmlDecode((domainGridview.Rows[i][j].FindControl("link") as HyperLink).NavigateUrl);
                    // string cellText = Server.HtmlDecode((domainGridview.Rows[i].Cells[j].FindControl("hyperLinkId") as HyperLink).NavigateUrl);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                    font.Color = new BaseColor(domainGridview.RowStyle.ForeColor);
                    iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));

                    pdfTable.AddCell(cell);
            }
        }

domainGridview 是网格名称。但是我正在使用数据表处理 pdf。 超链接是这样来的

http://dtsp2010vm:47707/sites/TS1>http://dtsp2010vm:47707/sites/TS1

如何撕掉附加链接? 编辑:我添加了pdf文件的截图

【问题讨论】:

  • 你能显示使用cellText 的值吗? iText 没有理由重复文本,因此问题可能是由于文本在 cellText 字符串中出现两次。
  • (谢谢布鲁诺!终于有人注意到了这个问题,而且 itextsharp 的所有者也是如此!我觉得有特权。再次感谢。) cellText 在创建新的 pdf 单元格时作为参数传递.在这一行: iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font));此外,仅在超链接列中观察到重复性。我还有其他几列文本呈现得很好。
  • 屏幕截图清楚地表明您正在将 HTML 字符串添加到 PDFCell。你不应该对这个 HTML 字符串是按字面添加的感到惊讶:你没有将 HTML 解析为 iText 对象。

标签: pdf gridview itext


【解决方案1】:

您最初的问题没有得到答案,因为它具有误导性。您声称​​链接会出现两次,但事实并非如此。从这个角度来看,链接显示为 HTML 语法:

<a href="http://stackoverflow.com">http://stackoverflow.com</a>

这是存储在cellText 参数中的单个链接 的HTML 定义。

您正在将此内容添加到PdfPCell,就好像它是一个简单的string。 iText 按原样呈现此 string 并不会让您感到惊讶。如果 iText 不显示,那将是一个严重的错误:

<a href="http://stackoverflow.com">http://stackoverflow.com</a>

如果您希望呈现 HTML,例如:http://stackoverflow.com,您需要将 HTML 解析为 iText 对象(例如,&lt;a&gt;-tag 将生成带有锚点的 Chunk 对象) .

解析 HTML 以在 PdfPCell 中使用在以下问题中进行了说明:How to add a rich Textbox (HTML) to a table cell?

当您拥有&lt;a href="http"//stackoverflow.com"&gt;http://stackoverflow.com&lt;/a&gt; 时,您在谈论 HTML,而不仅仅是普通文本。有很大的不同。

【讨论】:

  • 感谢布鲁诺让我理解了我理解中的缺陷。我解析了 cellText 的 html 并且它工作了
【解决方案2】:

我编写此代码是为了实现我的结果。感谢布鲁诺的回答

for (int j = 0; j < dtUIExport.Columns.Count; j++)
            {
                if (j == 1)
                { continue; }
                if (j == 2)
                {
                    String cellTextLink = Server.HtmlDecode(dtUIExport.Rows[i][j].ToString());
                    cellTextLink = Regex.Replace(cellTextLink, @"<[^>]*>", String.Empty);                        
                    iTextSharp.text.Font fontLink = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
                    fontLink.Color = new BaseColor(domainGridview.RowStyle.ForeColor);
                    iTextSharp.text.pdf.PdfPCell cellLink = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellTextLink, fontLink));

                    pdfTable.AddCell(cellLink);
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 2023-03-12
    • 2011-12-31
    相关资源
    最近更新 更多