BoldItalicEmulation.cs
//
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos.Basics
{
// 通常のフォントを使用するときに太字または斜体のエミュレーションを制御する方法を示します。
public class BoldItalicEmulation
{
public int CreatePDF(Stream stream)
{
var fc = new FontCollection();
// フォントディレクトリを登録
// fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
// または、より細かく制御するために個別のフォントを登録
fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerifJP-Regular.ttf"));
fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerifJP-Bold.ttf"));
fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Italic.ttf"));
fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-BoldItalic.ttf"));
var doc = new GcPdfDocument();
var g = doc.NewPage().Graphics;
var rc = Common.Util.AddNote(
"TextFormat.FontStyleでは、太字または斜体のフォントを適用するのではなく、" +
"太字または斜体のエミュレーションを有効にすることができます。", doc.Pages.Last);
// テキストの挿入位置。
var ip = new PointF(rc.Left, rc.Bottom + 36);
var tf = new TextFormat();
// 非太字/非斜体のフォントを取得します。
tf.Font = fc.FindFamilyName("Noto Serif JP", false, false);
tf.FontSize = 16;
g.DrawString($"通常のフォント: {tf.Font.FullFontName}", tf, ip);
ip.Y += 36;
// 同じ(標準の)フォントを使用して、太字と斜体をエミュレートした文字列を描画します。
tf.FontStyle = GCTEXT.FontStyle.Bold;
g.DrawString($"太字をエミュレーション: {tf.Font.FullFontName}", tf, ip);
ip.Y += 36;
tf.FontStyle = GCTEXT.FontStyle.Italic;
g.DrawString($"斜体をエミュレーション: {tf.Font.FullFontName}", tf, ip);
ip.Y += 36;
tf.FontStyle = GCTEXT.FontStyle.BoldItalic;
g.DrawString($"太字と斜体をエミュレーション: {tf.Font.FullFontName}", tf, ip);
ip.Y += 36;
//
// フォントの "実際の" 太字/斜体の変種を使用して、いくつかの文字列を描画します。
tf.FontStyle = GCTEXT.FontStyle.Regular;
tf.Font = fc.FindFamilyName("Noto Serif JP", true, false);
g.DrawString($"実際の太字フォントを適用: {tf.Font.FullFontName}", tf, ip);
ip.Y += 36;
tf.Font = fc.FindFamilyName("Noto Serif JP", false, false);
g.DrawString($"実際の斜体フォントを適用: ", tf, ip);
ip.X += 202;
tf.Font = fc.FindFamilyName("Noto Serif", false, true);
g.DrawString($"{tf.Font.FullFontName}", tf, ip);
ip.X -= 202;
ip.Y += 36;
tf.Font = fc.FindFamilyName("Noto Serif JP", false, false);
g.DrawString($"実際の太字/斜体フォントを適用: ", tf, ip);
ip.X += 238;
tf.Font = fc.FindFamilyName("Noto Serif", true, true);
g.DrawString($"{tf.Font.FullFontName}", tf, ip);
// PDF ドキュメントを保存します。
doc.Save(stream);
return doc.Pages.Count;
}
}
}