Geeks With Blogs
Guilherme Cardoso, Blog EN .NET geek

In this article i'll show very quick how to write an .pdf document in .NET.
First of all, download the itextsharp library and add the reference to your project: http://itextpdf.com/

Now create a new class, like pdfHandler.cs and a method to generate the .pdf document.
With itextsharp we work with a few objects to generate the pdf. The Document will handle with the .pdf document, and will create automatically the pages.
The PdfWriter is responsable for the .pdf file. It defines where it's saved. In the example bellow i'm setting the margins (5, 5, 15, 15).

 

Document document = new Document(PageSize.A4, 5, 5, 15, 15);
PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("~/Files/doc.pdf"), FileMode.Create));
document.Open();

After creating an Document and setting up the PdfWriter instance, we can start adding some content to the .pdf.
When an Gridview is generated by the framework, it'll be rendered as an table in html. In itextsharp we work too with tables: PdfPTable and PdfPCell.

One difference important to note is the idea of "collumns, rows" when creating the tables. In html we create an table adding an row, than the collumns, then closing the row. With itextsharp it's different: When we create the table, we define how many collumns it will have. Then, we start adding cells and the itextsharp will create the rows automatically.

For example, we create an table with 2 collumns. We add one cell, then another. At this point we have an row with two collumns. If we add more two cells to the table, we'll have 2 rows with 2 collumns, and so on.

If we want to merge the cells, we use collspan property in cell.

 

PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell();


In the above example we are creating an table with 3 collums.

 

cell.Phrase = new Phrase("Id");
table.AddCell(cell);

cell.Phrase = new Phrase("Name");
table.AddCell(cell);

cell.Phrase = new Phrase("Phone");
table.AddCell(cell);


Now we are setting up the Header. he AddCell method adds the cells to the table object. Here we are adding 3 cells, so it's one row completed.

 

foreach (var regist in DataSourceOfGridview)
            {
                cell.Phrase = new Phrase(regist.Id);
                table.AddCell(cell);

                cell.Phrase = new Phrase(regist.Nome);
                table.AddCell(cell);

cell.Phrase = new Phrase(regist.Phone); table.AddCell(cell); }

Here we are adding the content of our Gridview to the table. As you can see, we always add 3 cells, so it completes one row.

 

documento.Add(table);
documento.Close();

Finally, we need to add the table object to the Document and close it.

 

private void ShowPdf(string strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader
            ("Content-Disposition", "attachment; filename=" + strS);
            Response.TransmitFile(strS);
            Response.End();

            Response.Flush();
            Response.Clear();

        }

The method above prompts the user to download the .pdf file.

 

The itexthsarp library can be very usefull. I'm using it to print orders, graphs of sales, catalogs, etc. This is just an simple tutorial of how you can add an table with dynamically data, but there's so much more you can do. Adding images, using different fonts, footers and headers, numbering the pages, etc.

I've translated this article from my Portuguese blog: http://pontonetpt.com/blogs/guilhermecardoso/archive/2010/09/07/itextsharp-criar-um-documento-pdf-com-o-resultado-de-uma-gridview.aspx

Posted on Wednesday, September 22, 2010 10:01 PM | Back to top


Comments on this post: itextsharp - Create a .pdf document with a Gridview (Table)

# re: itextsharp - Create a .pdf document with a Gridview (Table)
Requesting Gravatar...
Thank you very much, is the only place i obtain 100% functional code
Left by Joel Lopez on Dec 06, 2012 11:35 AM

# re: itextsharp - Create a .pdf document with a Gridview (Table)
Requesting Gravatar...
Hi
Very Good Tutorial.
Clear, Simple, Accurate.
The best i've found.
Pls, share more of your experience with iTextSharp.
Greetings !!
Left by Alberto on Oct 04, 2015 2:29 AM

Your comment:
 (will show your gravatar)


Copyright © Guilherme Cardoso | Powered by: GeeksWithBlogs.net