ITextSharp in asp.net
This article will explain how to create pdf File using ITextSharp in C# window form application.
Step 1: Create window form application .
![]() |
| Add window Application |
Step2: download "iTextSharp.dll from following urls
and Add a reference of the downloaded "iTextSharp.dll" to the Windows Forms Application
- right Click on References from Solution Explorer
- Click on Add Reference and Browse downloaded itextsharp dll.
- After Selecting Click on OK.
or alternative way to add itextsharp reference is
Right click on application from Solution Explorer and go to Manage NuGet Packages.
Search for itextsharp and install
After Installation Complete itextsharp dll will be added inside References.
Now we are ready to use itextsharp Library.
Step3: Let Add Button on Form1 (Button Name: GeneratePDF and Text as Generate PDF)
First Add the following 3 namespaces to the top of the ".cs" file:
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
Step 4: Double Click on Button and go to its click Event.
and add following code.
Code:
private void GeneratePDF_Click(object sender, EventArgs e)
{
System.IO.FileStream fs = new FileStream("test.pdf", FileMode.OpenOrCreate);
//File will be save inside bin\debug folder
Document document = new Document(iTextSharp.text.PageSize.A4, 40, 40, 30, 25);
//Set Page Size , left,right,top and bottom Margin of pdf file
var HeadingFont = FontFactory.GetFont("Arial", 18, iTextSharp.text.Font.BOLD); //itextsharp bold text
var bodyFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
// Add meta information to the itextsharp document if required
document.AddAuthor("Zeeshan");
document.AddCreator("Zeeshan");
document.AddKeywords("Zeeshan");
document.AddSubject("Document subject - Zeeshan");
document.AddTitle("The document title - Zeeshan");
document.Open();
Paragraph pheading = new Paragraph("itextsharp Pdf File", HeadingFont);
pheading.SpacingAfter = 10f; // use SpacingAfter or SpacingBefore for Adding Space .
document.Add(pheading);
Paragraph pbodytext = new Paragraph("My First test Pdf File",bodyFont);
document.Add(pbodytext);
document.Close();
}
Note: You can provide any name for the generated file and any text that you want to print in the PDF file. For example here I provided "test.pdf" and the text as "My First test Pdf File" and the path also.
if you would like to save file inside folder then use Above line, above line shows test.pdf File will be saved on Generated-pdf Folder that is created inside bin\debug.
// System.IO.FileStream fs = new FileStream(System.IO.Path.GetFullPath("Generated-pdf") + "\\" + "test.pdf", FileMode.OpenOrCreate);
Step 5: Run Application .
Click on Generate Pdf Button.
pdf File will be Create and saved inside application path bin\debug Folder with name test.pdf
Step 6: Open the PDF file and you will see your text in the PDF file.
That's all ...
Let me Know if you need further Help
Next Article i will show you how to add logo, lines and Images inside pdf file.
Thanks for reading Article.









