The following example demonstrates how to move a page to a new location in a PDF document.
It also briefly covers how to create the initial document, add new pages to it, add text on the various pages of the document & then save said document with the newly edited content.
| Move pages to a new location in the PDF document (C#) |
Copy Code |
|---|---|
public static void MovePage() { Console.WriteLine( "=== Move PAGE ===" ); var outputFileName = "MovePage.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( "Move Page", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Defines a TextStyle that will be used for the following texts. var textStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 18 ); // Adds text on the first Page. page.AddText( "This text is on page 1 .", new Point( 30, 100 ), textStyle ); for( int i = 1; i < 5; ++i ) { // Adds a new Page. page = pdfoutput.Pages.Add(); // Adds text on the added Page. page.AddText( $"This text is on the page {i + 1}.", new Point( 30, 100 ), textStyle ); } // Moves the Page located at index 1 to its new position at index 3. pdfoutput.Pages.Move( 1, 3 ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |