2011-05-24 50 views
13

我已經嘗試了很多關於谷歌,但沒能找到..如何使用itextsharp將UTF-8字符寫入pdf文件?

任何幫助表示讚賞..

PLZ幫助..

請找到下面的代碼: -

protected void Page_Load(object sender, EventArgs e) 
    { 
     StreamReader read = new StreamReader(@"D:\queryUnicode.txt", Encoding.Unicode); 
     string str = read.ReadToEnd(); 

     Paragraph para = new Paragraph(str); 

     FileStream file = new FileStream(@"D:\Query.pdf",FileMode.Create); 

     Document pdfDoc = new Document(); 
     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, file); 

     pdfDoc.Open(); 
     pdfDoc.Add(para); 
     pdfDoc.Close(); 

     Response.Write("Pdf file generated"); 
    } 
+0

你看到了什麼問題?如果它缺少字符,那麼看看這裏:http://stackoverflow.com/questions/1322303/html-to-pdf-some-characters-are-missing-itextsharp – Nick 2011-05-24 12:27:03

+0

是的,字符在pdf中缺少,但我有已經看到並試過這個鏈接,當我下載itextsharp的源代碼時,它沒有'FactorySettings.cs'文件。而且,他正在使用「arial.ttf」,我想要UTF-8字符。 – teenup 2011-05-24 12:35:33

+0

實際上,當我將其中的字符串改爲「UTF-8」編碼時,從中提取字符串的記事本保存爲ANSI編碼,現在這些字符以pdf格式顯示爲「æ」。 – teenup 2011-05-24 12:47:58

回答

19

您是否將HTML轉換爲PDF?如果是這樣,你應該注意,否則沒關係。我問的唯一原因是你最後的評論æ讓我覺得這一點。如果你是,看看這篇文章: iTextSharp 5 polish character

此外,有時當人們說「Unicode」時,他們真正想要做的是將Wingdings等符號轉換爲PDF。如果你的意思是檢查這篇文章,並且知道Unicode和Wingding符號確實沒有任何關係。 Unicode symbols in iTextSharp

下面是一個完整的工作示例,它使用兩種方法編寫Unicode字符,一種使用字符本身,另一種使用C#轉義序列。確保以支持寬字符的格式保存文件。本示例使用iTextSharp 5.0.5。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //Create our document object 
      Document Doc = new Document(PageSize.LETTER); 

      //Create our file stream 
      using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read)) 
      { 
       //Bind PDF writer to document and stream 
       PdfWriter writer = PdfWriter.GetInstance(Doc, fs); 

       //Open document for writing 
       Doc.Open(); 

       //Add a page 
       Doc.NewPage(); 

       //Full path to the Unicode Arial file 
       string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); 

       //Create a base font object making sure to specify IDENTITY-H 
       BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 

       //Create a specific font object 
       Font f = new Font(bf, 12, Font.NORMAL); 

       //Write some text, the last character is 0x0278 - LATIN SMALL LETTER PHI 
       Doc.Add(new Phrase("This is a test ɸ", f)); 

       //Write some more text, the last character is 0x0682 - ARABIC LETTER HAH WITH TWO DOTS VERTICAL ABOVE 
       Doc.Add(new Phrase("Hello\u0682", f)); 

       //Close the PDF 
       Doc.Close(); 
      } 
     } 
    } 
} 

使用iTextSharp時,您必須確保您使用的字體支持您要使用的Unicode代碼點。使用字體時,您還需要指定IDENTITY-H。我不完全知道這意味着什麼,但這裏有一些討論:iTextSharp international text

+0

@Chris,你寫的字符,即ɸ和\ u0682即將到來,但我的文件中的字符仍然以代碼形式出現。例如字符'æ'即將作爲'æ','ø'即將作爲'ø'。這些在GridView中的網頁上很好,我在響應內容類型中使用了UTF-8。 – teenup 2011-05-25 05:06:12

+0

@Chris,如果我使用代碼編寫這些字符,例如'new Phrase(「æøå」,font)',那麼它們會很好。但是我從保存爲UTF8編碼的文本文件中提取文本,使用StreamReader將其轉換爲字符串,然後將此字符串傳遞給'Phrase構造函數'。 – teenup 2011-05-25 06:10:03

+0

@Puneet Dudeja,你是在談論一個gridview和一個文本文件,你正在使用?這些是你需要在你的問題中進一步解釋的兩個獨立的東西。對於文本文件,你確定它的UTF-8編碼(你用十六進制編輯器檢查過它)嗎?你如何獲取文本文件?文件系統還是網絡?對於gridview,你如何獲取?請使用一些代碼編輯您的文章,以便我們能夠更好地幫助您。 – 2011-05-25 12:53:24

相關問題