In This Topic
    Add text in a specific location
    In This Topic

    Introduction

    The following example demonstrates how to add text in a specific location of a PDF document.

    It also briefly covers how to stylize the added text using a standard font.


    Add text at a specific location in a PDF document (C#)
    Copy Code
    public static void AddTextInArea()
    {
      Console.WriteLine( "=== ADD TEXT IN AREA ===" );
      var outputFileName = "AddTextInArea.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 in area", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Defines a font for the text that will be added.
        var textFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier );
    
        // Adds text at (100,100) and makes sure it will be contained in a 400x400 area on the Page.
        page.AddText( "In the corner of a firstclass smoking carriage, Mr. Justice Wargrave, lately retired from the bench, puffed at a cigar and ran an interested eye through the political news in the Times. He laid the paper down and glanced out of the window. They were running now through Somerset. He glanced at his watch another two hours to go. He went over in his mind all that had appeared in the papers about Indian Island. that had appeared in the papers about Indian Island.",
                      new Rectangle( new Point( 100, 100 ),
                      new Size( 400, 400 ) ),
                      TextStyle.WithFont( textFont, 12 ) );
    
        // Saves the output document.
        pdfoutput.Save();
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }

     Learn More

    To know more about how to stylize text, please consult the "Use Standard Fonts", "Use True Type Fonts" or "Use Composite True Type Fonts" examples.

     

    See Also