Creating PDF Files in ASP.NET using iTextSharp

iText is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web and other applications with dynamic PDF document generation and/or manipulation.

Developers can use iText to:

(i)           Serve PDF to a browser
(ii)          Generate dynamic documents from XML files or databases
(iii)         Use PDF's many interactive features
(iv)         Add bookmarks, page numbers, watermarks, etc
(v)          Split, concatenate, and manipulate PDF pages
(vi)         Automate filling out of PDF forms
(vii)        Add digital signatures to a PDF file

iText is available in Java as well as in C#.

Find some links below that will help you to explore this:


So, now if you want to use this in ASP.NET application, I have created sample solution for you, just follow the steps to understand it.

Step 1

First of all you need to download the components (dll) from here.

Step 2

Now, open the Visual Studio and create and simple website and add a ‘Bin’ folder in the project.

Step 3

If you done with above, right click on the ‘Bin’ folder and select ‘Add Reference’ and in the appeared dialog box click on Browse button and locate the dll we have saved in step 1.


Step 4

Now in the code-behind add two namespaces using iTextSharp.text; and using iTextSharp.text.pdf; and add a folder named ‘PDF_Files’ on the root. Remember, if you are using hosting then you need to allow Read and Write permission on this folder.

Step 5

Okay, use the following code-behind codes:

        var doc1 = new iTextSharp.text.Document();
        string path = Server.MapPath("PDF_Files");
        PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
        doc1.Open();
        doc1.Add(new Paragraph("My sample text goes here."));
        doc1.Close();

Step 6

If you done with above, run the project and test it, find the complete code altogether.

Code-Behind

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var doc1 = new iTextSharp.text.Document();
        string path = Server.MapPath("PDF_Files");
        PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
        doc1.Open();
        doc1.Add(new Paragraph("My sample text goes here."));
        doc1.Close();
    }
}



If you want to explore it more, please visit here http://itextpdf.com/summit.php.
I hope you like it. Thanks.

Comments

  1. all i do,u said in ur procedure but when i debug the project the its show me the blank aspx page..:( i used visual studio 2010...

    ReplyDelete
  2. its not working ...its just show the blank page...

    ReplyDelete
    Replies
    1. You might be missing something. Try visiting http://sourceforge.net/projects/itextsharp/

      Delete
  3. Nice article worked perfectly with asp.net mvc 4 with vs 2012, here I needed to add dll reference to References folder.


    Thanks a lot.

    ReplyDelete
  4. Hi ...how to merge two PDF file into single PDF file....kindly Help...plz

    ReplyDelete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System