FontFromFile.cs
- //
- // This code is part of Document Solutions for PDF demos.
- // Copyright (c) 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
- {
- // 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.
- public class FontFromFile
- {
- public int CreatePDF(Stream stream)
- {
- const string sample = "The quick brown fox jumps over the lazy dog.";
- const string fnRegular = "NotoSerif-Regular.ttf";
- const string fnBoldItalic = "NotoSerif-BoldItalic.ttf";
-
- var fRegular = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", fnRegular)) ??
- throw new Exception($"Could not load font {fnRegular}");
-
- // Use the loaded font to draw some text:
- var tf = new TextFormat() { Font = fRegular, FontSize = 12 };
- var doc = new GcPdfDocument();
- var g = doc.NewPage().Graphics;
- g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, new PointF(18, 72));
- // We can change the font size:
- tf.FontSize += 2;
- g.DrawString($"Font {fRegular.FontFamilyName}, size {tf.FontSize}: {sample}", tf, new PointF(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, new PointF(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)) ??
- throw new Exception($"Could not load font {fnBoldItalic}");
- tf.Font = fBoldItalic;
- tf.FontStyle = GCTEXT.FontStyle.Regular;
- g.DrawString($"Font {fBoldItalic.FontFamilyName}, real Bold Italic: {sample}", tf, new PointF(18, 72 * 4));
- // Done:
- doc.Save(stream);
- return doc.Pages.Count;
- }
- }
- }
-