The following example demonstrates how to use standard fonts to stylize text in a PDF document.
Use standard fonts to write text in a PDF document (C#)
Copy Code
publicstaticvoid UseStandardFont()
{
Console.WriteLine( "=== USE STANDARD FONT ===" );
var outputFileName = "UseStandardFont.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 Standard Fonts", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Adds text with standard Courier Bold Oblique font.
var helveticaTextStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.CourierBoldOblique ), 15d );
page.AddText( "This document contains the Courier Bold Oblique font.", new Point( 50, 100 ), helveticaTextStyle );
// Adds text with standard Times Bold font in green.
var timesBoldTextStyle = TextStyle.WithFontAndColor( pdfoutput.Fonts.GetStandardFont( StandardFontType.TimesBold ), 30d, Brushes.Green );
page.AddText( "But also a green Times Bold font.", new Point( 20, 150 ), timesBoldTextStyle );
// Saves the output document.
pdfoutput.Save();
Console.WriteLine( $"Info exported to path: {outputFileName}" );
}
}
To know how to use True Type fonts instead of standard fonts to stylize text in a PDF document, please consult the "How To Use True Type Fonts" example.