jump to navigation

How to create PDFs from ASP.NET September 20 2005

I’m currently involved in a project where one of the functional requirements is to allow users to print documents without having the annoying web address bar on the footer of every page. This seems to be an impossible task since printing is handled by the client application (browser + window.print()) which the server has no control over.

An alternate method of achieving this was to create PDFs on the fly from the database data. After much searching I found what appears to be the holy grail for such a task. There is a library suite called iTextSharp. This is a free public licenced software library that will allow you to create PDFs via J#, C# and VB.NET.

1. Download the library (compiled DLLs)
2. Download the examples
3. Create a new ASP.NET project
4. Create references to the following DLLs:

5. Take a look at the chapter 1 example for VB.NET…. very simple

— START first example provided by iText —
Imports System
Imports com.lowagie.text
Imports com.lowagie.text.pdf
Imports System.IO

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
‘Put user code to initialize the page here

‘ step 1: creation of a document-object
Dim document As New Document
Dim strFileName As String = “e:\itext\example.pdf”

‘ step 2:
‘ we create a writer that listens to the document
‘ and directs a PDF-stream to a file

PdfWriter.getInstance(document, New FileStream(strFileName, FileMode.Create))

‘ step 3: we open the document
document.open()

‘ step 4: we add a paragraph to the document
Dim i As Integer
For i = 0 To 100
document.add(New Paragraph(”Hello World : ” & i))
Next

‘ step 5: we close the document

document.close()

….
end sub

Comments

Sorry comments are closed for this entry