2015-06-17 30 views
0

我的ASP的BoundField導出爲PDF:鏈接過來的兩倍,而使用iTextSharp的

<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 /網站/ TS1>http://dtsp2010vm:47707/sites/TS1

如何撕開addtional鏈接? 編輯:我已添加pdf文件的屏幕截圖enter image description here

+0

你可以顯示使用'cellText'的值? iText沒有理由重複文本,因此問題可能是由於文本在'cellText'字符串中出現了兩次。 –

+0

(感謝布魯諾!最後有人注意到了這個問題,這也是itextsharp的擁有者!我感到Privilidged。再次感謝。)cellText是beng作爲參數傳遞,同時創建新的pdf單元格。在這一行中:iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(12,cellText,font));此外,僅在超鏈接列中才能觀察到重複。我有幾個其他專欄文本渲染就好了。 – Deepika

+0

屏幕截圖清楚地表明您正在將HTML字符串添加到「PDFCell」。您不應該對這個HTML字符串按字面添加感到驚訝:您不會將HTML解析爲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的對象(例如<a> - 標籤將導致帶有錨點的Chunk對象)。

解析HTML用於在PdfPCell使用以下問題進行了說明:How to add a rich Textbox (HTML) to a table cell?

當你有<a href="http"//stackoverflow.com">http://stackoverflow.com</a>,你所談論的HTML,而不僅僅是普通的文本。有很大的不同。

+0

感謝布魯諾讓我理解我的理解中的缺陷。我解析了cellText的html,它工作 – Deepika

0

我寫了這個代碼來實現我的結果。感謝布魯諾爲您的答案

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