helloworld
[[helloworld]] last edit on
Jun 27, 2007
1:14 PM
by thomas_hoevel
The .ASPX code
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HelloWorld.aspx.vb"
Inherits="HelloWorld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
The .ASPX.VB code (code behind page)
Imports System.IO Imports PdfSharp Imports PdfSharp.Drawing Imports PdfSharp.Drawing.Layout Imports PdfSharp.Pdf Imports PdfSharp.Pdf.IO
Partial Class HelloWorld
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim oPDFDocument As PdfDocument
Dim oPDFPage As PdfPage
Dim oXGraphics As XGraphics
Dim oXFont As XFont
Dim oXTextFormatter As XTextFormatter
Dim sString As String
Dim oMemoryStream As MemoryStream
oPDFDocument = New PdfDocument 'create a new PDF document
oPDFPage = oPDFDocument.AddPage 'create a new page in the documetn
oXGraphics = XGraphics.FromPdfPage(oPDFPage) 'get an XGraphics object from the page for drawing
'draw crossing lines
Dim pen As XPen = New XPen(XColor.FromArgb(255, 0, 0))
oXGraphics.DrawLine(pen, New XPoint(0, 0), New XPoint(oPDFPage.Width.Point, oPDFPage.Height.Point))
oXGraphics.DrawLine(pen, New XPoint(oPDFPage.Width.Point, 0), New XPoint(0, oPDFPage.Height.Point))
'draw an elipse
oXGraphics.DrawEllipse(pen, _
3 * oPDFPage.Width.Point / 10, 3 * oPDFPage.Height.Point / 10, 2 * oPDFPage.Width.Point / 5, 2 * oPDFPage.Height.Point / 5)
' Create a font
oXFont = New XFont("Verdana", 20, XFontStyle.Bold)
' Draw the text in the middle of the page
oXGraphics.DrawString("Hello, World!", oXFont, XBrushes.Black, _
New XRect(0, 0, oPDFPage.Width.Point, oPDFPage.Height.Point), XStringFormat.Center)
' Draw a rectangle to use as a back ground
oXGraphics.DrawRectangle(XBrushes.LightYellow, _
New XRect(100, 540, oPDFPage.Width.Point - 200, oXFont.Height * 5))
' Put some text in the rectangle
sString = "This piece of text is displayed on top of the rectangle which was drawn in the " & _
"previous section of code. It is drawn in the same area and is left justified."
oXTextFormatter = New XTextFormatter(oXGraphics)
oXTextFormatter.Alignment = XParagraphAlignment.Left
oXTextFormatter.DrawString(sString, oXFont, XBrushes.Black, _
New XRect(100, 540, oPDFPage.Width.Point - 200, oXFont.Height * 5), XStringFormat.TopLeft)
' Draw a rectangle to use as a border
oXGraphics.DrawRectangle(XPens.Black, _
New XRect(100, 650, oPDFPage.Width.Point - 200, oXFont.Height * 5))
' Put some text inside the border
sString = "This piece of text is displayed inside a border which was drawn in the " & _
"previous section of code. It is drawn in the same area and is centered."
oXTextFormatter = New XTextFormatter(oXGraphics)
oXTextFormatter.Alignment = XParagraphAlignment.Center
oXTextFormatter.DrawString(sString, oXFont, XBrushes.Black, _
New XRect(100, 650, oPDFPage.Width.Point - 200, oXFont.Height * 5), XStringFormat.TopLeft)
'display the document
oMemoryStream = New MemoryStream
oPDFDocument.Save(oMemoryStream, False)
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", oMemoryStream.Length.ToString())
Response.AddHeader("content-disposition", "attachment; filename=HelloWorld.pdf")
Response.BinaryWrite(oMemoryStream.ToArray())
Response.Flush()
oMemoryStream.Close()
Response.End()
oXFont = Nothing
oXGraphics = Nothing
oPDFPage = Nothing
oPDFDocument = Nothing
End Sub
End Class