2011-05-24 49 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

相关问题