This section explains how to create PDF documents using PDF Graphics .NET. Read the sections below for an introduction to creating documents and pages.
Let's start by creating a document that displays the text 'Hello, World!'. See the code below:
This code will create a new PDF document with the sentence 'Hello, World!' in the top-left hand corner of the page.
It starts off by including the PdfCreative.Pdf.Graphics namespace, which will save us a bit of typing later on. Most of the classes in PDF Graphics .NET can be found in that namespace.
The first line in the Main() method creates a new Graphics object, and tells it to write to a file called HelloWorld.pdf.
After that, the second line creates a new font that uses the Times New Roman type. Note that unlike some graphics libraries, such as System.Drawing in the .NET Framework, the size of the font is not part of the font definition. Only the type name is.
The statement in line 3 draws the 'Hello, World!' text onto the page. The text will appear in the top-left hand corner of the page. Just like with System.Drawing, the coordinate system is based on the top-left hand corner, so graphics are drawn to the right and down the page. The text is drawn with a black brush, at 10 points. For more information on colours and font sizes, see the Painting Graphics section of this documentation.
And finally, the last line closes the Graphics object and finalises the PDF document. It is important to always call the Close() method, or else no PDF file will be created.
As the 'Hello, World!' example shows, it is not necessary to explicitly create the first page in your document; one will be created for you. However, to manually create a page, and create subsequent ones, the Graphics class has a NewPage() method to do just that. See the example below (classes and methods omitted for brevity);
This example code creates three pages, and writes some text on each. Note that the same font object is used throughout the PDF document; there is no need to instantiate a new one for every page you create.
The NewPage() method has several overloads that allow you to specify the kind of page you like. For example, to create a DIN A3 page, use the following statement:
For information on more advanced page creation, including their orientation in a PDF Viewer application, see the Advanced use section.