沒辦法顯示中文的原因是,iTextSharp元件預設的字型只有針對英文
聽說新版的dll檔有改善這個問題,但元件要換版本通常問題很多
例如要付費阿~跟現有架構不相容阿~大家不喜歡換阿~
所以我嘗試在現有的狀況下把中文生出來...
參考網址
以前我超討厭看程式內容,能整段貼上絕對不往內容多花一點心思
現在報應來了哈哈哈
前人的研究顯示,要解決這個問題,需要對輸入內文的字型進行設定....
因為我是用GridView轉到PDF,勢必要用每個格子進行字型設定
前台程式碼
記得AutoGenerateColumns這個一定要設False,從DB對應撈取的資料,不要讓他自己抓,不然gvPDF.Columns.Count會一輩子掛0
<div>
<asp:gridview ID="gvPDF" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="中文" HeaderText="中文" />
<asp:BoundField DataField="中文說明" HeaderText="中文說明" />
</Columns>
</asp:gridview>
<asp:Button ID="btnPDF" runat="server" Text="即時產生PDF" onclick="btnPDF_Click" />
</div>
後台程式碼
要引用以下東西,少的請拜神自己找~
我想應該沒有用什麼特別的東西...至少我開一個白的專案只引用iTextSharp都可以跑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = TmpData();
this.gvPDF.DataSource = dt;
this.gvPDF.DataBind();
this.gvPDF.CaptionAlign = TableCaptionAlign.Left;
}
protected void btnPDF_Click(object sender, EventArgs e)
{
DataTable dt = TmpData();
this.gvPDF.DataSource = dt;
this.gvPDF.DataBind();
this.gvPDF.CaptionAlign = TableCaptionAlign.Left;
if (gvPDF.Rows.Count > 0)
{
//設定字型
string fontPath = System.Environment.GetEnvironmentVariable("windir") + @"\Fonts\KAIU.ttf";
string fontName = "標楷體";
//設定檔名
string root = @"\StudiesProvePrint";
if (!Directory.Exists(root))
{
Directory.CreateDirectory(root);
}
string _fileName = root + String.Format(@"\{0}.pdf", DateTime.Now.ToString("yyyyMMdd"));
//開啟pdf檔案
Document document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_fileName, FileMode.Create));
document.Open();
//設定字型
FontFactory.Register(fontPath);
Font fontContentText = FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, 10f);
PdfPTable tbBody = new PdfPTable(1);
Rectangle pageSize = document.PageSize;
PdfPTable ptb = new PdfPTable(gvPDF.Columns.Count);
//表頭,我猜換成Dataatable說不定也可以?
for (int h = 0; h < gvPDF.Columns.Count; h++)
{
ptb.AddCell(new Phrase(gvPDF.HeaderRow.Cells[h].Text, fontContentText));
}
ptb.HeaderRows = 1;
//表格內文
for (int i = 0; i < gvPDF.Rows.Count; i++)
{
for (int j = 0; j < gvPDF.Columns.Count; j++)
{
ptb.AddCell(new Phrase(gvPDF.Rows[i].Cells[j].Text.Replace(" ", ""), fontContentText));
}
}
document.Add(ptb);
//關閉pdf檔
document.Close();
writer.Close();
//將檔案傳給User
FileInfo downloadFile = new FileInfo(_fileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
System.Web.HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", downloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(downloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}
private DataTable TmpData()
{
DataTable dtr = new DataTable();
dtr.Columns.Add("中文");
dtr.Columns.Add("中文說明");
dtr.Rows.Add(new object[] { "這是中文", "A1" });
dtr.Rows.Add(new object[] { "還是中文", "A2" });
dtr.Rows.Add(new object[] { "就要中文", "A3" });
return dtr;
}
沒有留言:
張貼留言