Thursday 2 August 2007

ASP.NET - Export to PDF

First of all, sorry for the lack of updates (is anyone out there reading this?) :) I'll have to update it a bit to try and bring in visitors.

Ok, we know how to export to Excel but how can we export to PDF? Below I shall explain using the iTextSharp library http://sourceforge.net/projects/itextsharp/ to export a table to PDF. There hasn't been much documentation on this on the web, especially when it's regarding VB as a language.

The example below is for exporting a table to PDF, however the functions are there to export absolutely anything, even plain text such as a word document. Make your own application and decide what you need it to do.

First of all, declare the following:


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

Dim ds As New System.Data.DataSet
Dim dg As New DataGrid
Dim stringWrite As New System.IO.StringWriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)



Now fill your DataSet (ds) with data somehow (I used MySQL to run a query against a database) then do the following:


dg.DataSource = ds.Tables(0)
dg.DataBind()

Dim mytable As New PdfPTable(ds.Tables(0).Columns.Count)

mytable.SpacingBefore = 8
mytable.DefaultCell.Padding = 1
mytable.WidthPercentage = 100
mytable.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED
mytable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE

Dim t
For t = 0 To ds.Tables(0).Columns.Count - 1

Dim cell As New PdfPCell
cell.BorderWidth = 0.001F
cell.BackgroundColor = New Color(101, 197, 250)
cell.BorderColor = New Color(0, 0, 0)
cell.Phrase = New Phrase(ds.Tables(0).Columns(t).ToString, FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 8, Font.BOLD))
mytable.AddCell(cell)

Next



Dim r
Dim c

For r = 0 To ds.Tables(0).Rows.Count - 1
For c = 0 To ds.Tables(0).Columns.Count - 1

Dim cell As New PdfPCell

cell.BorderWidth = 0.001F
cell.BackgroundColor = New Color(255, 255, 255)
cell.BorderColor = New Color(0, 0, 0)
cell.Phrase = New Phrase(ds.Tables(0).Rows(r).Item(c).ToString, FontFactory.GetFont("TIMES_ROMAN", BaseFont.WINANSI, 8, Font.BOLD))
mytable.AddCell(cell)

Next
Next



First we create the columns with different formatting and then we step through every cell and build the PDF. Finally, we write the file and certain attributes:




Try

Dim mystream As New MemoryStream()
Dim document As New Document(PageSize.A4.Rotate(), 15, 15, 15, 15)
Dim writer As PdfWriter
writer = PdfWriter.GetInstance(document, mystream)
writer.SetEncryption(PdfWriter.STRENGTH40BITS, "", "", PdfWriter.AllowPrinting)
document.AddAuthor("Joe Bloggs")
document.AddSubject("Published from my ASP.NET page")
document.AddTitle("Exported Document")
document.Open()
document.Add(mytable)
document.Close()
mystream.Flush()
mystream.Close()

Dim bytearray As Byte() = mystream.ToArray
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=export.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(bytearray)

Catch ex As DocumentException
Response.Write(ex.Message.ToString)
End Try



And there you have it! It is much easier to explain how to export normal text and not a table but if you understand how this works, the rest is easy!

A point to note is that you can allow unrestricted access to this PDF by commenting out this line: writer.SetEncryption(PdfWriter.STRENGTH40BITS, "", "", PdfWriter.AllowPrinting). This blocks editing of the document etc.

If you have any questions or problems, post a comment.

No comments: