Click here to Skip to main content
Click here to Skip to main content
Go to top

Creating PDF Documents in ASP.NET

, 26 Sep 2007
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

Questionhow can i show pdf file with btn click even as it has been created Pinmembermanoj.barmola17-Sep-09 22:39 
Generalpdf envelope using itextsharp Pinmembercpandiya13-Sep-09 23:30 
Questionhow to create arabic pdf files Pinmembershaaa5-Sep-09 0:19 
Questiondoes anyone know of a document laying out all the functionality of this DLL ? PinmemberTalind11-Aug-09 9:01 
QuestionConvert webpage to PDF PinmemberSteve4568798754423-Jun-09 22:41 
AnswerRe: Convert webpage to PDF Pinmembervamshi.k.choutpally16-Mar-10 23:32 
GeneralRe: Convert webpage to PDF Pinmemberstevemonty3872917-Mar-10 13:30 
AnswerRe: Convert webpage to PDF PinmemberGarrylg10-Apr-14 22:12 
GeneralE PinmemberTavox0117-Jun-09 4:44 
Generalsetting height and width of image. also setting cell height and width. Pinmemberyogi_85858-Jun-09 23:12 
GeneralTables without borders Pinmembergchq1-Jun-09 10:28 
GeneralRe: Tables without borders Pinmembergchq1-Jun-09 14:59 
QuestionHow Can I save this PDF Files? PinmembermaheshTCS22-Apr-09 1:35 
AnswerRe: How Can I save this PDF Files? Pinmemberfstrahberger22-Apr-09 7:18 
The code:
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\1.pdf",FileMode.Create))
creates a new file at the specified location. To store the pdf-file anywhere outside the IIS-directory you could try:
PdfWriter.GetInstance(doc, New FileStream(C:\tmp\1.pdf",FileMode.Create))

QuestionIs it possible to make Paragraph ? Pinmembermomydia1-Apr-09 6:55 
GeneralRegarding Licenses PinmemberSenthil S29-Jan-09 20:00 
QuestionPassword Protected Pdf....Please Help me....Urgent PinmemberAnishUnnithan13-Nov-08 19:08 
GeneralFont size Pinmemberradhakrishchn5-Nov-08 0:37 
GeneralRe: Font size Pinmemberfstrahberger6-Nov-08 4:15 
QuestionNew tables only?? Pinmemberdherrmann14-Oct-08 1:15 
AnswerRe: New tables only?? Pinmemberfstrahberger6-Nov-08 4:19 
GeneralC# Verssion Pinmembersheahad13-Jun-08 18:53 
GeneralRe: C# Verssion PinmemberEihab0079-Oct-08 3:55 
GeneralRe: C# Verssion PinmemberArkan#130-Oct-08 8:51 
GeneralRe: C# Verssion Pinmemberfstrahberger6-Nov-08 4:32 
GeneralRe: C# Verssion Pinmemberkx250f3721-Jul-09 10:32 
GeneralRe: C# Verssion PinmemberFlorianMayer26-Jul-09 23:26 
GeneralComplied DLL with AllowPartiallyTrustedCallers Pinmemberchrisreece15-Apr-08 0:05 
GeneralThank you so much Pinmembersrinivas_gppm23-Mar-08 23:00 
QuestionImages? PinmemberJohnny J.4-Mar-08 23:42 
AnswerRe: Images? PinmemberJohnny J.4-Mar-08 23:47 
AnswerRe: Images? Pinmemberfstrahberger8-Mar-08 13:14 
QuestionSecurity Error when running in Production Pinmemberdev_4u200013-Jan-08 21:54 
GeneralRe: Security Error when running in Production Pinmemberfstrahberger8-Mar-08 13:48 
GeneralFont PinsussDana6-Dec-07 4:57 
Questionwhere did you get the dll Pinmemberdeuce415-Oct-07 15:50 
AnswerRe: where did you get the dll Pinmemberfstrahberger15-Oct-07 21:41 
GeneralURL to PDF Example PinmemberJim Mac5-Oct-07 8:02 
GeneralThank you PinmemberPhorozan19803-Oct-07 1:09 
GeneralRe: Thank you Pinmemberfstrahberger3-Oct-07 20:56 
Questioncan i read pdf with this library? Pinmemberlak-b28-Sep-07 5:00 
AnswerRe: can i read pdf with this library? PinmemberT-Star1-Oct-07 13:10 
GeneralGreat place to get started PinmemberSajjitha Gunawardana27-Sep-07 5:01 
GeneralGreat Pinmemberrajantawate1(http// 4:19 
GeneralURL Pinmemberinetfly12327-Sep-07 2:21 
GeneralRe: URL Pinmemberfelipesabino30-Sep-07 4:06 
GeneralRe: URL PinmemberRatface231-Oct-07 9:18 
GeneralRe: URL Pinmemberfstrahberger1-Oct-07 22:20 
AnswerRe: URL Pinmemberexistenz_1-Oct-07 11:51 

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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by fstrahberger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid