Showing posts with label PDF. Show all posts
Showing posts with label PDF. Show all posts

Sunday, 19 November 2017

How to extract text from PDF file using iTextSharp with C#

In this tutorial, I am going to explain you how to extract text from PDF file using iTextSharp with C# in ASP.NET. Below is step by step tutorial.

Creating ASP.NET Empty Application

Create an ASP.NET Empty WebForm project as shown below.
Go to FileNewProject. A new window will be open as shown below.
Now go to WebVisual Studio 2012 → select .NET Framework 4.5 → select ASP.NET Empty Web Application and give project name and click on OK.

Creating asp.net 4.5 empty project

Now, an asp.net empty project will be created. Add a new webform to application.

Installing iTextSharp

Now the next step is to add iTextSharp reference to your application. We can add reference by two ways.
First: Download from Internet
Click on the below link to download the dll.
https://github.com/itext/itextsharp Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.
or Second: Nuget Package Manager
Go to TOOLS → Library Package Manager → Manage NuGet Packages for Solution.. and a new window will open. Type and search for iTextSharp and click on Install button as shown below. Once installed successfully, you can check iTextSharp in references folder.

Adding iTextSharp
Installing iTextSharp

You can also install by using Package Manager Console.
Go to TOOLS → Library Package Manager → Package Manager Console → write Install-Package iTextSharp and press enter. This will install iTextSharp in application.

In aspx file

In designer file create two button controls, first button is used to generate pdf file and second button is used to extract text from pdf file. One textbox control to display extracted text from pdf. Designer file look like as shown below.

Aspx designer file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <table>
        <tr>
            <td><b>Extract Text from PDF file using iTextSharp</b></td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF File" OnClick="btnGeneratePDF_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnExtract" runat="server" Text="Extract Text From PDF File" OnClick="btnExtract_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Style="width: 500px; min-height: 150px;"> 
                </asp:TextBox>
            </td>
        </tr>
    </table>
</div>
</form>
</body>
</html>

C# Code

Complete C# code is given below.

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
 
}
 
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }
 
    // create pdf file and save it to the root directory of the application 
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
 
    Document doc = new Document();
 
    PdfWriter.GetInstance(doc, fs);
 
    doc.Open();
 
    Paragraph page = new Paragraph("This is first page (page number 1)");
    doc.Add(page);
 
    Paragraph para1 = new Paragraph();
    Chunk c1 = new Chunk(@"This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph. This is first paragraph.");
    c1.SetBackground(BaseColor.YELLOW);
    para1.Add(c1);
    doc.Add(para1);
 
    Paragraph para2 = new Paragraph();
    Chunk c2 = new Chunk(@"This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph. This is second paragraph.");
    c2.SetBackground(BaseColor.GREEN);
    para2.Add(c2);
    doc.Add(para2);
 
    doc.Close();
}
 
protected void btnExtract_Click(object sender, EventArgs e)
{
    //string FilePath = @"H:\\Demo\\WebApplication1\\WebApplication1\\Example.pdf";
 
    string FilePath = Server.MapPath("Example.pdf");
 
    if (File.Exists(FilePath))
    {
        string ExtractedData = string.Empty;
 
        using (PdfReader reader = new PdfReader(FilePath))
        {
            ITextExtractionStrategy strategy = new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy();
 
            // 1. if pdf document has only one page
            //here second parameter is PDF Page number
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
 
 
            /*// 2. if pdf ducument has more than one page
            // iterating through all pages
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                ExtractedData = PdfTextExtractor.GetTextFromPage(reader, i, strategy);
            }*/
 
 
            /*// if pdf single page is having more than one paragraph
            // then split paragraph using newline
            ExtractedData = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
            string[] lines = ExtractedData.Split('\n');
            StringBuilder sb = new StringBuilder();
            foreach (string line in lines)
            {
                // 
            }*/
 
        }
        TextBox1.Text = ExtractedData;
    }
}
}
}

When you click on the Generate PDF File button, a PDF will be generated and will be saved at root directory of application. When you open pdf file, you will see 3 paragraph as shown below.

PDF file generated using iTextSharp

Now when you click on Extract Text From PDF File, all the text from page one will be extracted and displayed to the TextBox. You can iterate through all the pages using foor loop. Code is added and commented above.

Extract text from PDF using iTextSharp
Share:

Sunday, 24 September 2017

Generate Pay slip / Salary slip PDF using iTextSharp in ASP.NET

In this article, I am going to explain you how to generate employee pay slip or salary slip in PDF format using itextsharp in asp.net . I will be using visual studio 2013 professional. Before continuing this article, let's see the screen-shots.

When the page is loaded for the first time then you will see the below screen. You will select employee id and month and click on generate button to download salary slip. I have hard-coded some employee id and month in dropdownlist control in aspx file.

Generate salary slip using itextsharp in asp.net

I have shared pdf file sample as shown below in iframe.

So, below is the step by step tutorial.

Creating table and procedure

Below is the scripts to create 2 tables tbl_EmployeeDetails and tbl_SalaryDetails for employee details and salary details.

Share:

Saturday, 29 July 2017

Export DataTable to PDF file using iTextSharp and download/transmit at client machine

In this article, I am going to explain you how to export DataTable to PDF file using iTextSharp in C# and download or transmit at client machine. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.
https://github.com/itext/itextsharp

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

Related Article

  1. How to export GridView data into PDF using iTextSharp in asp.net with C#
  2. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  3. How to add meta information of PDF file using iTextSharp with C-Sharp
  4. How to extract images from a pdf file using C#.Net

In Code-Behind File

Add below nampespaces.

using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;

C# Code Snippet

Below is complete C# code to generate pdf file using dummy data table.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        ExportDataTableToPdfandDownloadAtClient();
    }
}

private void ExportDataTableToPdfandDownloadAtClient()
{
    // creating datatable and adding dumy data
    DataTable dtEmployee = new DataTable();
    dtEmployee.Columns.Add("EmpId", typeof(Int32));
    dtEmployee.Columns.Add("Name", typeof(string));
    dtEmployee.Columns.Add("Gender", typeof(string));
    dtEmployee.Columns.Add("Salary", typeof(Int32));
    dtEmployee.Columns.Add("Country", typeof(string));
    dtEmployee.Rows.Add(1, "Rahul", "Male", 60000, "India");
    dtEmployee.Rows.Add(2, "John", "Male", 50000, "USA");
    dtEmployee.Rows.Add(3, "Mary", "Female", 75000, "UK");
    dtEmployee.Rows.Add(4, "Mathew", "Male", 80000, "Australia");

    // creating document object
    iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(PageSize.A4);
    rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
    Document doc = new Document(rec);
    doc.SetPageSize(iTextSharp.text.PageSize.A4);
    PdfWriter writer = PdfWriter.GetInstance(doc, Response.OutputStream);
    doc.Open();

    //Creating paragraph for header
    BaseFont bfntHead = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font fntHead = new iTextSharp.text.Font(bfntHead, 16, 1, iTextSharp.text.BaseColor.ORANGE);
    Paragraph prgHeading = new Paragraph();
    prgHeading.Alignment = Element.ALIGN_LEFT;
    prgHeading.Add(new Chunk("Employee Details".ToUpper(), fntHead));
    doc.Add(prgHeading);

    //Adding paragraph for report generated by
    Paragraph prgGeneratedBY = new Paragraph();
    BaseFont btnAuthor = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    iTextSharp.text.Font fntAuthor = new iTextSharp.text.Font(btnAuthor, 8, 2, iTextSharp.text.BaseColor.BLUE);
    prgGeneratedBY.Alignment = Element.ALIGN_RIGHT;
    prgGeneratedBY.Add(new Chunk("Report Generated by : ASPArticles", fntAuthor));
    prgGeneratedBY.Add(new Chunk("\nGenerated Date : " + DateTime.Now.ToShortDateString(), fntAuthor));
    doc.Add(prgGeneratedBY);

    //Adding a line
    Paragraph p = new Paragraph(new Chunk(new iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, iTextSharp.text.BaseColor.BLACK, Element.ALIGN_LEFT, 1)));
    doc.Add(p);

    //Adding line break
    doc.Add(new Chunk("\n", fntHead));

    //Adding  PdfPTable
    PdfPTable table = new PdfPTable(dtEmployee.Columns.Count);

    for (int i = 0; i < dtEmployee.Columns.Count; i++)
    {
        string cellText = Server.HtmlDecode(dtEmployee.Columns[i].ColumnName);
        PdfPCell cell = new PdfPCell();
        cell.Phrase = new Phrase(cellText, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffffff"))));
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#990000"));
        //cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(grdStudent.HeaderStyle.ForeColor)));
        //cell.BackgroundColor = new BaseColor(grdStudent.HeaderStyle.BackColor);
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.PaddingBottom = 5;
        table.AddCell(cell);
    }

    //writing table Data
    for (int i = 0; i < dtEmployee.Rows.Count; i++)
    {
        for (int j = 0; j < dtEmployee.Columns.Count; j++)
        {
            table.AddCell(dtEmployee.Rows[i][j].ToString());
        }
    }

    doc.Add(table);
    doc.Close();
    writer.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" + "filename=EmployeeDetails.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(doc);
    Response.End();
}

Below is the pdf file will be downloaded at client machine.

Export datatable to pdf file using itextsharp


DOWNLOAD SOURCE CODE
Share:

Sunday, 16 October 2016

How to extract images from a pdf file using C#.Net

In this article, we are going to learn how to extract images from PDF file using itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.

https://github.com/itext/itextsharp

Related Article

  1. How to generate PDF file using iTextSharp in C#
  2. How to export GridView data into PDF using iTextSharp in asp.net with C#
  3. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  4. How to add meta information of PDF file using iTextSharp with C-Sharp

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

In Code-Behind File

Add below nampespaces.

using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

Complete C# Code

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ExtractImage();
            }
        }

        public void ExtractImage()
        {
            // existing pdf path
            PdfReader reader = new PdfReader("E:/Example.pdf");
            PRStream pst;
            PdfImageObject pio;
            PdfObject po;
            // number of objects in pdf document
            int n = reader.XrefSize;
            FileStream fs = null;
            // set image file location
            String path = "E:/";
            for (int i = 0; i < n; i++)
            {
                // get the object at the index i in the objects collection
                po = reader.GetPdfObject(i);
                // object not found so continue
                if (po == null || !po.IsStream())
                    continue;
                //cast object to stream
                pst = (PRStream)po;
                //get the object type
                PdfObject type = pst.Get(PdfName.SUBTYPE);
                //check if the object is the image type object
                if (type != null && type.ToString().Equals(PdfName.IMAGE.ToString()))
                {
                    //get the image
                    pio = new PdfImageObject(pst);
                    fs = new FileStream(path + "image" + i + ".jpg", FileMode.Create);
                    //read bytes of image in to an array
                    byte[] imgdata = pio.GetImageAsBytes();
                    //write the bytes array to file
                    fs.Write(imgdata, 0, imgdata.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
        }
    }
}
Share:

Saturday, 24 September 2016

How to add meta information of PDF file using iTextSharp with C-Sharp

In this article, we are going to learn how to add meta information of PDF file using itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the below link to download

https://github.com/itext/itextsharp

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

Related Article

  1. How to generate PDF file using iTextSharp in C#
  2. How to export GridView data into PDF using iTextSharp in asp.net with C#
  3. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  4. How to extract images from a pdf file using C#.Net

In Code-Behind File

Add below nampespaces.

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

Complete C# code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        AddMetaInfo();
    }
}
public void AddMetaInfo()
{
    // create filestream object
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    // create document object
    Document doc = new Document();

    // create PdfWriter instance which will write at file filestream
    PdfWriter.GetInstance(doc, fs);

    // opening the dociment
    doc.Open();

    // creating paragraph object
    Paragraph para = new Paragraph("Add meta information");
    para.Alignment = Element.ALIGN_CENTER;

    // Adding meta info 
    doc.AddTitle("C# iTextSharp");
    doc.AddAuthor("ASPArticles.com");
    doc.AddSubject("Adding meta information of pdf");
    doc.AddKeywords("ASP.Net Articles, iTextSharp, PDF, add Meta Info to PDF");
    doc.AddCreator("iTextSharp dll");

    // adding pargraph to document
    doc.Add(para);

    doc.Close();
}

Once PDF file is generated, Open the file-> go to file menu -> click on Properties then you will see the meta information as shown below.

Adding meta information of PDF file using iTextSharp
Share:

Tuesday, 20 September 2016

Insert an image into PDF using iTextSharp with C# (C-Sharp)

In this article, we are going to learn how to insert an image into PDF file using itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.

https://github.com/itext/itextsharp

Related Article

  1. How to generate PDF file using iTextSharp in C#
  2. How to export GridView data into PDF using iTextSharp in asp.net with C#
  3. How to add meta information of PDF file using iTextSharp with C-Sharp
  4. How to extract images from a pdf file using C#.Net

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

In Code-Behind File

Add below nampespaces.

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

Complete C# code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        InsertImage();
    }
}

public void InsertImage()
{
    // create filestream object
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    // create document object
    Document doc = new Document();

    // create PdfWriter instance which will write at file filestream
    PdfWriter.GetInstance(doc, fs);

    // opening the dociment
    doc.Open();

    // creating paragraph object
    Paragraph para = new Paragraph("Insert an image into pdf using C#.");
    para.Alignment = Element.ALIGN_CENTER;

    // adding pargraph to document
    doc.Add(para);

    // setting image path
    string imagePath = Server.MapPath("Images") + "\\demo.PNG";
    // string imagePath = Server.MapPath("Images\\demo.PNG") + "";

    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);

    image.Alignment = Element.ALIGN_CENTER;

    // set width and height
    image.ScaleToFit(180f, 250f);

    // adding image to document
    doc.Add(image);
    // closing the document
    doc.Close();
}

Below is the pdf file generated.

Insert image into pdf using c-sharp
Share:

Sunday, 18 September 2016

How to export GridView data into PDF using iTextSharp in asp.net with C#

In this article, we are going to learn how to export GridView data into PDF file using itextsharp in asp.net with C#. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.

https://github.com/itext/itextsharp

Related Article

  1. How to generate PDF file using iTextSharp in C#
  2. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  3. How to add meta information of PDF file using iTextSharp with C-Sharp
  4. How to extract images from a pdf file using C#.Net

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

In Designer File

In designer file create a GridView control to display data and two Button control for exporting GridView data in PDF file. First button will export complete GridView data and second button export current page data. Below is the designer file code.

Gridview designer for exporting data into pdf
<asp:GridView ID="grdStudent" runat="server" CellPadding="4" BackColor="#CCCCCC"
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="2px" CellSpacing="2" ForeColor="Black"
    AutoGenerateColumns="false" AllowPaging="true" PageSize="3" OnPageIndexChanging="grdStudent_PageIndexChanging">
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" ForeColor="Black" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    <Columns>
        <asp:BoundField DataField="StudentName" HeaderText="Student Name" />
        <asp:BoundField DataField="Physics" HeaderText="Physics" />
        <asp:BoundField DataField="Chemistry" HeaderText="Chemistry" />
        <asp:BoundField DataField="Maths" HeaderText="Maths" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportAll" runat="server" Text="Export To PDF" BackColor="#990000"
    BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" ForeColor="White" Height="30px"
    BorderColor="#333333" OnClick="btnExportAll_Click" />
<asp:Button ID="btnExportCurrent" runat="server" Text="Export Current Page" BackColor="#990000"
    BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" ForeColor="White" Height="30px"
    BorderColor="#333333" OnClick="btnExportCurrent_Click" />

In Code-Behind File

Add below nampespaces.

using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;

DataSource to Bind GridView

Below is the BindGrid method to bind GridView on Page_load event.

public DataTable BindGrid()
{
    //Creating a DataTable
    DataTable dt = new DataTable();
    //adding Columns to DataTable
    dt.Columns.Add("StudentName", typeof(string));
    dt.Columns.Add("Physics", typeof(Int32));
    dt.Columns.Add("Chemistry", typeof(Int32));
    dt.Columns.Add("Maths", typeof(Int32));
    //Adding rows to DataTable
    dt.Rows.Add("Vijay", 90, 80, 85);
    dt.Rows.Add("Rahul", 75, 85, 88);
    dt.Rows.Add("John", 75, 85, 95);
    dt.Rows.Add("Mary", 95, 85, 90);
    return dt;
}

Complete C# Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        grdStudent.DataSource = BindGrid();
        grdStudent.DataBind();
    }
}

public DataTable BindGrid()
{
    //Creating a DataTable
    DataTable dt = new DataTable();
    //adding Columns to DataTable
    dt.Columns.Add("StudentName", typeof(string));
    dt.Columns.Add("Physics", typeof(Int32));
    dt.Columns.Add("Chemistry", typeof(Int32));
    dt.Columns.Add("Maths", typeof(Int32));
    //Adding rows to DataTable
    dt.Rows.Add("Vijay", 90, 80, 85);
    dt.Rows.Add("Rahul", 75, 85, 88);
    dt.Rows.Add("John", 75, 85, 95);
    dt.Rows.Add("Mary", 95, 85, 90);
    return dt;
}

protected void btnExportAll_Click(object sender, EventArgs e)
{
    grdStudent.AllowPaging = false;
    grdStudent.DataSource = BindGrid();
    grdStudent.DataBind();
    GeneratePDF();
}

public void GeneratePDF()
{
    PdfPTable table = new PdfPTable(grdStudent.Columns.Count);

    for (int i = 0; i < grdStudent.Columns.Count; i++)
    {
        string cellText = Server.HtmlDecode(grdStudent.Columns[i].HeaderText);
        PdfPCell cell = new PdfPCell();
        cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffffff"))));
        cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#990000"));
        //cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(grdStudent.HeaderStyle.ForeColor)));
        //cell.BackgroundColor = new BaseColor(grdStudent.HeaderStyle.BackColor);
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.PaddingBottom = 5;
        table.AddCell(cell);
    }
    for (int i = 0; i < grdStudent.Rows.Count; i++)
    {
        if (grdStudent.Rows[i].RowType == DataControlRowType.DataRow)
        {
            for (int j = 0; j < grdStudent.Columns.Count; j++)
            {
                string cellText = Server.HtmlDecode(grdStudent.Rows[i].Cells[j].Text);
                PdfPCell cell = new PdfPCell();
                //cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(grdStudent.RowStyle.ForeColor)));
                //cell.BackgroundColor = new BaseColor(grdStudent.RowStyle.BackColor);
                cell.Phrase = new Phrase(cellText, new Font(Font.FontFamily.TIMES_ROMAN, 10, 1, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#000000"))));
                cell.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffffff"));
                cell.BackgroundColor = new BaseColor(grdStudent.RowStyle.BackColor);
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.PaddingBottom = 5;
                table.AddCell(cell);
            }
        }
    }
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    pdfDoc.Add(table);
    pdfDoc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;" + "filename=Student_Grid_pdf.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
}

protected void btnExportCurrent_Click(object sender, EventArgs e)
{
    GeneratePDF();
}

protected void grdStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdStudent.PageIndex = e.NewPageIndex;
    grdStudent.DataSource = BindGrid();
    grdStudent.DataBind();
}

Below is the pdf file generated.

Exported gridview into pdf
Share:

How to generate PDF file using iTextSharp in C#

In this article, I am going to explain you how to generate pdf file using iTextSharp in C# and asp.net. First, you need to download iTextSharp dll from the internet. Click on the below link to download the dll.

https://github.com/itext/itextsharp

Once file is downloaded, extract it, now you will find 6 more .rar file. Again extract itextsharp-dll-core.rar file, after that add reference of itextsharp.dll to your project.

Related Article

  1. How to export GridView data into PDF using iTextSharp in asp.net with C#
  2. Insert an image into PDF using iTextSharp with C# (C-Sharp)
  3. How to add meta information of PDF file using iTextSharp with C-Sharp
  4. How to extract images from a pdf file using C#.Net

In Code-Behind File

Add below nampespaces.

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

Creating a PDF file with a single paragraph

Below is the steps to create pdf file.

  1. Create a FileStream object
  2. FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);
    
  3. Create a Document object
  4. Document doc = new Document();
    
  5. Create a PdfWriter object to write the Document to the specified FileStream
  6. PdfWriter.GetInstance(doc, fs);
    
  7. Opening the document
  8. doc.Open();
    
  9. Create Paragraph object
  10. Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph.");
    
  11. Adding paragraph object to document.
  12. doc.Add(para);
  13. Closing the document
  14. doc.Close();

Complete code snippet is given below.

public void CreateDocument()
{
    // delete the file if already exists
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }

    // create filestream object
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    // create document object
    Document doc = new Document();

    // create PdfWriter instance which will write at file filestream
    PdfWriter.GetInstance(doc, fs);

    // opening the dociment
    doc.Open();

    // creating paragraph object
    Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph.");

    // adding pargraph to document
    doc.Add(para);

    // closing the document
    doc.Close();
}

Setting page size and background color of pdf document

public void SetPageSize()
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }

    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    // setting page page size and background color
    Rectangle rec = new Rectangle(PageSize.A4);
    rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);

    //OR
    //Rectangle rec = new Rectangle(720, 720); // for page size
    //rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);

    Document doc = new Document(rec);

    PdfWriter.GetInstance(doc, fs);

    doc.Open();

    Paragraph para = new Paragraph("Welcome to asparticles.com. Pdf with pagesize A4. Different kind of pagesize is supported");

    doc.Add(para);

    doc.Close();
}

Setting paragraph alignment

Paragraph para = new Paragraph("Welcome to asparticles.com. This is first paragraph. This is paragraph alignment example. Thare are varius alignment supported.");
para.Alignment = Element.ALIGN_RIGHT;

Adding a PdfPTable and a PdfPCell to PdfPTable

public void PdfTable1()
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    Rectangle rec = new Rectangle(PageSize.A4);
    rec.BackgroundColor = new BaseColor(System.Drawing.Color.Olive);
    Document doc = new Document(rec);

    PdfWriter.GetInstance(doc, fs);

    PdfPTable table = new PdfPTable(4); // creating a table with 4 columns
    // float[] width = { 20, 20, 30, 30 };  //Header Widths
    //table.SetWidths(width);        //Setting the pdf headers
    table.WidthPercentage = 80;       //Setting the PDF File width percentage

    //creating a cell and show as table title
    PdfPCell cell = new PdfPCell();
    cell.Colspan = 4;
    cell.Border = 1;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Phrase = new Phrase("Adding cell to table & display as table title", new Font(Font.FontFamily.COURIER, 18, 1, new BaseColor(System.Drawing.Color.Blue)));
    cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
    cell.PaddingBottom = 10;
    
    table.AddCell(cell); // adding cell to table

    doc.Open();
    doc.Add(table); // adding table to document
    doc.Close();
}

Adding a table and multiple cells to table

public void PdfTable2()
{
    if (File.Exists(Server.MapPath("Example.pdf")))
    {
        File.Delete(Server.MapPath("Example.pdf"));
    }
    FileStream fs = new FileStream(Server.MapPath("Example.pdf"), FileMode.Create);

    Rectangle rec = new Rectangle(PageSize.A4);
    rec.BackgroundColor = new BaseColor(System.Drawing.Color.WhiteSmoke);
    Document doc = new Document(rec);

    PdfWriter.GetInstance(doc, fs);

    PdfPTable table = new PdfPTable(4); // creating a table with 4 columns
    // float[] width = { 20, 20, 30, 30 }; //setting Header Widths
    //table.SetWidths(width);        //Setting the pdf headers
    table.WidthPercentage = 80;       //Setting the PDF File width percentage

    //creating 1st cell and show as table title
    PdfPCell cell = new PdfPCell();
    cell.Colspan = 4;
    cell.Border = 1;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Phrase = new Phrase("Adding cell to table & display as table title", new Font(Font.FontFamily.COURIER, 18, 1, new BaseColor(System.Drawing.Color.Blue)));
    cell.BackgroundColor = new BaseColor(System.Drawing.Color.Yellow);
    cell.PaddingBottom = 10;
   
    // adding 1st cell to table
    table.AddCell(cell);

    // creating and adding 2nd cell using constructor and display as a students marks header
    table.AddCell(new PdfPCell(new Phrase("Student Marks", new Font(Font.FontFamily.COURIER, 15, 2, BaseColor.WHITE))) { HorizontalAlignment = Element.ALIGN_CENTER, PaddingBottom = 10, BackgroundColor = BaseColor.RED, Colspan = 4 });

    // creating and adding 3rd,4th,5th,6th cell using constructor and display as a students name and subkects
    table.AddCell(new PdfPCell(new Phrase("Student Name", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
    table.AddCell(new PdfPCell(new Phrase("Physics", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
    table.AddCell(new PdfPCell(new Phrase("Chemistry", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });
    table.AddCell(new PdfPCell(new Phrase("Maths", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.BLACK))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = new BaseColor(System.Drawing.Color.Silver) });

    // creating and adding remaining cells using constructor and display as a students marks
    table.AddCell(new PdfPCell(new Phrase("John", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("80", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });

    table.AddCell(new PdfPCell(new Phrase("Mary", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("80", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });

    table.AddCell(new PdfPCell(new Phrase("Rahul", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("90", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("75", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    table.AddCell(new PdfPCell(new Phrase("85", new Font(Font.FontFamily.COURIER, 8, 1, BaseColor.RED))) { HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor = BaseColor.WHITE });
    
    doc.Open();

    doc.Add(table); // adding table to document

    doc.Close();
}

Below is the pdf generated by last program.

Generating pdf file using itextshap dll in csharp
Share:

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

Ads By Chitika