[]
        
(Showing Draft Content)

Font

To work with a PDF document, you need a library that supports different kinds of fonts. GcPdf provides support for following font types:

  • OpenType

  • TrueType

  • WOFF

To make sure that any of these listed fonts used in a layout can be viewed as it is after downloading or saving the file, GcPdf library provides the following techniques:

  • Automatic Font Embedding: Ensures the fonts used in a PDF document are displayed as it is intended even if the fonts are not installed on a machine.

  • Font Fallback: Used when a specified (in the user's source code) font does not have glyphs for the characters to be rendered in the text.

While creating a PDF file, you may want to use fonts other than the standard fonts. To do so, usually you need to add a font from C:\Windows\Fonts directory. Registering the whole directory every time you want to use different fonts can become unmanageable and time consuming. GcPdf library solves this issue with FontCollection class, that adds global font management services to your application. The FontCollection class provides font related services to different program modules. Fonts can be registered with a FontCollection via RegisterFont or RegisterDirectory methods. Both these methods register disk files, and do not load the actual fonts into memory. This saves space and improves performance as fonts are loaded only when needed.

Using Standard PDF Fonts

GcPdf supports the 14 standard fonts that are mentioned in the PDF specification 1.7 (section 9.6.2). To use these standard PDF fonts, specify one of the standard fonts using the StandardFonts enum members.

public void CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var page = doc.NewPage();
var g = page.Graphics;

var textFormat = new TextFormat()
   {
      Font = StandardFonts.HelveticaBold,
      FontSize = 14
   };
// Render text using DrawString method
g.DrawString("1. Test string.", textFormat, new PointF(72, 72));
// Save Document
doc.Save(stream);
}

Using Font from File

To use an external font file for applying fonts:

  1. Create a new font from a font file, using the FromFile method of the Font class.

  2. Use the font (for example, Gabriola) to render a text with the DrawString method of GcPdfGraphics class.

    public void CreatePDF(Stream stream)
    {
        GrapeCity.Documents.Text.Font gabriola = GrapeCity.Documents.Text.Font.FromFile("Gabriola.ttf");
    
        // Now that we have our font, use it to render some text
        TextFormat tf = new TextFormat()
        {
            Font = gabriola,
            FontSize = 16
        };
        GcPdfDocument doc = new GcPdfDocument();
        GcPdfGraphics g = doc.NewPage().Graphics;
        g.DrawString("Sample text drawn with font {gabriola.FontFamilyName}.",
         tf, new PointF(72, 72));
    
        // Save Document
        doc.Save(stream);
    }

Font Embedding

To embed font in a PDF file, you can use the FontEmbedMode property provided by the GcPdfDocument class. By default, font subsets containing only glyphs used in the document, are embedded. However, this can be changed and set to embed full font using FontEmbedMode enum, which leads to huge file size.

// Use GcPdfDocument object to set the FontEmbedMode
doc.FontEmbedMode = FontEmbedMode.EmbedFullFont;

Font Collection

To use FontCollection:

  1. Create an instance of the FontCollection class.

  2. Register font from a specified file using RegisterFont method of FontCollection class.. Also, you can register one or more directories containing fonts using the RegisterDirectory method of FontCollection class.

  3. Assign the font collection instance to GcGraphics.FontCollection property.

  4. Use DrawString method of GcPdfGraphics class to render text and specify the font name stored in font collection.

    public void CreatePDF(Stream stream)
    {
        // Create a FontCollection instance:
        FontCollection fc = new FontCollection();
    
        // Get the font file using RegisterFont method:
        fc.RegisterFont("georgia.ttf");
    
        // Generate a sample document using the font collection to provide fonts:
        var doc = new GcPdfDocument();
        var page = doc.Pages.Add();
        var g = page.Graphics;
    
        // 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 Georgia bold, drawn by GcGraphics.DrawString() method.",
            new TextFormat() { FontName = "georgia", FontSize = 10 },
            new PointF(72, 72 * 4));
        // Done:
        doc.Save(stream);
    }

Fallback Fonts

To use fallback fonts:

  1. Create an instance of the GcPdfDocument class.

  2. Get the list of fallback font families using GetFallbackFontFamilies method of SystemFontCollection class.

  3. Add the list of fallback font families to global SystemFonts using AppendFallbackFontFamilies method of SystemFontCollection class.

  4. Load your fallback file that includes Japanese glyphs using AppendFallbackFonts method of SystemFontCollection class.

  5. Use DrawString method to render Japanese text.

    public void CreatePDF(Stream stream)
    {
        // Set up GcPdfDocument:
        GcPdfDocument doc = new GcPdfDocument();
        GcPdfGraphics g = doc.NewPage().Graphics;
    
        // Set up some helper vars for rendering lines of text:
        const float margin = 36;
        PointF ip = new PointF(margin, margin);
    
        // Initialize a text format with one of the standard fonts. Standard fonts are minimal
        // and contain very few glyphs for non-Latin characters.
        TextFormat tf = new TextFormat() { Font = StandardFonts.Courier, FontSize = 14 };
    
        // Get the list of fallback font families:
        string[] fallbacks = FontCollection.SystemFonts.GetFallbackFontFamilies();
    
        // Add the original list of fallback font families to global SystemFonts:
        FontCollection.SystemFonts.AppendFallbackFontFamilies(fallbacks);
    
        // On some systems, default system fallbacks might not provide Japanese glyphs,
        // so we add our own fallback:
        Font arialuni = Font.FromFile(Path.Combine("Resources", "Fonts", "ARIALUNI.TTF"));
        FontCollection.SystemFonts.AppendFallbackFonts(arialuni);
    
        // As the fallback fonts are available, the Japanese text will render
        // correctly as an appropriate fallback will have been found:
        g.DrawString("Sample text with fallbacks available: あなたは日本語を話せますか?", tf, ip);
        ip.Y += 36;
    
        // Done:
        doc.Save(stream);
    }

Enumerate Fonts

To list all fonts in a PDF document along with some of the key font properties, use the following code example. The example code loads the PDF document into a temporary document to get the listing of all fonts and creates a Font object from each of those PDF fonts, and reports whether the operation succeeded.

// Open an arbitrary PDF, load it into a temp document and get all fonts:
using (var fs = new FileStream(Path.Combine("Resources", "PDFs", "Test.pdf"),
    FileMode.Open, FileAccess.Read))
{
    var doc1 = new GcPdfDocument();
    doc1.Load(fs);
    var fonts = doc1.GetFonts();
    tl.AppendLine($"Total of {fonts.Count} fonts found in {sourcePDF}:");
    tl.AppendLine();
    int i = 0;
    foreach (var font in fonts)
    {
        var nativeFont = font.CreateNativeFont();
        tl.Append($"{i}:\tBaseFont: {font.BaseFont}; IsEmbedded: {font.IsEmbedded}.");
        tl.AppendParagraphBreak();
        if (nativeFont != null)
            tl.AppendLine($"\tCreateNativeFont succeeded, family: {nativeFont.FontFamilyName};" +
                $" bold: {nativeFont.FontBold}; italic: {nativeFont.FontItalic}.");
        else
            tl.AppendLine("\tCreateNativeFont failed");
        tl.AppendLine();
        ++i;
    }
    tl.PerformLayout(true);

Advanced Features

GcPdf library supports variety of fonts that can work with multilingual characters to write a PDF in different languages. It also provides support for font features along with special characters, such as End-User Defined Characters (EUDC) support, surrogates, ligatures, and Unicode characters.

For more information about implementation of font features using GcPdf, see GcPdf sample browser.