Monday, October 13, 2008

Creating PDF file in C Sharp (C#): example "Hello, PDF!"

This page contains a step-by-step tutorial that will teach you how to create PDF files from Visual C# application using PDF Creator Pilot library.

1) Install PDF Creator Pilot library on your computer and run Microsoft Visual Studio.

Go to New menu and click Project... to create a new project

Microsoft Development Environment main menu

You will see the New Project Wizard. Select Windows Application in Visual C# Projects:

New Project wizard

2) New project will be created. Visual C# will generate new project Project1 and will automatically open the main form of the newly generated project:

Visual C# project form

3) Now we should add a reference to PDF Creator Pilot to be able to use pdf library in C#.NET project. Select Project in main menu and click Add Reference:

Project menu

Switch to COM tab and find PDF Creator Pilot in the list of available COM objects:

Add Reference dialog: list of available COM objects

Click the Select button to add a reference to PDF Creator Pilot and click OK

4) Now we should implement the code that will generate "Hello, PDF!" PDF document function. Double-click Form1 to create Form1_Load procedure:

VB.NET Application Form

The source code editing window will be opened:

Form1_Load procedure edit window

5) Now you have to implement the code that will generate your PDF file using PDF Creator Pilot.

To generate PDF document you have to do the following:

1. connect to the PDF Creator Pilot library;
2. set the file name for your PDF document;
3. draw "Hello, PDF!" message on the PDF page;
4. disconnect from the library.

Here is the source code:

private void Form1_Load(object sender, System.EventArgs e)


PDFCreatorPilot3Lib.PDFDocument3Class PDF = new PDFCreatorPilot3Lib.PDFDocument3Class();

// initialize PDF Engine

PDF.StartEngine("demo@demo", "demo");

// set filename

PDF.FileName = "TestCSHARP.PDF";

PDF.AutoLaunch = true; // auto-open generated pdf document

// start document generation


// draw "HELLO, PDF" message on the current PDF page

PDF.PDFPAGE_SetActiveFont("Verdana", true, false, false, false, 14, 0);

PDF.PDFPAGE_TextOut(10, 20, 0, "HELLO, PDF!");

// finalize document generation



This function will generate PDF document and save it as "TestCSHARP.PDF" file in the application's folder.

Hint: You can simply copy the source code from the snippet above and then paste it in the Visual C# code editor:

Visual C# source code editor

6) Press F5 to run the application (you can also use "Debug" | "Start" menu command).
Visual C# will run the application. The application will generate "TestCSHARP.PDF" document.

If you have any PDF viewer (for example, Adobe Reader) installed on your computer, the library will launch it to open the generated PDF document:

Adobe Reader window

PDF Creator Pilot can be used with different languages to generate PDF files. See "Hello, PDF" example for:

* Visual Basic Script (VBScript)
* Visual Basic (VB)
* Visual Basic.NET (VB.NET)
* C Sharp (C#)
* Active Server Pages (ASP)
* Active Server Pages for .NET (ASP.NET)
* Delphi
* Visual C++

See also:

* PDF documents and PDF files. Introduction and short description
* PDF Creator Pilot technology
* PDF Creator Pilot FAQ

You can download the source code for the example project here: Visual Studio 2003 (8 Kb), Visual Studio 2005 (6.5 Kb).


Sandra said...

You can create, read, modify and even convert PDF files by using Aspose.PDF for .NET Library. It was recommended to me by one of my facebook friends and i am using it since and haven't got any problem with this library.