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
publicstaticvoid 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}" );
}
}