C# 使用 iTextSharp 產生 PDF

Hello World

        public byte[] GetHelloWorldPdf()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();

                pdfDoc.Add(new Paragraph(14f, "HelloWorld"));

                pdfDoc.Close();

                return stream.ToArray();
            }
        }

在 Asp.Net Mvc 回傳檔案 : 

        [HttpPost]
        public FileResult HelloWorldPdf()
        {
            IiTextSharpService service = new ITextSharpService();

            byte[] content = service.GetHelloWorldPdf();

            return File(content, "application/pdf", $"HelloWorld.pdf");
        }


設定字型與中文問題

iTextSharp 預設字型無法處理中文,所以輸入 Hello ! 你好 ! 會變成 Hello ! !

使用 BaseFont 跟 Font 設定字體

            string chFontPath = "c:\\windows\\fonts\\kaiu.ttf"; //標楷體                           
            BaseFont baseFont = BaseFont.CreateFont(chFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font chtFont = new Font(baseFont,12);

            using (MemoryStream stream = new MemoryStream())
            {
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();
                pdfDoc.Add(new Paragraph(14f, "HelloWorld ! 你好!", chtFont));
                pdfDoc.Close();
                return stream.ToArray();
            }

執行結果 : 


換頁

pdfDoc.NewPage();

句子片段

chunk、phrase、paragraph

  • paragraph 文章段落
  • phrase 句子
  • chunk 片段

 

pdfDoc.Add(new Paragraph(14f, "HelloWorld"));

 


將 Html 轉成 PDF

使用 XMLWorkerHelper

        public byte[] GetHelloWorldPdf()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                pdfDoc.Open();

                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, GetHtmlContent(), GetStyleCss(), Encoding.UTF8,
                    new UnicodeFontFactory());

                pdfDoc.Close();

                return stream.ToArray();
            }
        }

GetHtmlContent

        public MemoryStream GetHtmlContent()
        {
            string content = @"
<html>
<head></head>
<body>
<p>Hello World ! 你好!</p>
</body>
</html>
";
            byte[] byteArray = Encoding.UTF8.GetBytes(content);
            MemoryStream sr = new MemoryStream(byteArray);

            return sr;

GetStyleCss

        private MemoryStream GetStyleCss()
        {
            string content = @"
p{
  color : red;
}
";
            byte[] byteArray = Encoding.UTF8.GetBytes(content);
            MemoryStream sr = new MemoryStream(byteArray);
            return sr;
        }

UnicodeFontFactory 部分(從網路上直接拷貝來用的)

    public class UnicodeFontFactory : FontFactoryImp
    {

        private static readonly string KaiuPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
            "KAIU.TTF");//標楷體


        public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color,
            bool cached)
        {
            BaseFont baseFont = BaseFont.CreateFont(KaiuPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            return new Font(baseFont, size, style, color);
        }
    }

 

執行結果 :


參考 : 

http://www.cc.ntu.edu.tw/chinese/epaper/0015/20101220_1509.htm

https://ithelp.ithome.com.tw/articles/10190232

 

 

 

 

print

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *