Create Pdf Documents using IText#

46,814

Solution 1

Add Namespace:

using iTextSharp.text;
using iTextSharp.text.pdf;

code using c#:

    Document doc = new Document(PageSize.A4);
    var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
    var writer = PdfWriter.GetInstance(doc, output);


    doc.Open();


    var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/ABsIS_Logo.jpg"));
    logo.SetAbsolutePosition(430, 770);
    logo.ScaleAbsoluteHeight(30);
    logo.ScaleAbsoluteWidth(70);
    doc.Add(logo);

    PdfPTable table1 = new PdfPTable(2);
    table1.DefaultCell.Border = 0;
    table1.WidthPercentage = 80;

    var titleFont = new Font(Font.FontFamily.UNDEFINED, 24);
    var subTitleFont = new Font(Font.FontFamily.UNDEFINED, 16);

    PdfPCell cell11 = new PdfPCell();
    cell11.Colspan = 1;
    cell11.AddElement(new Paragraph("ABC Traders Receipt", titleFont));

    cell11.AddElement(new Paragraph("Thankyou for shoping at ABC traders,your order details are below", subTitleFont));


    cell11.VerticalAlignment = Element.ALIGN_LEFT;

    PdfPCell cell12 = new PdfPCell();


    cell12.VerticalAlignment = Element.ALIGN_CENTER;


    table1.AddCell(cell11);

    table1.AddCell(cell12);


    PdfPTable table2 = new PdfPTable(3);

    //One row added

    PdfPCell cell21 = new PdfPCell();

    cell21.AddElement(new Paragraph("Photo Type"));

    PdfPCell cell22 = new PdfPCell();

    cell22.AddElement(new Paragraph("No. of Copies"));

    PdfPCell cell23 = new PdfPCell();

    cell23.AddElement(new Paragraph("Amount"));


    table2.AddCell(cell21);

    table2.AddCell(cell22);

    table2.AddCell(cell23);


    //New Row Added

    PdfPCell cell31 = new PdfPCell();

    cell31.AddElement(new Paragraph("Safe"));

    cell31.FixedHeight = 300.0f;

    PdfPCell cell32 = new PdfPCell();

    cell32.AddElement(new Paragraph("2"));

    cell32.FixedHeight = 300.0f;

    PdfPCell cell33 = new PdfPCell();

    cell33.AddElement(new Paragraph("20.00 * " + "2" + " = " + (20 * Convert.ToInt32("2")) + ".00"));

    cell33.FixedHeight = 300.0f;



    table2.AddCell(cell31);

    table2.AddCell(cell32);

    table2.AddCell(cell33);


    PdfPCell cell2A = new PdfPCell(table2);

    cell2A.Colspan = 2;

    table1.AddCell(cell2A);

    PdfPCell cell41 = new PdfPCell();

    cell41.AddElement(new Paragraph("Name : " + "ABC"));

    cell41.AddElement(new Paragraph("Advance : " + "advance"));

    cell41.VerticalAlignment = Element.ALIGN_LEFT;

    PdfPCell cell42 = new PdfPCell();

    cell42.AddElement(new Paragraph("Customer ID : " + "011"));

    cell42.AddElement(new Paragraph("Balance : " + "3993"));

    cell42.VerticalAlignment = Element.ALIGN_RIGHT;


    table1.AddCell(cell41);

    table1.AddCell(cell42);


    doc.Add(table1);

    doc.Close();

Solution 2

You can have look also at http://www.mikesdotnetting.com/Category/20, some handy samples of stuff people are often after it

PS: AbhiRoczz... personally I do avoid roseindia as they tend to steal plenty of resources, meaning they will copy&paste without giving credit to original owner. Plus site is badly organized and have one to many adverts

Solution 3

Check out following examples of using Itext.

Itext Examples for tables lists and images

You can further search for html to pdf converter. There are lot of free tools available. You need to pass your html containing the table and it will return the pdf document. I have developed one such application. Let me know if u need it.

Share:
46,814
Asanka
Author by

Asanka

Updated on July 05, 2022

Comments

  • Asanka
    Asanka almost 2 years

    How can I create a pdf document with tables, which looks like this.

    enter image description here

  • Vishal Anand
    Vishal Anand over 8 years
    roseindia is actually a very bad site, with lot of bad practices and flaws
  • peter_budo
    peter_budo over 8 years
    @VishalAnand yeap, and nothing changed since I posted above. It went even worse
  • mkl
    mkl over 3 years
    In your other answer from 2018 you at least added a header to a wall of code, here there's only such a wall...