The following example demonstrates how to use standard fonts to stylize text in a PDF document. It also briefly covers how to place the text at a specific location in the document, as well as how to set characters' size.
Add text to a PDF document (C#)
Copy Code
publicstaticvoid AddText()
{
Console.WriteLine( "=== ADD TEXT ===" );
var outputFileName = "AddText.pdf";
var outputPath = TextsSample.TextsSampleOutputDirectory + outputFileName;
// Create a pdf document.
using( var pdfoutput = PdfDocument.Create( outputPath ) )
{
// Get the first page.
var page = pdfoutput.Pages.First();
// Add Title.
var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
page.AddParagraph( "Add text", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
// Define a textStyle to use to add text.
var textFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier );
// Add text at specific position with different font sizes.
page.AddText( "This is the first Text", new Point( 25, 150 ), TextStyle.WithFont( textFont, 12 ) );
page.AddText( "This is the second Text", new Point( 200, 200 ), TextStyle.WithFont( textFont, 18 ) );
page.AddText( "This is the third Text", new Point( 100, 300 ), TextStyle.WithFont( textFont, 24 ) );
// Save 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.