|
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(); |
|