Click here to Skip to main content
11,720,364 members (71,253 online)
Click here to Skip to main content

Creating PDF Documents in ASP.NET

, 26 Sep 2007 CPOL 519.6K 19.9K 250
Rate this:
Please Sign up or sign in to vote.
How to create PDF documents in ASP.NET.

Screenshot - 081_hello_pdf.jpg

Introduction

This short article explains how to create PDF documents from ASP.NET web pages using this free library: http://sourceforge.net/projects/itextsharp/.

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 http://sourceforge.net/projects/itextsharp/ 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
         ShowHello()
       Else
         ShowTable()
       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.Open()
   doc.Add(New Paragraph("Hello World"))
   doc.Close()
   Response.Redirect("~/1.pdf")
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))
   doc.Open()
   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
   table.AddCell(cell)
   cell = New Cell("example cell with colspan 1 and rowspan 2")
   cell.Rowspan = 2
   cell.BorderColor = New Color(255, 0, 0)
   table.AddCell(cell)
   table.AddCell("1.1")
   table.AddCell("2.1")
   table.AddCell("1.2")
   table.AddCell("2.2")
   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)
   table.AddCell("cell test2")
   doc.Add(table)
   doc.Close()
   Response.Redirect("~/2.pdf")
End Sub

Using the iTextSharp library (http://sourceforge.net/projects/itextsharp/) makes it very easy to create PDF documents from web applications.

License

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

Share

About the Author

fstrahberger
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: www.venco.de

For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com

You may also be interested in...

Comments and Discussions

 
QuestionAbout pdf viewer Pin
Member 94118954-Sep-13 17:04
memberMember 94118954-Sep-13 17:04 
GeneralMy vote of 5 Pin
Sheikh Muhammad Haris13-Apr-13 5:09
memberSheikh Muhammad Haris13-Apr-13 5:09 
QuestionMy vote of 3 Pin
Alireza_136226-Mar-13 17:33
memberAlireza_136226-Mar-13 17:33 
QuestionHow to use c# Pin
bayte25-May-12 3:21
memberbayte25-May-12 3:21 
GeneralMy vote of 5 Pin
sravani.v24-Apr-12 1:22
membersravani.v24-Apr-12 1:22 
QuestionRowSpan problem Pin
Vincent Roggero9-Apr-12 3:40
memberVincent Roggero9-Apr-12 3:40 
Generalaaaaaaaa Pin
pragneshpml11-Mar-12 19:59
memberpragneshpml11-Mar-12 19:59 
GeneralMy vote of 5 Pin
sameerAbdulla9-Jan-12 23:25
membersameerAbdulla9-Jan-12 23:25 
QuestionProblem function Table Pin
Member 851181522-Dec-11 23:30
memberMember 851181522-Dec-11 23:30 
QuestionIs this pdf abel to have the HTML controls inside ? Pin
amit_usa12-Dec-11 11:23
memberamit_usa12-Dec-11 11:23 
Questionitextsharp 4.0.4 Pin
mauirinho5-Sep-11 15:22
membermauirinho5-Sep-11 15:22 
Generalnot work with some langauge Pin
Basharss19-Apr-11 4:01
memberBasharss19-Apr-11 4:01 
GeneralMy vote of 5 Pin
prajod26-Feb-11 19:34
memberprajod26-Feb-11 19:34 
Generalthanks Pin
syed8323-Feb-11 14:56
membersyed8323-Feb-11 14:56 
GeneralMy vote of 5 Pin
BElavarasan13-Feb-11 21:51
memberBElavarasan13-Feb-11 21:51 
GeneralGreat Pin
jluisrojas17-Jan-11 13:14
memberjluisrojas17-Jan-11 13:14 
GeneralGOOD Pin
alessahilton15-Jan-11 22:09
memberalessahilton15-Jan-11 22:09 
GeneralMy vote of 2 Pin
Libin Jose chemperi6-Dec-10 23:55
memberLibin Jose chemperi6-Dec-10 23:55 
GeneralMy vote of 5 Pin
Eswa8-Nov-10 0:45
memberEswa8-Nov-10 0:45 
GeneralMy vote of 5 Pin
Eswa28-Oct-10 23:50
memberEswa28-Oct-10 23:50 
GeneralByte[] to File.pdf Pin
Member 362262327-Oct-10 13:13
memberMember 362262327-Oct-10 13:13 
GeneralRe: Byte[] to File.pdf Pin
Garrylg10-Apr-14 22:05
memberGarrylg10-Apr-14 22:05 
GeneralMy vote of 4 Pin
CGI99923-Aug-10 17:14
memberCGI99923-Aug-10 17:14 
GeneralMy vote of 2 Pin
ashish Sharma kumar11-Aug-10 18:16
memberashish Sharma kumar11-Aug-10 18:16 
GeneralError in server Pin
dvbsaravan20-Jul-10 20:58
memberdvbsaravan20-Jul-10 20:58 
GeneralRe: Error in server Pin
Member 446143310-Aug-10 2:03
memberMember 446143310-Aug-10 2:03 
GeneralMy vote of 5 Pin
Don_Hard11-Jul-10 3:39
memberDon_Hard11-Jul-10 3:39 
GeneralCompile time error: Table, Cell and AddCell method define Pin
Bajrang Singh17-Jun-10 22:51
memberBajrang Singh17-Jun-10 22:51 
GeneralRe: Compile time error: Table, Cell and AddCell method define Pin
Member 37782905-Aug-10 12:33
memberMember 37782905-Aug-10 12:33 
GeneralTable Pin
maxnis17-Jun-10 9:54
membermaxnis17-Jun-10 9:54 
GeneralRe: Table Pin
grokuk7-Jul-10 4:00
membergrokuk7-Jul-10 4:00 
GeneralRe: Table Pin
ChristineDaae7-Jan-11 3:28
memberChristineDaae7-Jan-11 3:28 
GeneralRe: Table Pin
Napoleon9th1-Sep-13 17:18
memberNapoleon9th1-Sep-13 17:18 
QuestionHow to set the width and height of a column? Pin
Menandro2-Feb-10 4:03
memberMenandro2-Feb-10 4:03 
QuestionRun this from a server? Pin
John Straumann8-Jan-10 11:41
memberJohn Straumann8-Jan-10 11:41 
QuestionHow set image with in table cell? Pin
nangaram4-Jan-10 1:03
membernangaram4-Jan-10 1:03 
QuestionHow to add image inside table Pin
Fatuma Hassen10-Dec-09 22:28
memberFatuma Hassen10-Dec-09 22:28 
Questionhow should i add fussioncharts to this pdf? Pin
Patilvaishalimangesh29-Oct-09 1:52
memberPatilvaishalimangesh29-Oct-09 1:52 
Questionhow can i show pdf file with btn click even as it has been created Pin
manoj.barmola17-Sep-09 22:39
membermanoj.barmola17-Sep-09 22:39 
Generalpdf envelope using itextsharp Pin
cpandiya13-Sep-09 23:30
membercpandiya13-Sep-09 23:30 
Questionhow to create arabic pdf files Pin
shaaa5-Sep-09 0:19
membershaaa5-Sep-09 0:19 
Questiondoes anyone know of a document laying out all the functionality of this DLL ? Pin
Talind11-Aug-09 9:01
memberTalind11-Aug-09 9:01 
QuestionConvert webpage to PDF Pin
Steve4568798754423-Jun-09 22:41
memberSteve4568798754423-Jun-09 22:41 
AnswerRe: Convert webpage to PDF Pin
vamshi.k.choutpally16-Mar-10 23:32
membervamshi.k.choutpally16-Mar-10 23:32 
GeneralRe: Convert webpage to PDF Pin
stevemonty3872917-Mar-10 13:30
memberstevemonty3872917-Mar-10 13:30 
GeneralE Pin
Tavox0117-Jun-09 4:44
memberTavox0117-Jun-09 4:44 
Generalsetting height and width of image. also setting cell height and width. Pin
yogi_85858-Jun-09 23:12
memberyogi_85858-Jun-09 23:12 
GeneralTables without borders Pin
gchq1-Jun-09 10:28
membergchq1-Jun-09 10:28 
GeneralRe: Tables without borders Pin
gchq1-Jun-09 14:59
membergchq1-Jun-09 14:59 
QuestionHow Can I save this PDF Files? Pin
maheshTCS22-Apr-09 1:35
membermaheshTCS22-Apr-09 1:35 

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.150901.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by fstrahberger
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid