Click here to Skip to main content
15,879,066 members
Articles / Web Development / ASP.NET

Creating PDF Documents in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.74/5 (63 votes)
26 Sep 2007CPOL 694.9K   22.8K   255   85
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:

VB
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:

VB
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:

VB
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:

VB
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)


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

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

Comments and Discussions

 
GeneralThank you Pin
Phorozan19803-Oct-07 1:09
Phorozan19803-Oct-07 1:09 
GeneralRe: Thank you Pin
fstrahberger3-Oct-07 20:56
fstrahberger3-Oct-07 20:56 
Questioncan i read pdf with this library? Pin
lak-b28-Sep-07 5:00
lak-b28-Sep-07 5:00 
AnswerRe: can i read pdf with this library? Pin
T-Star1-Oct-07 13:10
T-Star1-Oct-07 13:10 
GeneralGreat place to get started Pin
Sajjitha Gunawardana27-Sep-07 5:01
Sajjitha Gunawardana27-Sep-07 5:01 
GeneralURL Pin
ryanoc33327-Sep-07 2:21
ryanoc33327-Sep-07 2:21 
GeneralRe: URL Pin
felipesabino30-Sep-07 4:06
felipesabino30-Sep-07 4:06 
GeneralRe: URL Pin
Ratface231-Oct-07 9:18
Ratface231-Oct-07 9:18 
GeneralRe: URL Pin
fstrahberger1-Oct-07 22:20
fstrahberger1-Oct-07 22:20 
AnswerRe: URL Pin
existenz_1-Oct-07 11:51
existenz_1-Oct-07 11:51 

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.