PDFsharp Sample: Page Sizes

Modified on 2015/09/14 10:22 by Thomas Hövel — Categorized as: PDFsharp Samples, Samples

<< Previous^UpNext >>
This sample shows a document with different page sizes.

Note:  You can set the size of a page to any size using the Width and Height properties. This sample just shows the predefined sizes.

PDF Output File

See the PDF file created by this sample: output.pdf (40 kB)

Screen Shots

Here is a sample screen shot:

The PageSize sample

Source Code

This is the whole source code needed to create the PDF file:
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create a font
XFont font = new XFont("Times", 25, XFontStyle.Bold);
PageSize[] pageSizes = (PageSize[])Enum.GetValues(typeof(PageSize));
foreach (PageSize pageSize in pageSizes)
  if (pageSize == PageSize.Undefined)
  // One page in Portrait...
  PdfPage page = document.AddPage();
  page.Size = pageSize;
  XGraphics gfx = XGraphics.FromPdfPage(page);
  gfx.DrawString(pageSize.ToString(), font, XBrushes.DarkRed,
    new XRect(0, 0, page.Width, page.Height),
  // ... and one in Landscape orientation.
  page = document.AddPage();
  page.Size = pageSize;
  page.Orientation = PageOrientation.Landscape;
  gfx = XGraphics.FromPdfPage(page);
  gfx.DrawString(pageSize + " (landscape)", font,
    XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),
// Save the document...
const string filename = "PageSizes_tempfile.pdf";
// ...and start a viewer.

Note: The samples on this site usually show and discuss code snippets only. The complete source code of the samples with solutions for Visual Studio is available from the download area on CodePlex.