Create PDF document using C#

This resource shows how to create a PDF document using C#.

Step 1: First create an instance of document object


Document myDocument= new Document(PageSize.A4.Rotate());

Step 2: Now create a writer that listens to this doucment and writes the document to desired Stream.

PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

Step 3: Open the document now using

myDocument.Open();

Step 4: Now add some contents to the document

myDocument.add( new Paragraph ( "First Pdf File made by Salman using iText"));

Step 5: Remember to close the documnet

myDocument.close();




// Code

using System;
using System.IO;
using System.Diagnostics;

using iTextSharp.text;
using iTextSharp.text.pdf;

public class iTextDemo
{
public static void Main()
{
Console.WriteLine("iText Demo");

// step 1: creation of a document-object
Document myDocument = new Document(PageSize.A4.Rotate());

try
{

// step 2:
// Now create a writer that listens to this doucment and writes the document to desired Stream.

PdfWriter.GetInstance(myDocument, new FileStream("Salman.pdf", FileMode.Create));

// step 3: Open the document now using
myDocument.Open();

// step 4: Now add some contents to the document
myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));

}
catch(DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch(IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}

// step 5: Remember to close the documnet

myDocument.Close();
}
}


Comments

Author: Gaurav Kumar Arora09 Aug 2008 Member Level: Gold   Points : 2

Hi Divya,
This is a good one shot using I/O Text but it works only when you have PDF utility installed on your system.

What about if one hasn't installed the utility on his/her system.

I have gone through the same the Code is quite interesting and bug free but it has some miscrepancies or say dependencies.

Thanks & regards,
Gaurav Arora

Author: Gaurav Kumar Arora09 Aug 2008 Member Level: Gold   Points : 2

Hi Divya,
This is a good one shot using I/O Text but it works only when you have PDF utility installed on your system.

What about if one hasn't installed the utility on his/her system.

I have gone through the same the Code is quite interesting and bug free but it has some miscrepancies or say dependencies.

Thanks & regards,
Gaurav Arora

Author: Himasagar Kutikuppala12 Aug 2008 Member Level: Silver   Points : 2

Hi Gaurav
Yes, iTextSharp DLL required to create the PDF file.
This is open source library only, so you can download from the following link and enjoy..
http://downloads.sourceforge.net/itextsharp/itextsharp-4.0.2-dll.zip?modtime=1173357588&big_mirror=0

For more details about iTextSharp & PDF file creation, Go through the following link
http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx

Regards
Himasagar Kutikuppala

Author: Jaiswar Vipin Kumar R.28 Aug 2008 Member Level: Bronze   Points : 2

This application is really grate but as you said the it is open source. But there no provision to download the source code.
Because this open source (as you said) DLL may breaking security of the i.e. if I run application by using this DLL, so may be this DLL sending use information via mail internally to unwanted users.

May I am wring, Hoping I am wrong on this discussion, but if it is really true then where is source code?

Thanks & Regards
Jaiswar Vipin Kumar R.