In This Topic
    Add paragraphs
    In This Topic

    Introduction

    The following example demonstrates how to add a paragraph of text into a PDF document.

    It also briefly covers how to add a margin around a page, as well as how to stylize text using both a text style & a paragraph style.

    Add paragraphs in a PDF document (C#)
    Copy Code
    public static void AddParagraph()
    {
      Console.WriteLine( "=== ADD PARAGRAPH ===" );
      var outputFileName = "AddParagraph.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 titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Add paragraph", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Defines a margin for the Page.
        page.PageSettings = PageSettings.Default.SetMargins( new Margins( 50, 25, 50, 10 ) );
    
        // Sets a TextStyle, a ParagraphStyle and adds text in the Paragraph, starting 40 pts after the top margin.
        var textFont = pdfoutput.Fonts.GetStandardFont( StandardFontType.Courier );
        var textStyle = TextStyle.WithFont( textFont, 12 );
        var paragraphStyle = new ParagraphStyle( ParagraphHorizontalAlignment.Justify, LineSpacingType.Single );
        page.AddParagraph( TextsSample.SuperText, 40, textStyle, paragraphStyle );
    
        // 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