Sunday, 18 September 2016

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:

3 comments:

Email Subscription

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

Blog Archive

BUY FROM AMAZON