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/.
At the header section of Program.cs file, you need to write the following code to use iTextSharp and IO libraries.
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.
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.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: