Xceed PDF Library for .NET Documentation v1.0
In This Topic
    Use standard fonts
    In This Topic

    Introduction

    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
    public static void 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}" );
      }
    }

     Learn More

    To know more about how to add text at a specific location in the document, please consult the "How To Add Text In A Specific Location" example.

    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.