Creating PDF Docs

Creating PDF Documents in

ASP .NET


In this article I will explain how to create PDF documents in ASP .NET using itextsharp free library.

Hello World Document

First of all, I will create a simple "Hello PDF". Next, I will create a more complex PDF document with tables. To start creating PDF documents, you need to download the iTextSharp library from here and reference it in your project. Although you can use the latest library but I recommend using the version on this link. The PDF documents are created on the fly in Default.aspx and a dialog box appears asking whether to save it on any location on the computer or open it in a temporary file.
We have to use the following using directives:

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


Place a Button on the Default.aspx page and on the click event of button call ShowHelloWorld() function listed below:

private void ShowHello()
{

       Document doc = new Document();

       PdfWriter.GetInstance(doc,new FileStream(Request.PhysicalApplicationPath + "\\1.pdf", FileMode.Create));

       doc.Open();

      doc.Add(new Paragraph("Hello World"));

      doc.Close();

      Response.Redirect("~/1.pdf");

}

The ShowHello() function creates a simple document with just one string: "Hello World", and then asks the user whether to open the newly created document or open it temporarily:

A More Complex Example

Place another button on Default.aspx page and on click event of this button call ShowTable() function listed below:

public void ShowTable()
{
        Document doc = new Document();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=2.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        iTextSharp.text.Table table = new iTextSharp.text.Table(3);
        table.BorderWidth = 1;
        table.BorderColor = new Color(0, 0, 255);
        table.Padding = 3;
        table.Spacing = 1;
        Cell cell = new Cell("Real Estate Property");
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        cell.VerticalAlignment = Element.ALIGN_MIDDLE;
        cell.Header = true;
        cell.Colspan = 3;
        table.AddCell(cell);
        cell = new Cell("example cell with colspan 1 and rowspan 2");
        cell.Rowspan = 4;
        cell.Colspan = 1;
        cell.BorderColor = new Color(255, 0, 0);
        table.AddCell(cell);
        table.AddCell("Property Type");
        table.AddCell("Commercial");
        table.AddCell("Property For");
        table.AddCell("Sell");
        table.AddCell("Property For");
        table.AddCell("Sell");
        table.AddCell("Property For");
        table.AddCell("Sell");
        doc.Add(table);
       
        doc.Close();
}

The function ShowTable() is slightly more complex. It also creates a PDF document and ask the user whether to open the file or save it. It creates a table in the pdf file and writes string values in cells of the table

Putting an image in one cell of the Table in Pdf Document

If in any case you need to put an image in any of the cells of the table, use the following code for that:

iTextSharp.text.Image logo;
logo = iTextSharp.text.Image.GetInstance("Path of the Image");
cell = new Cell(logo);

To edit the column where image is placed use the following code if you need or you can change it according to your requirements:

cell.Rowspan = 12;
cell.Colspan = 1;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BorderColor = new Color(0, 0, 0);
table.AddCell(cell);


Error Handling

In some cases there maybe an error on localhost while generating pdf document. In such scenario, include the following function in the .cs file

public override void VerifyRenderingInServerForm(Control control)
{
        /* Verifies that the control is rendered */
}


Also it works fine when you run on localhost but generates an error sometimes when website is published and is live. The error it usually generates is:

System.Security.SecurityException: That assembly does not allow partially trusted callers

in that ugly yellow colored window that we've all come to hate. For this to be solved, the iTextSharp library would need to be re-compiled so that Partially-Trusted callers would be able to use the iTextSharp library. So, download the source code and re-compile the library to allow Partially-Trusted callers. But remember in this case download only the code of the dll you are using. In this for drawing tables in pdf, i used itextsharp.dll 4.0.4.0. The source code can be downloaded from here .

Modify the AssemblyInfo.cs file by adding these reference:

using System.Security;
using System.Security.Permissions;
[assembly: AllowPartiallyTrustedCallers]


Compile the code and generate a new itextsharp.dll file. Now use this dll on live website replacing the old one. You will be able to generate Pdf files on the live website as well.


I hope after reading this article you can write pdf files with content of your choice in ASP .NET C#.

References:

http://www.codeproject.com/KB/aspnet/Creating_PDF_documents_in.aspx

http://www.tomasvera.com/programming/godaddy-mysql-itextsharp-and-shared-hosting-woes/