2009-08-24 133 views
9

我想通過使用itextsharp庫將gridview導出爲pdf。問題在於pdf文檔中缺少一些土耳其字符,例如©,ı,Ş,等等。用於導出PDF的代碼是:HTML到pdf某些字符丟失(itextsharp)

protected void LinkButtonPdf_Click(object sender, EventArgs e) 
    { 
     Response.ContentType = "application/pdf"; 
     Response.ContentEncoding = System.Text.Encoding.UTF8; 
     Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf"); 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     System.IO.StringWriter stringWrite = new StringWriter(); 
     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
     GridView1.RenderControl(htmlWrite); 
     StringReader reader = new StringReader(textConvert(stringWrite.ToString())); 
     Document doc = new Document(PageSize.A4); 
     HTMLWorker parser = new HTMLWorker(doc); 
     PdfWriter.GetInstance(doc, Response.OutputStream); 
     doc.Open(); 
     parser.Parse(reader); 
     doc.Close(); 
    } 
    public static string textConvert(string S) 
    { 
     if (S == null) { return null; } 
     try 
     { 
      System.Text.Encoding encFrom = System.Text.Encoding.UTF8; 
      System.Text.Encoding encTo = System.Text.Encoding.UTF8; 
      string str = S; 
      Byte[] b = encFrom.GetBytes(str); 
      return encTo.GetString(b); 
     } 
     catch { return null; } 
    } 

注意:當我要插入字符到pdf文檔時,缺少的字符會顯示在其中。我用這段代碼插入字符:

BaseFont bffont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     Font fontozel = new Font(bffont, 12, Font.NORMAL, new Color(0, 0, 0)); 
     doc.Add(new Paragraph("İİııŞŞşşĞĞğğ", fontozel)); 

回答

7

Finaly我想我找到的解決方案,我爲了顯示土耳其字符改變iTextSharp的源代碼中的一些。(土耳其字符代碼是cp1254)

我在源代碼中將[public const string CP1254 = "Cp1254";]添加到[BaseFont.cs]中。

之後,我修改了[FactoryProperties.cs]。我改變了這個;

public Font GetFont(ChainedProperties props) 
{ 
I don't write the whole code.I changed only code below; 
------------Default itextsharp code------------------------------------------------------ 
    if (encoding == null) 
       encoding = BaseFont.WINANSI; 
      return fontImp.GetFont(face, encoding, true, size, style, color); 
-------------modified code-------------------------------------------- 

      encoding = BaseFont.CP1254; 
      return fontImp.GetFont("C:\\WINDOWS\\Fonts\\arial.ttf", encoding, true, size, style, color); 
} 

。經過我編譯新的DLL,缺少的字符顯示。

+0

這完美的作品。特別是當出口gridview到PDF。非常感謝。 – bselvan 2013-01-31 09:55:22

+0

謝謝。它在一開始並不起作用。除您之外,我搜索整個項目並更改所有「BaseFont.WINANSI」 - >「BaseFont.CP1254」。然後它完美地工作。 – VVovoVV 2013-10-07 08:45:02

2

我不熟悉iTextSharp庫;不過,您似乎將gridview組件的輸出轉換爲字符串並從該字符串中讀取以構建PDF文檔。你也有一個從UTF-8到UTF-8的奇怪轉換。

從我所看到的(鑑於您的GridView正在輸出字符)如果您將字符輸出到字符串,他們將在內存中表示爲UTF-16。您可能需要將此字符串直接傳遞到PDF庫(例如,如何按原樣傳遞原始UTF-16 .NET字符串"İııŞŞşşĞĞğğ")。

+0

對不起,UTF-8到UTF-8的轉換,它只是試用,我在寫問題的時候就這樣忘記了。我嘗試了不同的組合,比如UTF-8到Unicode ,Unicode到UTF-8等... – slayer35 2009-08-24 13:36:43

+0

我想說的是:當你不做任何轉換時會發生什麼?沒有轉換的 – paracycle 2009-08-24 13:42:20

+0

,字符仍然丟失。 – slayer35 2009-08-24 13:56:07

1

土耳其編碼

CultureInfo ci = new CultureInfo("tr-TR"); 
Encoding enc = Encoding.GetEncoding(ci.TextInfo.ANSICodePage); 

如果您輸出HTML,在頁面頂部嘗試不同的DOCTYPE標籤。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

請注意,如果使用HTML,您可能需要HTMLEncode字符。

Server.HTMLEncode()

HttpServerUtility.HtmlEncode()

+0

我做了你說的,但沒有任何改變。我認爲我們必須改變htmlworker的字體,但不知道如何。 謝謝 – slayer35 2009-08-25 08:55:41

5

無需更改源代碼。

試試這個:

iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica","Cp1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);  

iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL); 
+0

@Jason Plank這個字體是否可以分配給LoadTagStyle中的html標籤主體? – Alex 2012-07-18 10:55:56

+0

這應該是答案! (fontFont.HELVETICA,「Cp1254」,BaseFont.NOT_EMBEDDED,24,Font.BOLD,BaseColor.BLACK); – kaya 2015-11-23 13:43:56

1
BaseFont bF = BaseFont.CreateFont("c:\\arial.ttf","windows-1254",true); 
Font f = new Font(bF,12f,Font.NORMAL); 
Chunk c = new Chunk(); 
c.Font = f; 
c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç"); 
document.Add(c); 

在第一行,你可以寫這些,而不是 「窗口-1254」 的。所有作品:

  • Cp1254
  • ISO-8859-9
  • 窗口-1254
2

您可以使用:

iTextSharp.text.pdf.BaseFont Vn_Helvetica = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", "Identity-H", iTextSharp.text.pdf.BaseFont.EMBEDDED); 
iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(Vn_Helvetica, 12, iTextSharp.text.Font.NORMAL); 
+0

@Jason Plank這個字體是否可以分配給LoadTagStyle中的html標籤主體? – Alex 2012-07-18 10:57:25

+0

@Alex我不知道,我只修復了這個答案的格式。不幸的是,這個答案的作者似乎並沒有在這裏活躍起來。 – 2012-07-19 14:21:57

+0

@Jason Plank是的,非常糟糕 – Alex 2012-07-19 14:27:02

-1

我解決了這個問題。我可以提供我的另一種解決方案類型...

try 
{ 
     BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf", 
      BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     Document document = new Document(PageSize.A4, 25, 25, 30, 30); 
     PdfWriter writer = PdfWriter.GetInstance(document, fs); 

     Font f = new Font(bf, 12f, Font.NORMAL); 
     // Open the document to enable you to write to the document 
     document.Open(); 
     // Add a simple and wellknown phrase to the document 
     for (int x = 0; x != 100; x++) 
     { 
      document.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü",f)); 
     } 

     // Close the document 
     document.Close();   
} 
catch(Exception) 
{ 

} 
0

不要更改iTextSharp的源代碼。定義一種新的風格:

 var styles = new StyleSheet(); 
     styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma"); 
     styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, "Identity-H"); 

然後將它傳遞給HTMLWorker.ParseToList方法。

0

我終於找到了這個問題的靈魂,通過這個你可以打印所有的土耳其字符。

String htmlText = html.ToString();

Document document = new Document(); 

    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/"); 
    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create)); 
    document.Open(); 

    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document); 
    FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")), "Garamond"); // just give a path of arial.ttf 
    StyleSheet css = new StyleSheet(); 
    css.LoadTagStyle("body", "face", "Garamond"); 
    css.LoadTagStyle("body", "encoding", "Identity-H"); 
    css.LoadTagStyle("body", "size", "12pt"); 

    hw.SetStyleSheet(css); 

    hw.Parse(new StringReader(htmlText)); 
3

非常感謝所有誰張貼的樣本..

我用從CodeProject下面的解決方案,並有土耳其字符集由於字體..問題

如果使用htmlworker要註冊的字體,並傳遞給htmlworker

http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3

 StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet(); 
       styles.LoadTagStyle("h3", "size", "5"); 
       styles.LoadTagStyle("td", "size", ".6"); 
       FontFactory.Register("c:\\windows\\fonts\\arial.ttf", "Garamond"); // just give a path of arial.ttf 
       styles.LoadTagStyle("body", "face", "Garamond"); 
       styles.LoadTagStyle("body", "encoding", "Identity-H"); 
       styles.LoadTagStyle("body", "size", "12pt"); 
       using (var htmlViewReader = new StringReader(htmlText)) 
       { 
        using (var htmlWorker = new HTMLWorker(pdfDocument, null, styles)) 
        { 
         htmlWorker.Parse(htmlViewReader); 
        } 
       }