Click here to Skip to main content
6,595,854 members and growing! (17,379 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Creating PDF documents in ASP.NET

By fstrahberger

Creating PDF documents in ASP.NET
Windows, .NET 2.0, ASP.NET, WebForms, Dev
Posted:26 Sep 2007
Views:91,772
Bookmarked:120 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 5.49 Rating: 4.46 out of 5

1
1 vote, 5.9%
2

3
6 votes, 35.3%
4
10 votes, 58.8%
5
Screenshot - 081_hello_pdf.jpg

Introduction

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

The code

First of all I 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 does also a "Response.Redirect" to the created PDF. First import some required Namespaces:

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Next I decide in the Page_Load event 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 the most simple document with just one string "Hello World" and the 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, but also creates a PDF document and redirects the user:

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 library http://sourceforge.net/projects/itextsharp/ makes it very easy to create PDF documents from web applications.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

fstrahberger


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
Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 47 (Total in Forum: 47) (Refresh)FirstPrevNext
Questionhow should i add fussioncharts to this pdf? PinmemberPatilvaishalimangesh2:52 29 Oct '09  
Generalhow can i show pdf file with btn click even as it has been created Pinmembermanoj.barmola23:39 17 Sep '09  
Generalpdf envelope using itextsharp Pinmembercpandiya0:30 14 Sep '09  
Generalhow to create arabic pdf files Pinmembershaaa1:19 5 Sep '09  
Generaldoes anyone know of a document laying out all the functionality of this DLL ? PinmemberTalind10:01 11 Aug '09  
QuestionConvert webpage to PDF PinmemberSteve4568798754423:41 23 Jun '09  
GeneralE PinmemberTavox015:44 17 Jun '09  
Generalsetting height and width of image. also setting cell height and width. Pinmemberyogi_85850:12 9 Jun '09  
GeneralTables without borders Pinmembergchq11:28 1 Jun '09  
GeneralRe: Tables without borders Pinmembergchq15:59 1 Jun '09  
GeneralHow Can I save this PDF Files? PinmembermaheshTCS2:35 22 Apr '09  
GeneralRe: How Can I save this PDF Files? Pinmemberfstrahberger8:18 22 Apr '09  
GeneralIs it possible to make Paragraph ? Pinmembermomydia7:55 1 Apr '09  
GeneralRegarding Licenses PinmemberSenthil S21:00 29 Jan '09  
QuestionPassword Protected Pdf....Please Help me....Urgent PinmemberAnishUnnithan20:08 13 Nov '08  
GeneralFont size Pinmemberradhakrishchn1:37 5 Nov '08  
GeneralRe: Font size Pinmemberfstrahberger5:15 6 Nov '08  
GeneralNew tables only?? Pinmemberdherrmann2:15 14 Oct '08  
GeneralRe: New tables only?? Pinmemberfstrahberger5:19 6 Nov '08  
GeneralC# Verssion Pinmembersheahad19:53 13 Jun '08  
GeneralRe: C# Verssion PinmemberEihab0074:55 9 Oct '08  
GeneralRe: C# Verssion PinmemberArkan#19:51 30 Oct '08  
GeneralRe: C# Verssion Pinmemberfstrahberger5:32 6 Nov '08  
GeneralRe: C# Verssion Pinmemberkx250f3711:32 21 Jul '09  
GeneralRe: C# Verssion PinmemberFlorianMayer0:26 27 Jul '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 Sep 2007
Editor:
Copyright 2007 by fstrahberger
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project