Click here to Skip to main content
15,498,859 members
Articles / Web Development / ASP.NET
Posted 26 Sep 2007


255 bookmarked

Creating PDF Documents in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.74/5 (63 votes)
26 Sep 2007CPOL
How to create PDF documents in ASP.NET.

Screenshot - 081_hello_pdf.jpg


This short article explains how to create PDF documents from ASP.NET web pages using this free library:

The code

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 and reference it in your project. The PDF documents are created "on the fly" by the web page "ShowPDF.aspx". This page also does a "Response.Redirect" to the created PDF. We will first import the required namespaces:

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Next, in the Page_Load event, we find out which document was requested by the user:

Partial Class ShowPDF
   Inherits System.Web.UI.Page
   Protected Sub Page_Load(ByVal sender As Object, _
             ByVal e As System.EventArgs) Handles Me.Load
      If Request.QueryString("id") = 1 Then
       End If
   End Sub
End Class

The ShowHello function creates a simple document with just one string: "Hello World", and then redirects the user to the newly created document:

Sub ShowHello()
   Dim doc As Document = New Document
   PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + _
                         "\1.pdf", FileMode.Create))
   doc.Add(New Paragraph("Hello World"))
End Sub

A more complex example

The function ShowTable is slightly more complex. It also creates a PDF document and redirects the user to it:

Sub ShowTable()
   Dim doc As Document = New Document
   PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + _
                         "\2.pdf", FileMode.Create))
   Dim table As Table = New Table(3)
   table.BorderWidth = 1
   table.BorderColor = New Color(0, 0, 255)
   table.Padding = 3
   table.Spacing = 1
   Dim cell As Cell = New Cell("header")
   cell.Header = True
   cell.Colspan = 3
   cell = New Cell("example cell with colspan 1 and rowspan 2")
   cell.Rowspan = 2
   cell.BorderColor = New Color(255, 0, 0)
   table.AddCell("cell test1")
   cell = New Cell("big cell")
   cell.Rowspan = 2
   cell.Colspan = 2
   cell.HorizontalAlignment = Element.ALIGN_CENTER
   cell.VerticalAlignment = Element.ALIGN_MIDDLE
   cell.BackgroundColor = New Color(192, 192, 192)
   table.AddCell("cell test2")
End Sub

Using the iTextSharp library ( makes it very easy to create PDF documents from web applications.


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

Written By
Web Developer
Germany Germany
Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit:

For video tutorials about, ajax, gridviews, ... (in german) visit:

Comments and Discussions

QuestionAbout pdf viewer Pin
pravin_kale4-Sep-13 18:04
Memberpravin_kale4-Sep-13 18:04 
GeneralMy vote of 5 Pin
Sheikh Muhammad Haris13-Apr-13 6:09
MemberSheikh Muhammad Haris13-Apr-13 6:09 
QuestionMy vote of 3 Pin
Alireza_136226-Mar-13 18:33
MemberAlireza_136226-Mar-13 18:33 
QuestionHow to use c# Pin
bayte25-May-12 4:21
Memberbayte25-May-12 4:21 
GeneralMy vote of 5 Pin
sravani.v24-Apr-12 2:22
Membersravani.v24-Apr-12 2:22 
QuestionRowSpan problem Pin
Vincent Roggero9-Apr-12 4:40
MemberVincent Roggero9-Apr-12 4:40 
Generalaaaaaaaa Pin
pragneshpml11-Mar-12 20:59
Memberpragneshpml11-Mar-12 20:59 
GeneralMy vote of 5 Pin
tulasiram.v10-Jan-12 0:25
Membertulasiram.v10-Jan-12 0:25 
QuestionProblem function Table Pin
Member 851181523-Dec-11 0:30
MemberMember 851181523-Dec-11 0:30 
QuestionIs this pdf abel to have the HTML controls inside ? Pin
amit_usa12-Dec-11 12:23
Memberamit_usa12-Dec-11 12:23 
Questionitextsharp 4.0.4 Pin
mauirinho5-Sep-11 16:22
Membermauirinho5-Sep-11 16:22 
Generalnot work with some langauge Pin
Basharss19-Apr-11 5:01
MemberBasharss19-Apr-11 5:01 
GeneralMy vote of 5 Pin
prajod26-Feb-11 20:34
Memberprajod26-Feb-11 20:34 
Generalthanks Pin
syed8323-Feb-11 15:56
Membersyed8323-Feb-11 15:56 
GeneralMy vote of 5 Pin
BElavarasan13-Feb-11 22:51
MemberBElavarasan13-Feb-11 22:51 
GeneralGreat Pin
jluisrojas17-Jan-11 14:14
Memberjluisrojas17-Jan-11 14:14 
GeneralGOOD Pin
alessahilton15-Jan-11 23:09
Memberalessahilton15-Jan-11 23:09 
GeneralMy vote of 2 Pin
Libin Jose chemperi7-Dec-10 0:55
MemberLibin Jose chemperi7-Dec-10 0:55 
GeneralMy vote of 5 Pin
TweakBird8-Nov-10 1:45
MemberTweakBird8-Nov-10 1:45 
GeneralMy vote of 5 Pin
TweakBird29-Oct-10 0:50
MemberTweakBird29-Oct-10 0:50 
GeneralByte[] to File.pdf Pin
Wikus.Olivier27-Oct-10 14:13
MemberWikus.Olivier27-Oct-10 14:13 
GeneralRe: Byte[] to File.pdf Pin
Garrylg10-Apr-14 23:05
MemberGarrylg10-Apr-14 23:05 
GeneralMy vote of 4 Pin
CGI99923-Aug-10 18:14
MemberCGI99923-Aug-10 18:14 
GeneralMy vote of 2 Pin
ashish Sharma kumar11-Aug-10 19:16
Memberashish Sharma kumar11-Aug-10 19:16 
GeneralError in server Pin
dvbsaravan20-Jul-10 21:58
Memberdvbsaravan20-Jul-10 21:58 

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.