易丰科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4254|回复: 0
打印 上一主题 下一主题

iTextSharp 产生 PDF 中文问题解决范例

[复制链接]

111

主题

117

帖子

3588

积分

论坛元老

Rank: 8Rank: 8

积分
3588
跳转到指定楼层
楼主
发表于 2011-9-27 16:48:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Document document = new Document(PageSize.A4);
            BaseFont bfChinese;
            Font fontChinese;
            string outputText = @"
------------------
Hello World!
哈囉
哈啰
こんにちは

";

            PdfWriter.GetInstance(
              document,
              new FileStream(@"c:\hello.pdf", FileMode.Create)
            );
            document.Open();

            #region 使用BaseFont產生字體
            string fontPath;
            fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\..\Fonts\kaiu.ttf";
            bfChinese = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontChinese = new Font(bfChinese, 16f, Font.BOLD);

            document.Add(new Paragraph("BaseFont方法" + outputText, fontChinese));
            #endregion

            #region 使用FontFactory產生字體
            fontPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\..\Fonts\kaiu.ttf";
            FontFactory.Register(fontPath);
            fontChinese = FontFactory.GetFont("標楷體", BaseFont.IDENTITY_H, 16f, Font.BOLD);

            document.Add(new Paragraph("FontFactory方法" + outputText, fontChinese));
            #endregion

            #region BaseFont VS FontFactory差別
            // 第一段設粗體
            // 第二段換字型與設斜體
            // 第三段設回原字體與斜體

            // BaseFont:初始建立的Style會被延續
            Phrase myPhrase = new Phrase("Hello ! ", new Font(Font.TIMES_ROMAN, 14, Font.BOLD));
            myPhrase.Add(new Phrase("some other font ", new Font(Font.HELVETICA, 14, Font.ITALIC)));
            myPhrase.Add(new Phrase("This is the end of the sentence.\n", new Font(Font.TIMES_ROMAN, 14, Font.ITALIC)));
            document.Add(myPhrase);

            // FontFactory:初始建立的Style不會被延續
            myPhrase = new Phrase("Hello ! ", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, Font.BOLD));
            myPhrase.Add(new Phrase("some other font ", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.ITALIC)));
            myPhrase.Add(new Phrase("This is the end of the sentence.\n", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, Font.ITALIC)));
            document.Add(myPhrase);
            #endregion

            document.Close();
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|易丰科技

GMT+8, 2024-6-2 17:04 , Processed in 0.035758 second(s), 28 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表