The following example demonstrates how ajust the page settings.
It also covers how to get those page settings & use them as a default setting for newly created pages.
| Set the page settings (C#) |
Copy Code |
|---|---|
public static void SetPageSettings() { Console.WriteLine( "=== SET PAGE SETTINGS ===" ); var outputFileName = "SetPageSettings.pdf"; var outputPath = PagesSample.PagesSampleOutputDirectory + outputFileName; // Creates a PdfDocument. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the document. var firstPage = pdfoutput.Pages.First(); // Sets the first Page settings to be an A3 format. firstPage.PageSettings = PageSettings.Predefined[ PageFormatType.A3 ]; // Adds a title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); firstPage.AddParagraph( "Set Page Settings", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Adds Lorem Ipsum text on the first Page. var paragraphLayout = firstPage.AddParagraph( PagesSample.LoremIpsumText, 100, paragraphStyle: new ParagraphStyle( ParagraphHorizontalAlignment.Justify ) ); // Adds a paragraph at the specific Y position. firstPage.AddParagraph( "_____________________________________________________________", 365 ); firstPage.AddParagraph( "This PDF has the following PageSettings properties:\n\n", 382 ); // Gets the PageSettings for the first Page. var pageSettings = firstPage.PageSettings; // Adds text representing the firstPage's PageSettings. firstPage.AddParagraph( $"FormatType: {pageSettings.FormatType}", 415 ); firstPage.AddParagraph( $"Orientation: {pageSettings.Orientation}", 430 ); firstPage.AddParagraph( $"Width: {pageSettings.Width:F0} points", 445 ); firstPage.AddParagraph( $"Width: {pageSettings.Height:F0} points", 460 ); var margins = pageSettings.Margins; firstPage.AddParagraph( $"Margins:", 475 ); firstPage.AddParagraph( $"- Left: {margins.Left:F0} points", 490 ); firstPage.AddParagraph( $"- Top: {margins.Top:F0} points", 505 ); firstPage.AddParagraph( $"- Right: {margins.Right:F0} points", 520 ); firstPage.AddParagraph( $"- Bottom: {margins.Bottom:F0} points", 535 ); // Sets a PdfDocument.PageSettings so that every new Page added will use these settings. pdfoutput.PageSettings = PageSettings.Default.SetWidth( 400 ).SetHeight( 300 ); // Adds a second Page. var secondPage = pdfoutput.Pages.Add(); // Adds text on the second Page. paragraphLayout = secondPage.AddParagraph( "Text for second page." ); // Gets the PageSettings for the second Page. pageSettings = secondPage.PageSettings; // Adds a paragraph at the specific Y position. secondPage.AddParagraph( "_____________________________________________________________", 45 ); secondPage.AddParagraph( "This PDF has the following PageSettings properties:", 60 ); // Adds text representing the secondPage's PageSettings. secondPage.AddParagraph( $"FormatType: {pageSettings.FormatType}", 105 ); secondPage.AddParagraph( $"Orientation: {pageSettings.Orientation}", 120 ); secondPage.AddParagraph( $"Width: {pageSettings.Width:F0} points", 135 ); secondPage.AddParagraph( $"Width: {pageSettings.Height:F0} points", 150 ); margins = pageSettings.Margins; secondPage.AddParagraph( $"Margins:", 165 ); secondPage.AddParagraph( $"- Left: {margins.Left:F0} points", 180 ); secondPage.AddParagraph( $"- Top: {margins.Top:F0} points", 195 ); secondPage.AddParagraph( $"- Right: {margins.Right:F0} points", 210 ); secondPage.AddParagraph( $"- Bottom: {margins.Bottom:F0} points", 225 ); // Saves the output document. pdfoutput.Save(); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |
The following example demonstrates how ajust the page settings for each page individually.
| Set the page settings (C#) |
Copy Code |
|---|---|
public static void AddMultiplePageSettings() { Console.WriteLine( "=== ADD MULTIPLE PAGE SETTINGS ===" ); string outputFileName = "AddMultiplePageSettings.pdf"; string outputPath = PagesSample.PagesSampleOutputDirectory + outputFileName; Console.WriteLine( $"Creating PDF: {outputFileName}" ); Console.WriteLine( $"Output path: {outputPath}" ); using( var pdfDoc = PdfDocument.Create( outputPath ) ) { // Gets the first Page of the document. var page = pdfDoc.Pages.First(); // Adds a title. var titleStyle = TextStyle.WithFont( pdfDoc.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Add Multiple Page Settings", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Page 0: Default settings. var page0 = pdfDoc.Pages[ 0 ]; // Add Text for PageSettings of Page0. PagesSample.ShowPageSettingsData( page0, "Default Settings" ); // Page 1: Letter Portrait with custom margins. var settings1 = PageSettings.Predefined[ PageFormatType.Letter ] .SetMargins( new Margins( 80, 60, 80, 60 ) ); var page1 = pdfDoc.Pages.Add(); page1.PageSettings = settings1; // Adds text representing the PageSettings of Page1. PagesSample.ShowPageSettingsData( page1, "Letter Portrait with custom Margins" ); // Page 2: Letter Landscape with custom Margins. var settings2 = PageSettings.Predefined[ PageFormatType.Letter ] .SetMargins( new Margins( 80, 60, 80, 60 ) ) .SetOrientation( PageOrientation.Landscape ); var page2 = pdfDoc.Pages.Add(); page2.PageSettings = settings2; // Adds text representing the PageSettings of Page2. PagesSample.ShowPageSettingsData( page2, "Letter Landscape with Custom Margins" ); // Page 3: A4 Portrait with custom margins. var settings3 = PageSettings.Predefined[ PageFormatType.A4 ] .SetMargins( new Margins( 80, 60, 80, 60 ) ); var page3 = pdfDoc.Pages.Add(); page3.PageSettings = settings3; // Adds text representing the PageSettings of Page3. PagesSample.ShowPageSettingsData( page3, "A4 Portrait with Custom Margins" ); // Page 4: A4 Landscape with custom margins. var settings4 = PageSettings.Predefined[ PageFormatType.A4 ] .SetOrientation( PageOrientation.Landscape ) .SetMargins( new Margins( 80, 60, 80, 60 ) ); var page4 = pdfDoc.Pages.Add(); page4.PageSettings = settings4; // Adds text representing the PageSettings of Page4. PagesSample.ShowPageSettingsData( page4, "A4 Landscape with Custom Margins" ); // Page 5: A3 with custom margins. var settings5 = PageSettings.Predefined[ PageFormatType.A3 ] .SetMargins( new Margins( 200, 200, 50, 40 ) ); var page5 = pdfDoc.Pages.Add(); page5.PageSettings = settings5; // Adds text representing the PageSettings of Page5. PagesSample.ShowPageSettingsData( page5, "A3 with Custom Margins" ); // Page 6: Custom Size and Margins. var settings6 = PageSettings.Default .SetSize( 800, 600 ) .SetMargins( new Margins( 80, 60, 80, 60 ) ); var page6 = pdfDoc.Pages.Add(); page6.PageSettings = settings6; // Adds text representing the PageSettings of Page6. PagesSample.ShowPageSettingsData( page6, "Custom Size and Margins" ); // Page 7: A4 Portrait with small margins. var settings7 = PageSettings.Predefined[ PageFormatType.A4 ] .SetMargins( new Margins( 20, 15, 20, 15 ) ); var page7 = pdfDoc.Pages.Add(); page7.PageSettings = settings7; // Adds text representing the PageSettings of Page7. PagesSample.ShowPageSettingsData( page7, "A4 with small Margins" ); // Saves the output document. pdfDoc.Save(); Console.WriteLine( $" PDF successfully created and saved to: {outputPath}" ); } } private static void ShowPageSettingsData( Page page, string title ) { var pageSettings = page.PageSettings; // Adds Paragraph text at the specific Y position. page.AddParagraph( $"Page {page.Id}: {title}", 60 ); page.AddParagraph( $"Width: {pageSettings.Width:F0} points", 90 ); page.AddParagraph( $"Height: {pageSettings.Height:F0} points", 105 ); page.AddParagraph( $"Margins: Left: {pageSettings.Margins.Left:F0}, Top: {pageSettings.Margins.Top:F0}, Right: {pageSettings.Margins.Right:F0}, Bottom: {pageSettings.Margins.Bottom:F0}", 120 ); } | |