[]
        
(Showing Draft Content)

Use FontCollection

Adding fonts to a PDF document is typically a slow operation. DsPdf provides the FontCollection class which allows you to register fonts or directories during document creation, making it a fast and lightweight process. When using FontCollection, here are some key points and recommended steps:

Step 1: Create an instance of the FontCollection class.

FontCollection is not a static class, so you need an instance to use it. In addition, it's a regular .NET collection of Font objects, so all usual collection manipulation methods (Add, Insert, Remove, etc.) can be used on it.

Step 2: Populate the font collection with fonts using any of the following methods:

  • RegisterDirectory(): Registers all fonts found in a specified directory;

  • RegisterFont(): Registers font(s) found in a specified file;

  • Add(Font): Adds a font instance that you created.

Note that registering directories or fonts with a font collection is a fast and lightweight operation. The font collection does not actually load all the font data when directories or individual fonts are registered with it. Instead, it loads only the minimal info so that it can find and provide fonts quickly and efficiently when needed.

Step 3: Assign your instance of the font collection

Assign your font collection instance to TextLayout.FontCollection (and to GcGraphics.FontCollection if using GcGraphics.MeasureString/DrawString) so that the correct fonts can be found.

Step 4: Select fonts by specifying font names

In your text rendering code, select fonts by specifying font names (TextFormat.FontName, the names must match exactly but the case is not important), font bold and italic flags (TextFormat.FontBold/FontItalic). If a suitable bold/italic version of the requested font is found in the collection, it will be used; otherwise, font emulation will be applied.

FontCollection methods and properties are thread-safe, so once your font collection has been populated, you can cache and share it between sessions and/or modules of your application.

Refer to the following code sample to use the FontCollection class:

public int CreatePDF(Stream stream)
        {
            // Create a FontCollection instance:
            var fc = new FontCollection();
            // Populate it with fonts from the specified directory:
            fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
 
            // Generate a sample document using the font collection to provide fonts:
            var doc = new GcPdfDocument();
            var page = doc.Pages.Add();
            var g = page.Graphics;
 
            // For TextLayout/TextFormat to be able to use a font collection, it must be
            // associated with it like so:
            var tl = new TextLayout(g.Resolution) { FontCollection = fc };
 
            // Render some strings using the different fonts from our collection:
            var tf = new TextFormat() { FontName = "times new roman", FontSize = 16 };
            tl.Append("Using FontCollection to manage fonts and render text\n\n", tf);
            tf.FontSize = 12;
            tl.Append("Text rendered using Times New Roman regular font. \n", tf);
            // Setting a font style (bold or italic) will tell the font collection
            // to search for a suitable font (if it is not found, emulation will be used):
            tf.FontItalic = true;
            tl.Append("Text rendered using Times New Roman italic font. \n", tf);
            // Text format is applied to a text run when the text is appended,
            // so we can re-use the same format, modifying its properties
            // to render differently formatted texts:
            tf.FontBold = true;
            tl.Append("Text rendered using Times New Roman bold italic font. \n", tf);
            tf.FontItalic = false;
            tl.Append("Text rendered using Times New Roman bold font. \n", tf);
            tf.FontName = "segoe ui";
            tl.Append("Text rendered using Segoe UI bold font. \n", tf);
            tf.FontBold = false;
            tl.Append("Text rendered using Segoe UI regular font. \n", tf);
 
            // Apply page settings to the page layout and render the page:
            tl.MaxWidth = page.Size.Width;
            tl.MaxHeight = page.Size.Height;
            tl.MarginAll = 72;
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);
 
            // If using GcGraphics.DrawString/MeasureString, this will allow the TextLayout
            // created internally by GcGraphics to find the specified fonts in the font collection:
            g.FontCollection = fc;
 
            // Use GcGraphics.DrawString to show that the font collection is also used
            // by the graphics once the FontCollection has been set on it:
            g.DrawString("Text rendered using Segoe UI bold, drawn by GcGraphics.DrawString() method.",
                new TextFormat() { FontName = "segoe ui", FontBold = true, FontSize = 10 },
                new PointF(72, tl.ContentRectangle.Bottom + 12));
 
            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }