Click here to Skip to main content
15,881,248 members
Articles / Web Development / XHTML

PDF file creation using the ItextSharp library with ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.00/5 (6 votes)
31 Mar 2009CPOL2 min read 39.9K   22   11
PDF file creation using the ItextSharp library with ASP.NET and C#, and deleting the file from the server after displaying it.

Introduction

Today I am going to explain how to create a PDF file on a server using ASP.NET and how to delete it from the server.

I was trying to create a PDF file with help of ItextSharp, which was relatively easy task. After creation, I wanted to load this file on the web browser for the user. Once loaded, I wanted to delete the file from the server. I searched on the web regarding this topic, but found almost nothing of help. After a lot of thinking, I came up with this solution. If you have a better solution, please share it with me.

Background

The primary requirements for this application are a PDF file template, a data source (data that will go inside the PDF file), and the ItextSharp library. Download ItextSharp from SourceForge.net. I would like to thank the authors of ItextSharp for creating such a nice library which makes PDF creation a piece of cake.

Please read this article for understanding how to create a PDF file.

Using the code

I will explain how to load the PDF file into a web browser from a server directory once the PDF file is created, we will also see how to delete the file once it is displayed.

Step 1: Create a directory on the server, for example "/pdfFactory/". Use this folder as the output path from the PDF file creation method.

Step 2: Once the file is created, construct a thread:

C#
protected void Page_Load(object sender, EventArgs e){
    .
    .
    .

    string filePath = Server.MapPath("/pdfFactory/")+"/pdfFile.pdf"; 
    if (File.Exist(filePath)){
        Thread t = new Thread( deleteMe); 

        t.Start(filePath); 

        Response.Redirect("/pdfFactory/pdfFile.pdf"); 
    }
    .
    .
    .
}

static void deleteMe(object fileObject){
    string filePath = (string) fileObject; 
    Thread.Sleep(5000); 
    if(File.Exist(filePath)){
    File.Delete(filePath); 
    }
}

Explanation

Once the PDF file is created inside the "pdfFactory" folder, it can be loaded directly to the browser with a relative URL. The Response.Redirect method uses this path to load the newly created PDF file into the web browser. Once the file is redirected with the above method, server side control is lost. So, we need another event to delete the file from the client browser. But, it requires another server call from the client. To overcome this problem, we are using a Thread.

The created thread "t" calls the deleteMe method. This method takes a filePath parameter. Once started, it sleeps until the file is loaded to the client machine. After 5000 milliseconds, the server deletes this file with the File object.

If the client wishes to save this file, the client can save it on their local machine. This way, the server is not loaded with a lot of files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CSU Office of Chancellor
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
OriginalGriff3-May-12 20:41
mveOriginalGriff3-May-12 20:41 
QuestionPDF header signature not found Pin
barkatkhan23-Feb-12 1:51
barkatkhan23-Feb-12 1:51 
GeneralMy vote of 1 Pin
Jay Riggs14-Aug-09 10:16
Jay Riggs14-Aug-09 10:16 
GeneralMy vote of 1 Pin
Member 423314514-Jun-09 1:53
Member 423314514-Jun-09 1:53 
GeneralMy vote of 1 Pin
Akash Kava12-May-09 23:27
Akash Kava12-May-09 23:27 
GeneralMy vote of 1 Pin
Richard Deeming7-Apr-09 8:48
mveRichard Deeming7-Apr-09 8:48 
GeneralMy vote of 1 Pin
Rui Holdorf6-Apr-09 13:25
Rui Holdorf6-Apr-09 13:25 
GeneralRe: My vote of 1 Pin
Abhay Mhatre7-Apr-09 8:06
Abhay Mhatre7-Apr-09 8:06 
QuestionPDF Create? Pin
ZeroInfinity1-Apr-09 23:30
ZeroInfinity1-Apr-09 23:30 
AnswerRe: PDF Create? Pin
Abhay Mhatre7-Apr-09 8:05
Abhay Mhatre7-Apr-09 8:05 
GeneralRe: PDF Create? Pin
Hoosier17-Jul-09 3:01
Hoosier17-Jul-09 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.