My first PDF file
Version 6.1.0
In this article
- Font resolving
- Creating the PDF document
- Add a page
- Drawing graphic
- Drawing text
- Finish and save the document
- Complete source
- Try MigraDoc
This article describes step by step how to create a simple PDF document.
Font resolving
If you use the Core build, PDFsharp needs to know how to map a font. You should set a FontResolver that supports all the fonts that your application needs. Here, we set the FailsafeFontResolver that only supports Segoe WP. That's enough for our "Hello, World!" demonstration, but if your code requests "Times New Roman" or "Courier New", the PDF will always use Segoe WP.
if (Capabilities.Build.IsCoreBuild)
GlobalFontSettings.FontResolver = new FailsafeFontResolver();
(A font resolver can only be set once. For more details on font resolvers see Font resolving)
Creating the PDF document
In the next step, you create a new, empty PDF document in memory.
var document = new PdfDocument();
You can add some meta information.
document.Info.Title = "My first document created with PDFsharp";
document.Info.Author = "myself";
document.Info.Subject = "Just an example to demonstrate PDFsharp";
Add a page
Your document currently does not contain any pages. Next you add a page to the document.
var page = document.AddPage();
The function AddPage adds a new and blank page to the document. You can access the pages with the Pages property.
The default size of the page depends on your country/region settings. If you use the metric system for measurements, the size is set to A4 (21 x 29.7 cm²). Otherwise the page size is set to Letter US (8.5 x 11 inch²). The default orientation of the page is portrait.
You can change the page size and orientation with the following properties.
page.Size = PageSize.A3;
page.Orientation = PageOrientation.Landscape;
Alternatively you can use the Width and Height properties to set the page size individually.
Drawing graphic
To draw text or graphic on this page, you create an XGraphics
object.
var gfx = XGraphics.FromPdfPage(page);
You can think of the graphics object as a canvas for the page.
The following code draws two diagonal lines from the corners of the page using a red pen.
var width = page.Width;
var height = page.Height;
gfx.DrawLine(XPens.Red, 0, 0, width, height);
gfx.DrawLine(XPens.Red, width, 0, 0, height);
Now we draw a circle filled with a white brush at the center of the page.
var r = width / 5;
gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r));
Drawing text
To draw text, we first have to create a font.
var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
Now you can use this font to draw some text.
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
Finish and save the document
When your document is finished, you can save it to a file.
var filename = IOUtility.GetTempPdfFullFileName("HelloWorldSample");
document.Save(filename);
After saving the document, it must not be modified anymore.
Complete source
Here is the complete source for reference.
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Quality;
using PdfSharp.Snippets.Font;
// Create a new PDF document.
var document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
document.Info.Subject = "Just a simple Hello-World program.";
// Create an empty page in this document.
var page = document.AddPage();
// Get an XGraphics object for drawing on this page.
var gfx = XGraphics.FromPdfPage(page);
// Draw two lines with a red default pen.
var width = page.Width.Point;
var height = page.Height.Point;
gfx.DrawLine(XPens.Red, 0, 0, width, height);
gfx.DrawLine(XPens.Red, width, 0, 0, height);
// Draw a circle with a red pen which is 1.5 point thick.
var r = width / 5;
gfx.DrawEllipse(new XPen(XColors.Red, 1.5), XBrushes.White, new XRect(width / 2 - r, height / 2 - r, 2 * r, 2 * r));
// Create a font.
var font = new XFont("Times New Roman", 20, XFontStyleEx.BoldItalic);
// Draw the text.
gfx.DrawString("Hello, PDFsharp!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save the document...
var filename = IOUtility.GetTempPdfFullFileName("HelloWorldSample");
document.Save(filename);
// ...and start a viewer.
PdfFileUtility.StartPdfViewer(filename);
Link to this example on GitHub
Link to this Program.cs file on GitHub
Try MigraDoc
PDFsharp gives you low level features to create a PDF document. If you want to create a structured document with sections, headers, footers, paragraphs, tables, etc. try MigraDoc. With MigraDoc, you describe your document with an object model which is formatted and rendered to PDF.