The following example demonstrates how to add 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 stylize it using a text style.
| Get text located in a specific area of a PDF document (C#) |
Copy Code |
|---|---|
public static void AddText() { Console.WriteLine( "=== ADD TEXT ===" ); var outputFileName = "AddText.pdf"; var outputPath = TextsSample.TextsSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page. var page = pdfoutput.Pages.First(); // Adds a title. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add text", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Defines a TextStyle that will be used to add the text. var textFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier ); // Adds text at a specific position, using different font sizes and Brushes. page.AddText( "This is the first Text", new Point( 25, 150 ), TextStyle.WithFontAndBrush( textFont, 12, Brushes.Green ) ); page.AddText( "This is the second Text", new Point( 200, 200 ), TextStyle.WithFontAndBrush( textFont, 18, Brushes.Red ) ); page.AddText( "This is the third Text", new Point( 100, 300 ), TextStyle.WithFontAndBrush( textFont, 24, Brushes.Blue ) ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |