PDFsharp Sample: HelloWorld

Modified on 2009/08/11 12:09 by Thomas Hövel — Categorized as: PDFsharp Samples, Samples

<< Previous^UpNext >>
This sample is the obligatory Hello World program. It shows how to create a PDF document with one page and the text "Hello, World!" written in its center.

Here is a similar Visual Basic .NET version of HelloWorld.

PDF Output File

See the PDF file created by this sample: HelloWorld.pdf (3 kB)

Source Code

This is the whole source code needed to create the PDF file:
using System;
using System.Diagnostics;
using System.IO;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
namespace HelloWorld
  /// This sample is the obligatory Hello World program.
  class Program
    static void Main(string[] args)
      // Create a new PDF document
      PdfDocument document = new PdfDocument();
      document.Info.Title = "Created with PDFsharp";
      // Create an empty page
      PdfPage page = document.AddPage();
      // Get an XGraphics object for drawing
      XGraphics gfx = XGraphics.FromPdfPage(page);
      // Create a font
      XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
      // Draw the text
      gfx.DrawString("Hello, World!", font, XBrushes.Black,
        new XRect(0, 0, page.Width, page.Height),
      // Save the document...
      const string filename = "HelloWorld.pdf";
      // ...and start a viewer.