simple way to create pdf document using itextsharp
In this article we will see how to create pdf file using itextsharp on the fly using c#,VB.net and Asp.net.
you can also refer to my articles on ItextSharp in asp.net for adding image into pdf file here and to create table in pdf doc here
Step 1: First of all we have to download the iTextShaprp pdf library. you can download that from here
Step2 : After downloading file add the reference of iTextSharp.dll to your project by right clicking on solution explorer.
Step 3 : Now add the Namespace to your .cs or .vb file
using iTextSharp.text;
using iTextSharp.text.pdf;
Step 4 : Now one whatever click you want to generate the file you can write the below code. in this example i am generating the pdf file on button click event.
Now we will create document
//Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create));
doc.Open();//Open Document to write
Now Write some content to it
//Write some content
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
Add the content to document class object
// Now add the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
Now Close the document
doc.Close(); //Close document
And you are done.
Here is the sample pdf file

And below is the full code used inside button click event in C# example
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
//Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
try
{
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create));
//Open Document to write
doc.Open;
//Write some content
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
// Now add the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
}
catch (DocumentException dex)
{
//Handle document exception
}
catch (IOException ioex)
{
//Handle IO exception
}
catch (Exception ex)
{
//Handle Other Exception
}
finally
{
doc.Close(); //Close document
}
}
Vb.NET Code sample
Protected Sub btnGeneratePDF_Click(ByVal sender As Object, ByVal e As EventArgs)
'Create Document class obejct and set its size to letter and give space left, right, Top, Bottom Margin
Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
Try
Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("c:\Test11.pdf", FileMode.Create))
'Open Document to write
doc.Open
'Write some content
Dim paragraph As New Paragraph("This is my first line using Paragraph.")
Dim pharse As New Phrase("This is my second line using Pharse.")
Dim chunk As New Chunk(" This is my third line using Chunk.")
' Now add the above created text using different class object to our pdf document
doc.Add(paragraph)
doc.Add(pharse)
doc.Add(chunk)
Catch dex As DocumentException
'Handle document exception
Catch ioex As IOException
'Handle IO exception
Catch ex As Exception
'Handle Other Exception
Finally
'Close document
doc.Close()
End Try
End SubCheers!!!
Pankaj