Deepika
Deepika

Reputation: 300

link coming twice while exporting to pdf using itextsharp

my asp boundfield:

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

My 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 is the grid name. However I am manipulating the pdf using data table. The hyperlink is coming in this way

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

How to rip the addtional link? Edit: i have added the screenshot of pdf fileenter image description here

Upvotes: 0

Views: 220

Answers (2)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Your initial question didn't get an answer because it is rather misleading. You claim link coming twice, but that's not true. From the point of view, the link is shown as HTML syntax:

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

This is the HTML definition of a single link that is stored in the cellText parameter.

You are adding this content to a PdfPCell as if it were a simple string. It shouldn't surprise you that iText renders this string as-is. It would be a serious bug if iText didn't show:

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

If you want the HTML to be rendered, for instance like this: http://stackoverflow.com, you need to parse the HTML into iText objects (e.g. the <a>-tag will result in a Chunk object with an anchor).

Parsing HTML for use in a PdfPCell is explained in the following question: How to add a rich Textbox (HTML) to a table cell?

When you have <a href="http"//stackoverflow.com">http://stackoverflow.com</a>, you are talking about HTML, not just ordinary text. There's a big difference.

Upvotes: 1

Deepika
Deepika

Reputation: 300

I wrote this code for achiveing my result. Thanks Bruno for your answer

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

Upvotes: 0

Related Questions