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; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var page = pdfoutput.Pages.First(); // Add title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Pages", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Define a TextStyle to use for next texts. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 18 ); // Add Text on first page. page.AddText( "This text is on FIRST page.", new Point( 30, 100 ), textStyle ); // Add a new page. page = pdfoutput.Pages.Add(); // Add Text on second page. page.AddText( "This text is on SECOND page.", new Point( 30, 100 ), textStyle ); // Add a new page. page = pdfoutput.Pages.Add(); // Add Text on third page. page.AddText( "This text is on THIRD page.", new Point( 30, 100 ), textStyle ); // Save the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |