web 2.0

ASP.NET(vb.net) & Export/Report Database to Pdf

ASP.NET(vb.net) & Export/Report Database to Pdf - This article explains and example how to create PDF documents Export data from Database.

ShotDev Focus:
- ASP.NET(vb.net) & Export/Report Database to Pdf

Example

AspNetPDFReport.aspx

<%@ Import Namespace="PdfSharp"%>
<%@ Import Namespace="PdfSharp.Drawing"%>
<%@ Import Namespace="PdfSharp.Pdf"%>
<%@ Import Namespace="PdfSharp.Pdf.IO"%>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
' Create a new PDF document
Dim DocPDF As PdfDocument = New PdfDocument

' Create an empty page
Dim objPage As PdfPage = DocPDF.AddPage

' Get an XGraphics object for drawing
Dim gfx As XGraphics = XGraphics.FromPdfPage(objPage)

' Create a font
Dim font1 As XFont = New XFont("Verdana", 15, XFontStyle.Bold)
Dim font2 As XFont = New XFont("Tahoma", 8, XFontStyle.Bold)
Dim font3 As XFont = New XFont("Tahoma", 8, 0)

' Draw the text
gfx.DrawString("My Customer", font1, XBrushes.Black, _
New XRect(0, 50, objPage.Width.Point, objPage.Height.Point), XStringFormats.TopCenter)

' Draw the text
gfx.DrawString("CustomerID", font2, XBrushes.Black, 65, 80, XStringFormats.TopLeft)
gfx.DrawString("Name", font2, XBrushes.Black, 140, 80, XStringFormats.TopLeft)
gfx.DrawString("Email", font2, XBrushes.Black, 215, 80, XStringFormats.TopLeft)
gfx.DrawString("CountryCode", font2, XBrushes.Black, 365, 80, XStringFormats.TopLeft)
gfx.DrawString("Budget", font2, XBrushes.Black, 425, 80, XStringFormats.TopLeft)
gfx.DrawString("Used", font2, XBrushes.Black, 485, 80, XStringFormats.TopLeft)

' Line
Dim pen As XPen = New XPen(XColor.FromArgb(0, 0, 0))
gfx.DrawLine(pen, New XPoint(65, 78), New XPoint(520, 78))
gfx.DrawLine(pen, New XPoint(65, 90), New XPoint(520, 90))

' Customer From Database (Start)

Dim objConn As New OleDbConnection
Dim objCmd As New OleDbCommand
Dim dtAdapter As New OleDbDataAdapter

Dim ds As New DataSet
Dim dt As DataTable
Dim strConnString,strSQL As String

strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
strSQL = "SELECT * FROM customer"

objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd

dtAdapter.Fill(ds)
dt = ds.Tables(0)

dtAdapter = Nothing
objConn.Close()
objConn = Nothing

Dim intLine As Integer = 90
Dim i As Integer

For i = 0 To dt.Rows.Count - 1
gfx.DrawString(dt.Rows(i)("CustomerID"), font3, XBrushes.Black, 65, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Name"), font3, XBrushes.Black, 140, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Email"), font3, XBrushes.Black, 215, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("CountryCode"), font3, XBrushes.Black, 365, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Budget"), font3, XBrushes.Black, 425, intLine, XStringFormats.TopLeft)
gfx.DrawString(dt.Rows(i)("Used"), font3, XBrushes.Black, 485, intLine, XStringFormats.TopLeft)
intLine = intLine + 10
Next

' Customer From Database (End)

' Save the document...
Dim FileName As String = "MyPDF/PdfDoc.pdf"
DocPDF.Save(Server.MapPath(FileName))

DocPDF.Close()
DocPDF = Nothing

Me.lblText.Text = "PDF Created <a href=" & FileName & ">click here</a> to view"

End Sub

</script>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>

Screenshot

ASP.NET(vb.net) & Export/Report Database to Pdf
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.