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.
Please note that only simple True Type fonts files are currently supported. Complex files with too many characters (about 250) are not currently supported, even though they might work.
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;
// Create a pdf document.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Get the first page of the document.
var page = pdfoutput.Pages.First();
// Add title.
var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Use Simple TrueType Fonts", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Add Text with a local ttf font file.
var ttfTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetFontFromFile( FontsSample.FontsSampleResourcesDirectory + @"ALGER.ttf" ), 18 );
page.AddText( "This document contains the Alger font.", new Point( 20, 100 ), ttfTextStyle );
// Add Text with local ttf font file in Red.
var redTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetFontFromFile( FontsSample.FontsSampleResourcesDirectory + @"BOOKOSB.ttf" ), 15, Color.Red );
page.AddText( "But also a red Bookman Old Style Bold font.", new Point( 50, 150 ), redTextStyle );
// Save the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}