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
doc.Open();
//write a paragraph to the document
doc.Add(new Paragraph("Hello World"));
//close the document
doc.Close();
//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.



Comments

hjk comment

 hjk

jgjh


2016-08-30
lorretadt comment

 lorretadt

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.


2016-06-30
lorretadt comment

 lorretadt

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.


2016-06-30
bnm comment

 bnm

bnnnb


2016-05-24
lorretadt comment

 lorretadt

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/


2016-05-14
dsfdsf comment

 dsfdsf

dsfsdf


2016-04-04
Sodama comment

 Sodama

It is very nice C# to PDF tutorial.


2015-01-12
Omar comment

 Omar

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:

http://www.aspose.com/.net/pdf-component.aspx


2014-04-29
CAPTCHA image



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.