The following example demonstrates how to add a paragraph 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; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page. var page = pdfoutput.Pages.First(); // Add Title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add paragraph", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Define a margin for the page. page.PageSettings = new PageSettings() { Margins = new Margins( 50, 25, 50, 10 ) }; // Set a textStyle, a paragraph style and add a paragraph text, starting 40 pts after 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 ); // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |