C# tutorial: create pdf file

Create pdf file

In this page, you learn to create a PDF document from scratch by using iTextSharp library in C#. The iText library is available in two versions: the original Java version, and the C# port, iTextSharp. The iTexSharp is a useful library that can be used with C# to create and manipulate PDF documents. To start creating a PDF document in C# by using iTextSharp library, you need to download the library from http://sourceforge.net/projects/itextsharp/files/.

Then create a new Console project in Visual Studio 2010. You will create a libs folder in this Console project folder and extract the iTextSharp library zip file to this libs folder. In the Solution Explorer, right click the References->Add Reference.... In the Add Reference dialog box, select browse. Navigate to the libs folder that contains the iTextSharp libary. Select itextsharp.dll from the list box. Click Ok to add the itextsharp library to the Console project.

create pdf in c#

At the header section of Program.cs file, you need to write the following code to use iTextSharp and IO libraries.

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

To create a simple PHP document, write the C# code shown below in the Main method.

//create a document object
var doc = new Document();
//get the current directory
string path = Environment.CurrentDirectory;
//get PdfWriter object
PdfWriter.GetInstance(doc, new FileStream(path + "/pdfdoc.pdf", FileMode.Create));
//open the document for writing
//write a paragraph to the document
doc.Add(new Paragraph("Hello World"));
//close the document
//view the result pdf file
System.Diagnostics.Process.Start(path + "/pdfdoc.pdf");

By running the program, you will see a PDF document as shown below.

pdf result

The C# code above creates a PDF file called pdfdoc.pdf in the current working folder of the Console project. There are five steps in the PDF file creation:

-Create a PDF document object.

-Get PDFWriter object.

-Open the document.

-Write content to the document.

-Close the document.

You can download the complete source code of the example project from here.


Leeroy Maxwell comment

 Leeroy Maxwell

I am using ZetPDF SDK. It is really easy to develop PDF functionality using this SDK. You can download the SDK from this link. (ZetPDF - PDF library for .NET, Windows Forms, ASP.NET, Mono, WPF, Silverlight)a

Ronald Kaplan  comment

 Ronald Kaplan

ZetPDF.com is the best PDF library in the market. I have tried many of others and they either aren't as simple or simply don't have the features like ZetPDF.com

Kim Black comment

 Kim Black

Such a helpful guide into learning iTextSharp since I see a lot of forum posts about it. You can check out https://zetpdf.com/ as well for pdf generation.

hjk comment



lorretadt comment


i find a free online WPF PDF viewer on rasteredge http://www.rasteredge.com/how-to/vb-net-imaging/pdf-net-wpf-feature-view/, it can view the pdf document for free online.

lorretadt comment


i find a free online WPF PDF viewer on rasteredge http://www.rasteredge.com/how-to/vb-net-imaging/pdf-net-wpf-feature-view/, it can view the pdf document for free online.

bnm comment



lorretadt comment


vb.net wpf display pdf file is what you are trying to do is impossible, what you could try to do is show vb.net create pdf from wpf online was that what you were trying?, if

so, try something like this:http://www.rasteredge.com/how-to/vb-net-imaging/pdf-net-wpf-feature-view/

dsfdsf comment



Sodama comment


It is very nice C# to PDF tutorial.

Omar comment


I would like to recommend the .NET Library i use for managing my PDF files known as Aspose.PDF for .NET. You can create simple or complex PDF file with this lib and it offers many other features. Check it out:



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.