如何通过使用Itextsharp从Mysql中获取UTF8字符以在C#程序中存储为PDF?

时间:2014-03-14 07:27:38

标签: c# mysql pdf utf-8 itextsharp

我是havig用UTF8编码我的记录的Mysql数据库我能在PHP MyAdmin中看到 正常。 我也可以使用这个数据库和c#prog在google crome上生成o / p。 我正在使用Itextsharp dll生成pdf。 我想将数据库中的数据存储到devnagri中的pdf。 我尝试设置字体,因为我找到了很多教程。 设置字体适用于硬编码字符串,但不适用于数据库。

 public Example1()
        {
            string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
            try
            {
                // Step 1: Creating System.IO.FileStream object
                using (FileStream fs = new FileStream(appRootDir + "/PDFs/" + "Chapter1_Example1.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
                // Step 2: Creating iTextSharp.text.Document object
                using (Document doc = new Document())
                // Step 3: Creating iTextSharp.text.pdf.PdfWriter object
                // It helps to write the Document to the Specified FileStream
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                {
                    // Step 4: Openning the Document
                    doc.Open();


                    MySqlDataReader reader = null;
                    string connectionString = @"Data Source=localhost; Database=Records; User ID=root; Password=''";
                    using (MySql.Data.MySqlClient.MySqlConnection cs = new MySql.Data.MySqlClient.MySqlConnection(connectionString))
                    {
                        string sqlText = "Select * from app";
                        cs.Open();

                        MySqlCommand cmd = new MySqlCommand(sqlText, cs);
                       // var font = FontFactory.GetFont(BaseFont.)
                        BaseFont bf = BaseFont.CreateFont("C:\\Windows\\Fonts\\cprkshn.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

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

                        reader = cmd.ExecuteReader();
                        while (reader.Read())                        
                        {
                            //String s = reader["name"].ToString();

                            doc.Add(new Paragraph(reader["id"] + "---" + reader["name"].ToString() + "¶ããñ‡ãŠÀãè", f));
                        }
                    }

                    // Step 5: Adding a paragraph
                    // NOTE: When we want to insert text, then we've to do it through creating paragraph


                    // Step 6: Closing the Document
                    doc.Close();
                }
            }
            // Catching iTextSharp.text.DocumentException if any
            catch (DocumentException de)
            {
                throw de;
            }
            // Catching System.IO.IOException if any
            catch (IOException ioe)
            {
                throw ioe;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您应该配置连接 - 设置charset UTF8。您可以使用此MySQL命令执行此操作 -

SET NAMES utf8;

首先打开连接后运行此命令。

此外,您可以配置连接字符串,尝试添加Character Set=utf8。结果连接字符串可以是这样的 -

string connectionString = "Data Source=localhost; Database=Records; User ID=root; Password=''; Character Set=utf8";

答案 1 :(得分:0)

您尝试使用的字体不具有梵文Unicode范围,因此无法使用。梵文字符应使用Unicode U+0900 through U+097F以及扩展名U+A8E0 through U+A8FF。您使用的字体具有高阶ASCII区域(128-255)。尝试切换到ARIALUNI.TTF,你应该会看到你的角色出现。

我知道Arial Unicode是一个非常大的字体,但首先尝试它只是为了确保你的代码有效。然后,您可以开始寻找适合您需求的较小字体。