Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Pdf Documents using IText#

Tags:

c#

itext

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

enter image description here

like image 611
Asanka Avatar asked Apr 25 '11 07:04

Asanka


People also ask

How to add text to a PDF file using iText?

The iText library has a class named PdfWriter that creates a new pdf file to write into it. Once the file is open, you add text, image, etc. Let us understand the steps to create a pdf file and add text and image to it. Create an instance of the PdfWriter class by passing the file’s name as a parameter to the constructor.

What is pdfdocument in iText?

When an object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified. The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf.

How do I create a PDF file using Java?

Once the libraries are installed, you can use the iText library to create pdf files using the Java program. The iText library has a class named PdfWriter that creates a new pdf file to write into it. Once the file is open, you add text, image, etc.

What is iText library in Java?

iText - The community version of iText is an open-source library. It reads, creates, and manipulates the PDF files using Java. It has a hierarchical structure and can perform arbitrarily complex PDF files to generate desired results. The iText library is available in Java and .NET.


3 Answers

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();
like image 58
kripanand Avatar answered Sep 23 '22 07:09

kripanand


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

like image 21
peter_budo Avatar answered Sep 22 '22 07:09

peter_budo


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.

like image 25
AbhiRoczz... Avatar answered Sep 23 '22 07:09

AbhiRoczz...