Thursday, June 11, 2009

Create PDFs in .Net on the fly

I was doing a project in that i want to create PDF reports on the fly. I goggled a lot and i went through lot of tools over internet.But i went back on seeing the 200$ + license.I cannot buy licence becoz i 'm doing project as a self service towards a charity association.Then i came across a open source project called iTextSharp. iTextSharp is a library entirely written ic C# on .Net platform that allows u to create PDF dynamically.

So lets code to create a simple PDF in VB.Net

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html

Public Sub GenerateReport()
Dim Doc As New Document(PageSize.A4, 80, 50, 30, 65)
Dim FileName As String = Guid.NewGuid.ToString
Dim Par As New Paragraph()
Dim oLabResults As New LabResult
Dim iTextTable As iTextSharp.text.Table
Dim imgLogo As iTextSharp.text.Image
Dim strLogoPath As String
Dim oDirInfo As New DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath & "Temp")
PdfWriter.GetInstance(Doc, New FileStream(oDirInfo.ToString & "\" & FileName & ".pdf", FileMode.Create))
Dim oDocHeader As New iTextSharp.text.HeaderFooter(New Phrase("Amrita Hospitals"), False)
Doc.Header = oDocHeader
oDocHeader.Border = Rectangle.NO_BORDER
oDocHeader.Alignment = Rectangle.ALIGN_CENTER
End Sub



Post a Comment