Click here to Skip to main content
12,821,494 members (22,865 online)
Click here to Skip to main content
Add your own
alternative version


253 bookmarked
Posted 26 Sep 2007

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: <a href=>

For video tutorials about, ajax, gridviews, ... (in german) visit: <a href=>

You may also be interested in...


Comments and Discussions

GeneralFont size Pin
radhakrishchn5-Nov-08 1:37
memberradhakrishchn5-Nov-08 1:37 
GeneralRe: Font size Pin
fstrahberger6-Nov-08 5:15
memberfstrahberger6-Nov-08 5:15 
QuestionNew tables only?? Pin
dherrmann14-Oct-08 2:15
memberdherrmann14-Oct-08 2:15 
AnswerRe: New tables only?? Pin
fstrahberger6-Nov-08 5:19
memberfstrahberger6-Nov-08 5:19 
GeneralC# Verssion Pin
sheahad13-Jun-08 19:53
membersheahad13-Jun-08 19:53 
GeneralRe: C# Verssion Pin
Eihab0079-Oct-08 4:55
memberEihab0079-Oct-08 4:55 
GeneralRe: C# Verssion Pin
Arkan#130-Oct-08 9:51
memberArkan#130-Oct-08 9:51 
GeneralRe: C# Verssion Pin
fstrahberger6-Nov-08 5:32
memberfstrahberger6-Nov-08 5:32 
Try this conversion tool:[^]

This generated for me:

public void ShowTable() 
    Document doc = new Document(); 
    PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\2.pdf", FileMode.Create)); 
    Table table = new Table(3); 
    table.BorderWidth = 1; 
    table.BorderColor = new Color(0, 0, 255); 
    table.Padding = 3; 
    table.Spacing = 1; 
    Cell 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"); 

GeneralRe: C# Verssion Pin
kx250f3721-Jul-09 11:32
memberkx250f3721-Jul-09 11:32 
GeneralRe: C# Verssion Pin
FlorianMayer27-Jul-09 0:26
memberFlorianMayer27-Jul-09 0:26 
GeneralComplied DLL with AllowPartiallyTrustedCallers Pin
chrisreece15-Apr-08 1:05
memberchrisreece15-Apr-08 1:05 
GeneralThank you so much Pin
srinivas_gppm24-Mar-08 0:00
membersrinivas_gppm24-Mar-08 0:00 
QuestionImages? Pin
Johnny J.5-Mar-08 0:42
memberJohnny J.5-Mar-08 0:42 
AnswerRe: Images? Pin
Johnny J.5-Mar-08 0:47
memberJohnny J.5-Mar-08 0:47 
AnswerRe: Images? Pin
fstrahberger8-Mar-08 14:14
memberfstrahberger8-Mar-08 14:14 
QuestionSecurity Error when running in Production Pin
dev_4u200013-Jan-08 22:54
memberdev_4u200013-Jan-08 22:54 
GeneralRe: Security Error when running in Production Pin
fstrahberger8-Mar-08 14:48
memberfstrahberger8-Mar-08 14:48 
GeneralFont Pin
Dana6-Dec-07 5:57
sussDana6-Dec-07 5:57 
Questionwhere did you get the dll Pin
deuce415-Oct-07 16:50
memberdeuce415-Oct-07 16:50 
AnswerRe: where did you get the dll Pin
fstrahberger15-Oct-07 22:41
memberfstrahberger15-Oct-07 22:41 
GeneralURL to PDF Example Pin
Jim Mac5-Oct-07 9:02
memberJim Mac5-Oct-07 9:02 
GeneralThank you Pin
Phorozan19803-Oct-07 2:09
memberPhorozan19803-Oct-07 2:09 
GeneralRe: Thank you Pin
fstrahberger3-Oct-07 21:56
memberfstrahberger3-Oct-07 21:56 
Questioncan i read pdf with this library? Pin
lak-b28-Sep-07 6:00
memberlak-b28-Sep-07 6:00 
AnswerRe: can i read pdf with this library? Pin
T-Star1-Oct-07 14:10
memberT-Star1-Oct-07 14:10 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by fstrahberger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid