//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Drawing;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGCTEXT = GrapeCity.Documents.Text;usingGCDRAW = GrapeCity.Documents.Drawing; namespaceDsPdfWeb.Demos.Basics{// This short sample demonstrates how a Font can be loaded from a file// and used in your code to render text.// The sample relies on font files NotoSerif-Regular.ttf and// NotoSerif-BoldItalic.ttf to be present in the Resources/Fonts folder.// // NOTE 1: When Font.FromFile() is used, the actual data is loaded on demand,// so that usually a Font instance will not take too much space.// The situation is different for fonts created using Font.FromArray()// and Font.FromStream() methods - in those cases the whole font is// immediately loaded into memory. The font will still be parsed// only on demand, but memory consumption is slightly higher,// so using Font.FromFile() should generally be preferred.//// NOTE 2: When different Font instances (created using any of the static ctors// mentioned above) are used to render text in a PDF, each instance will result// in embedding a separate subset of glyphs even if the glyphs are the same,// because DsPdf has no way of knowing that two different Font instances // represent the same physical font. So either make sure that only one Font instance// is created for each physical font, or better yet use the FontCollection class// to add the fonts you need, and specify them via TextFormat.FontName.publicclassFontFromFile {publicintCreatePDF(Stream stream) {conststringsample = "The quick brown fox jumps over the lazy dog.";conststringfnRegular = "NotoSerif-Regular.ttf";conststringfnBoldItalic = "NotoSerif-BoldItalic.ttf"; var fRegular = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnRegular)) ??thrownewException($"Could not load font {fnRegular}"); // Use the loaded font to draw some text:var tf = newTextFormat() { Font = fRegular, FontSize = 12 };var doc = newGcPdfDocument();var g = doc.NewPage().Graphics; g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, newPointF(18, 72));// We can change the font size: tf.FontSize += 2; g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, newPointF(18, 72 * 2));// We can tell DsPdf to emulate bold and/or italic style with a regular font: tf.FontStyle = GCTEXT.FontStyle.BoldItalic; g.DrawString($"Font {fRegular.FontFamilyName}, emulated Bold Italic: {sample}", tf, newPointF(18, 72 * 3));// But of course rather than emulated, it is better to use real bold/italic fonts.// So finally, get a real bold italic font and print a line with it:var fBoldItalic = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnBoldItalic)) ??thrownewException($"Could not load font {fnBoldItalic}"); tf.Font = fBoldItalic; tf.FontStyle = GCTEXT.FontStyle.Regular; g.DrawString($"Font {fBoldItalic.FontFamilyName}, real Bold Italic: {sample}", tf, newPointF(18, 72 * 4));// Done: doc.Save(stream);return doc.Pages.Count; } }}