【发布时间】:2014-03-14 07:27:38
【问题描述】:
我是使用 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;
}
}
}
【问题讨论】:
标签: c# mysql pdf utf-8 itextsharp