In This Topic
    Use True Type fonts
    In This Topic

    Introduction

    The following example demonstrates how to use True Type fonts in a PDF document.

    It also briefly covers how to choose the character size & put characters in color using text styles.

    Use simple true type fonts (C#)
    Copy Code
    public static void UseSimpleTrueTypeFont()
    {
      Console.WriteLine( "=== USE SIMPLE TRUE TYPE FONT ===" );
      var outputFileName = "UseSimpleTrueTypeFont.pdf";
      var outputPath = FontsSample.FontsSampleOutputDirectory + outputFileName;
    
      // Creates a PdfDocument.
      using( var pdfoutput = PdfDocument.Create( outputPath ) )
      {
        // Gets the first Page of the document.
        var page = pdfoutput.Pages.First();
    
        // Adds a title.
        var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Use Simple TrueType Fonts", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Adds text with a local ttf font file.
        var ttfTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetFontFromFile( FontsSample.FontsSampleResourcesDirectory + @"ALGER.ttf", FontEmbeddingMode.Simple ), 18 );
        page.AddText( "This document contains the Alger font.", new Point( 20, 100 ), ttfTextStyle );
    
        // Adds text with a local ttf font file in Red.
        var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetFontFromFile( FontsSample.FontsSampleResourcesDirectory + @"BOOKOSB.ttf", FontEmbeddingMode.Simple ), 15, Brushes.Red );
        page.AddText( "But also a red Bookman Old Style Bold font.", new Point( 50, 150 ), redTextStyle );
    
        // Saves the output document.
        pdfoutput.Save();
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }

     Learn More

    To know more about how to add text at a specific location in the document, please consult the "How To Add Text In A Specific Location" example.

    To know how to use standard fonts instead of standard fonts to stylize text in a PDF document, please consult the "How To Use Standard Fonts" example.


    See Also