The following example demonstrates how to add pages to a PDF document, as well as how to add text on the newly added pages.
It is important to note that a PDF document will always have at least one page; otherwise, it cannot exist. This means that whenever a PDF document is created, a page is automatically created with it; this page will act as the first page in said document.
| Add pages to a PDF document (C#) |
Copy Code |
|---|---|
public static void AddPages() { Console.WriteLine( "=== ADD PAGES ===" ); var outputFileName = "AddPages.pdf"; var outputPath = PagesSample.PagesSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the document. var page = pdfoutput.Pages.First(); // Adds a title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Pages", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Defines a TextStyle to use for the next texts. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 18 ); // Adds text on the first Page. page.AddText( "This text is on FIRST page.", new Point( 30, 100 ), textStyle ); // Adds a new Page. page = pdfoutput.Pages.Add(); // Adds text on the second Page. page.AddText( "This text is on SECOND page.", new Point( 30, 100 ), textStyle ); // Adds a new Page. page = pdfoutput.Pages.Add(); // Adds text on the third Page. page.AddText( "This text is on THIRD page.", new Point( 30, 100 ), textStyle ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |