BoldItalicEmulation.cs
C# VB
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//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{ // Sample shows how to control bold and/or italic emulation when using normal fonts. public class BoldItalicEmulation { public int CreatePDF(Stream stream) { var fc = new FontCollection(); // Register the font directory for convenience: // fc.RegisterDirectory(Path.Combine("Resources", "Fonts")); // or individual fonts for more control: fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf")); fc.RegisterFont(Path.Combine("Resources", "Fonts", "NotoSerif-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 enables using Bold and/or Italic emulation\n" + "on a regular (not a bold/italic) font.", doc.Pages.Last); // Text insertion point: var ip = new PointF(rc.Left, rc.Bottom + 36); var tf = new TextFormat(); // We specifically get a non-bold/non-italic version of the font: tf.Font = fc.FindFamilyName("Noto Serif", false, false); tf.FontSize = 16; g.DrawString($"Regular Noto Serif font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // Draw some strings using the same (regular) font but emulating bold and italic: tf.FontStyle = GCTEXT.FontStyle.Bold; g.DrawString($"Bold emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.FontStyle = GCTEXT.FontStyle.Italic; g.DrawString($"Italic emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.FontStyle = GCTEXT.FontStyle.BoldItalic; g.DrawString($"Bold+Italic emulation using font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // // Now we render some strings using the "real" bold/italic variants of the font: tf.FontStyle = GCTEXT.FontStyle.Regular; tf.Font = fc.FindFamilyName("Noto Serif", true, false); g.DrawString($"Using real bold font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.Font = fc.FindFamilyName("Noto Serif", false, true); g.DrawString($"Using real italic font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; tf.Font = fc.FindFamilyName("Noto Serif", true, true); g.DrawString($"Using real bold italic font: {tf.Font.FullFontName}", tf, ip); ip.Y += 36; // Done: doc.Save(stream); return doc.Pages.Count; } }}