0

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

Deepika
  • 290
  • 1
  • 5
  • 17
  • Can you show use the value of `cellText`? There is no reason why iText would duplicate text, hence the problem is probably caused by the fact that the text occurs twice in the `cellText` string. – Bruno Lowagie Jun 18 '15 at 06:27
  • (Thanks Bruno! at last someone took notice of the question, and that too the owner of the itextsharp! I feel Privilidged. thanks Again.) cellText is beng passed as a parameter while creating new pdf cell. in this line: iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12, cellText, font)); Also the duplicacy is observed only in hyperlink column. i have sevral other column where the texts renders just fine. – Deepika Jun 18 '15 at 08:05
  • The screen shot makes it clear that you are adding an HTML string to a `PDFCell`. You shouldn't be surprised that this HTML string is added literally: you aren't parsing the HTML into an iText object. – Bruno Lowagie Jun 18 '15 at 08:41

2 Answers2

1

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Bruno for making me understand the flaw in my understanding. I parsed the html of cellText and it worked – Deepika Jun 18 '15 at 09:32
0

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);
                }
Deepika
  • 290
  • 1
  • 5
  • 17