Click here to Skip to main content
15,886,799 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 695.4K   22.8K   255  
How to create PDF documents in ASP.NET.
Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

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

    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

    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

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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