Click here to Skip to main content
Licence CPOL
First Posted 26 Sep 2007
Views 322,161
Downloads 8,246
Bookmarked 213 times

Creating PDF Documents in ASP.NET

By fstrahberger | 26 Sep 2007
How to create PDF documents in ASP.NET.

1
3 votes, 6.8%
2

3
11 votes, 25.0%
4
30 votes, 68.2%
5
4.67/5 - 44 votes
3 removed
μ 4.53, σa 1.45 [?]

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)

About the Author

fstrahberger

Web Developer

Germany Germany

Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmembersameerAbdulla0:25 10 Jan '12  
QuestionProblem function Table PinmemberMember 85118150:30 23 Dec '11  
QuestionIs this pdf abel to have the HTML controls inside ? Pinmemberamit_usa12:23 12 Dec '11  
Questionitextsharp 4.0.4 Pinmembermauirinho16:22 5 Sep '11  
Generalnot work with some langauge PinmemberBasharss5:01 19 Apr '11  
GeneralMy vote of 5 Pinmemberprajod20:34 26 Feb '11  
Generalthanks Pinmembersyed8315:56 23 Feb '11  
GeneralMy vote of 5 PinmemberBElavarasan22:51 13 Feb '11  
GeneralGreat Pinmemberjluisrojas14:14 17 Jan '11  
GeneralGOOD Pinmemberalessahilton23:09 15 Jan '11  
GeneralMy vote of 2 PinmemberLibin Jose chemperi0:55 7 Dec '10  
GeneralMy vote of 5 PinmemberEswa1:45 8 Nov '10  
GeneralMy vote of 5 PinmemberEswa0:50 29 Oct '10  
GeneralByte[] to File.pdf PinmemberMember 362262314:13 27 Oct '10  
GeneralMy vote of 4 PinmemberCGI99918:14 23 Aug '10  
GeneralMy vote of 2 Pinmemberashish Sharma kumar19:16 11 Aug '10  
GeneralError in server Pinmemberdvbsaravan21:58 20 Jul '10  
GeneralRe: Error in server PinmemberMember 44614333:03 10 Aug '10  
GeneralMy vote of 5 PinmemberDon_Hard4:39 11 Jul '10  
GeneralCompile time error: Table, Cell and AddCell method define PinmemberBajrang Singh23:51 17 Jun '10  
GeneralRe: Compile time error: Table, Cell and AddCell method define PinmemberMember 377829013:33 5 Aug '10  
GeneralTable Pinmembermaxnis10:54 17 Jun '10  
GeneralRe: Table Pinmembergrokuk5:00 7 Jul '10  
GeneralRe: Table PinmemberChristineDaae4:28 7 Jan '11  
QuestionHow to set the width and height of a column? PinmemberMenandro5:03 2 Feb '10  

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.

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