Xceed PDF Library for .NET Documentation v1.0
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, as well as a chosen character size & color.


    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;
    
      // 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 in area", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Define a font for the text to add.
        var textFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier );
    
        // Add text at (100,100) and make sure it will be contained in a 400x400 area on the page.
        var lastWordLayout = 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 ) );
    
        // Add text to show details about text added in area.
        var infoLayout = page.AddText( $"AddText details: ",
                                      new Point( 25, lastWordLayout.EndTextPosition.Y + 50 ),
                                      TextStyle.WithFontAndColor( textFont, 12, Color.Red ) );
    
        page.AddText( $"Last Word Position: {lastWordLayout.EndTextPosition}, Number of lines: {lastWordLayout.NumberOfLinesWritten}, Text Height: {lastWordLayout.TextHeight}",
                      new Point( 25, infoLayout.EndTextPosition.Y + ( 12 * 1.5 ) ),
                      TextStyle.WithFont( textFont, 12 ) );
    
        // Save 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" or "Use True Type Fonts" examples.

     

    See Also