Let us learn to generate PDF using IText Sharp

Raj.Trivedi
Posted by in C# category on for Intermediate level | Points: 250 | Views : 20934 red flag

In this article, we will see how to generate PDF using an Open Source Library provided by Itextsharp

Introduction

Hello Team,

In real world programming we come to a stage where we need to create PDF from the data we have.


In this article we are going to see how to generate the PDF using ITextSharp library.

This library is a part of an opensource project available on www.sourceforge.net.



Objective


Generating the PDF's on the FLY.....

Using the code

Let us start the Code part now.

Create a New Project in Visual Studio -> Select Windows Application.

Drag a Button and Textbox on the Windows from

Set the textbox to Multi-line property to true.

Now we will have to download the Itextsharp library from the below link

http://sourceforge.net/projects/itextsharp/

Once you download the Zip File just extract it to a folder in that you will get multiple zip files.

From the group of multiple zip files extract the Zip File which is named as itextsharp-dll-core check the Image






Once you extract the Zip you will get a Itextsharp.dll and an xml file.Check the image


 

Now go to the project and Right Click on reference and the Itextsharp.dll  to the reference.

We will also have to add name space using system.IO and System.Diagnostics as well for creating the PDF.

and write the following code on the button click event.




// Code Behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;



namespace PDF_Generation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnPDF_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Application.StartupPath;
                Document pdfdoc = new Document(PageSize.A4); // Setting the page size for the PDF

                PdfWriter.GetInstance(pdfdoc, new FileStream(path + "/Sample.pdf", FileMode.Create)); //Using the PDF Writer class to generate the PDF
                pdfdoc.Open(); // Opening the PDF to write the data from the textbox
                pdfdoc.Add(new Paragraph(xtxtData.Text)); // Adding the Text to the PDF
                pdfdoc.Close();
                MessageBox.Show("PDF Generation Successfull");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
    }
}


Output Screen



Adding Text Screen









Checking the PDF in the Debug Folder





Checking the data in the PDF





Reference

http://sourceforge.net/projects/itextsharp/

Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: debal_saha-9451 on: 4/28/2013 | Points: 25
Hi
I really like this article, excellent . Interested to know more from you
Posted by: Raj.Trivedi on: 7/1/2013 | Points: 25
@ Bagesh, Yes you can do this

Please check this where i have exported the logo and the gird with images to pdf from webpage.

http://www.dotnetfunda.com/articles/article2326-converting-web-page-to-pdf-using-itextsharp.aspx
Posted by: Naveed1229 on: 6/12/2017 | Points: 25
How to Convert/Export an ASPX page with texboxes and other controls(Checkbox etc) to PDF using itextsharp

Can any one help?
Posted by: Seobeast20 on: 7/6/2018 | Points: 25
thnx for sharing it....But i am using zetpdf to generate pdf which is really really good. I will recommend you to use zetpdf...

Login to post response

Comment using Facebook(Author doesn't get notification)