如何使用itextsharp将UTF-8字符写入pdf文件?

| 我在Google上尝试了很多,但找不到。 任何帮助表示赞赏。 请在下面找到代码:
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\");
    }
    
已邀请:
您要将HTML转换为PDF吗?如果是这样,您应该注意这一点,否则不要紧。我问的唯一原因是,您对获得ѭ1的最后评论使我想到了这一点。如果您是,请查看此帖子: iTextSharp 5波兰语字符 同样,有时当人们说“ Unicode”时,他们真正想做的就是将诸如Wingdings之类的符号转换为PDF。如果您的意思是,请查看这篇文章,并知道Unicode和Wingding Symbols确实无关。 iTextSharp中的Unicode符号 这是一个完整的工作示例,它使用两种方式编写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代码点。使用字体时,还需要指定“ 3”。我不完全知道这是什么意思,但是这里有一些讨论:iTextSharp国际文本     

要回复问题请先登录注册