link 使用 itextsharp 导出为 pdf 时出现两次

link coming twice while exporting to pdf using itextsharp

我的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 是网格名称。但是我正在使用数据 table 处理 pdf。 hyperlink就这样来了

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

如何撕掉额外的link? 编辑:我添加了pdf文件的截图

您最初的问题没有得到答案,因为它具有误导性。你声称 link 来了两次 ,但事实并非如此。从上看,link 显示为 HTML 语法:

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

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

您正在将此内容添加到 PdfPCell,就好像它是一个简单的 string。 iText 按原样呈现此 string 应该不足为奇。如果 iText 没有显示,那将是一个严重的错误:

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

如果你想要 HTML 被渲染,例如像这样:http://whosebug.com,你需要将 HTML 解析为 iText 对象(例如 <a> -tag 将导致 Chunk 带有锚点的对象)。

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

当你有 <a href="http"//whosebug.com">http://whosebug.com</a> 时,你在谈论 HTML,而不仅仅是普通的文本。有很大的不同。

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

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);
                }