Click here to Skip to main content
Click here to Skip to main content

Creating PDF Documents in ASP.NET

, 26 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)


About the Author

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 PinmemberMember 94118954-Sep-13 18:04 
GeneralMy vote of 5 PinmemberSheikh Muhammad Haris13-Apr-13 6:09 
QuestionMy vote of 3 PinmemberAlireza_136226-Mar-13 18:33 
Questionextracting table PinmemberKalpana Volety11-Jan-13 9:07 
QuestionHow to use c# Pinmemberbayte25-May-12 4:21 
GeneralMy vote of 5 Pinmembersravani.v24-Apr-12 2:22 
QuestionRowSpan problem PinmemberVincent Roggero9-Apr-12 4:40 
Generalaaaaaaaa Pinmemberpragneshpml11-Mar-12 20:59 
GeneralMy vote of 5 PinmembersameerAbdulla10-Jan-12 0:25 
QuestionProblem function Table PinmemberMember 851181523-Dec-11 0:30 
QuestionIs this pdf abel to have the HTML controls inside ? Pinmemberamit_usa12-Dec-11 12:23 
Questionitextsharp 4.0.4 Pinmembermauirinho5-Sep-11 16:22 
Generalnot work with some langauge PinmemberBasharss19-Apr-11 5:01 
GeneralMy vote of 5 Pinmemberprajod26-Feb-11 20:34 
Generalthanks Pinmembersyed8323-Feb-11 15:56 
GeneralMy vote of 5 PinmemberBElavarasan13-Feb-11 22:51 
GeneralGreat Pinmemberjluisrojas17-Jan-11 14:14 
GeneralGOOD Pinmemberalessahilton15-Jan-11 23:09 
GeneralMy vote of 2 PinmemberLibin Jose chemperi7-Dec-10 0:55 
GeneralMy vote of 5 PinmemberEswa8-Nov-10 1:45 
GeneralMy vote of 5 PinmemberEswa29-Oct-10 0:50 
GeneralByte[] to File.pdf PinmemberMember 362262327-Oct-10 14:13 
GeneralRe: Byte[] to File.pdf PinmemberGarrylg10-Apr-14 23:05 
GeneralMy vote of 4 PinmemberCGI99923-Aug-10 18:14 
GeneralMy vote of 2 Pinmemberashish Sharma kumar11-Aug-10 19:16 
GeneralError in server Pinmemberdvbsaravan20-Jul-10 21:58 
GeneralRe: Error in server PinmemberMember 446143310-Aug-10 3:03 
GeneralMy vote of 5 PinmemberDon_Hard11-Jul-10 4:39 
GeneralCompile time error: Table, Cell and AddCell method define PinmemberBajrang Singh17-Jun-10 23:51 
GeneralRe: Compile time error: Table, Cell and AddCell method define PinmemberMember 37782905-Aug-10 13:33 
GeneralTable Pinmembermaxnis17-Jun-10 10:54 
GeneralRe: Table Pinmembergrokuk7-Jul-10 5:00 
GeneralRe: Table PinmemberChristineDaae7-Jan-11 4:28 
GeneralRe: Table [modified] PinmemberNapoleon9th1-Sep-13 18:18 
QuestionHow to set the width and height of a column? PinmemberMenandro2-Feb-10 5:03 
QuestionRun this from a server? PinmemberJohn Straumann8-Jan-10 12:41 
QuestionHow set image with in table cell? Pinmembernangaram4-Jan-10 2:03 
QuestionHow to add image inside table PinmemberFatuma Hassen10-Dec-09 23:28 
Questionhow should i add fussioncharts to this pdf? PinmemberPatilvaishalimangesh29-Oct-09 2:52 
Questionhow can i show pdf file with btn click even as it has been created Pinmembermanoj.barmola17-Sep-09 23:39 
Generalpdf envelope using itextsharp Pinmembercpandiya14-Sep-09 0:30 
Questionhow to create arabic pdf files Pinmembershaaa5-Sep-09 1:19 
Questiondoes anyone know of a document laying out all the functionality of this DLL ? PinmemberTalind11-Aug-09 10:01 
QuestionConvert webpage to PDF PinmemberSteve4568798754423-Jun-09 23:41 
AnswerRe: Convert webpage to PDF Pinmembervamshi.k.choutpally17-Mar-10 0:32 
GeneralRe: Convert webpage to PDF Pinmemberstevemonty3872917-Mar-10 14:30 
AnswerRe: Convert webpage to PDF PinmemberGarrylg10-Apr-14 23:12 
GeneralE PinmemberTavox0117-Jun-09 5:44 
Generalsetting height and width of image. also setting cell height and width. Pinmemberyogi_85859-Jun-09 0:12 
GeneralTables without borders Pinmembergchq1-Jun-09 11:28 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by fstrahberger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid