search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

使用PDF元件[itextsharp]到C#專案 新增中文字+顏色+大小 程式 – jashliao部落格

使用PDF元件[itextsharp]到C#專案 新增中文字+顏色+大小 程式

 

資料來源:http://renjin.blogspot.tw/2009/01/using-chinese-fonts-in-itextsharp.html
         http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm

 

00.去GOOGLE 搜尋itextsharp 並且下載

01.解壓 itextsharp-dll-core.zip
    iTextSharp.xml
    itextsharp.dll

02.解壓 itextsharp-dll-pdfa.zip
    itextsharp.pdfa.dll

03.解壓 itextsharp-dll-xtra.zip    
    itextsharp.xtra.dll

04.專案引入三個DLL參考

05.在程式碼中加入引用函式庫語法
    //PDF_start
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    //PDF_end
    
06.建立一個按鈕事件並加入測試程式碼
    private void button1_Click(object sender, EventArgs e)//基本使用測試
    {
        iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);//左,右,上,下 邊界
        iTextSharp.text.pdf.PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(“test.pdf”, FileMode.Create));
        doc.Open();
        Paragraph paragraph = new Paragraph(“this is my first line.”);
        doc.Add(paragraph);
        doc.Close();
    }

07.    建立一個按鈕事件並加入測試程式碼
    private void button2_Click(object sender, EventArgs e)//顯示中文+顏色
    {
        //資料來源:http://renjin.blogspot.tw/2009/01/using-chinese-fonts-in-itextsharp.html
        //http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm
        iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);//左,右,上,下 邊界
        iTextSharp.text.pdf.PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(“test2.pdf”, FileMode.Create));
        iTextSharp.text.pdf.BaseFont bfTW = BaseFont.CreateFont(“01.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//01.ttf是微軟正黑體
        iTextSharp.text.pdf.BaseFont bfCN = BaseFont.CreateFont(“01.ttf”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        iTextSharp.text.Font fontTW = new iTextSharp.text.Font(bfTW, 40, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(51, 0, 153));
        iTextSharp.text.Font fontCN = new iTextSharp.text.Font(bfCN, 12, iTextSharp.text.Font.NORMAL, BaseColor.RED);

        doc.Open();
        Paragraph paragraph = new Paragraph(“this is my first line.”);
        doc.Add(paragraph);
        doc.Add(new Paragraph(“foolish”, fontTW));
        doc.Add(new Paragraph(“難得糊塗”, fontTW));//繁中
        doc.Add(new Paragraph(“难得糊涂”, fontCN));//簡中
        doc.Close();
    }

 

 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦