<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>pdf</title>
        <link>http://onteorasoftware.com/category/24.aspx</link>
        <description>pdf</description>
        <language>en-US</language>
        <copyright>Ken Tucker</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Print to PDF</title>
            <link>http://blog.onteorasoftware.net/archive/2008/04/06/print-to-pdf.aspx</link>
            <description>&lt;p&gt;
The .Net framework provides a print document class for printing.  There are times that it would be nice to redirect what you are printing to a pdf.   In this example we are going to use the Sharp Pdf lib version 1.3.1 to print to a pdf.  
&lt;/p&gt;
&lt;p&gt;
The sharp pdf lib allows you to add an image to a page in a pdf.  To make it possible to print to a pdf we are going to create a new print controller class which creates a bitmap and has the print document draw the page on the bitmap.  Then it adds the bitmap as a pdf page.  Once the document is done printing it saves the pdf to disk.  
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://sharppdf.sourceforge.net/"&gt;http://sharppdf.sourceforge.net/&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Update this Project is now available on CodePlex
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.codeplex.com/Print2Pdf"&gt;http://www.codeplex.com/Print2Pdf&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
 
&lt;/p&gt;
&lt;p&gt;
Imports sharpPDF 
&lt;/p&gt;
&lt;p&gt;
Public Class PdfPrintController&lt;br /&gt;
    Inherits Printing.PrintController&lt;br /&gt;
    Dim pdf As pdfDocument&lt;br /&gt;
    Dim bm As Image 
&lt;/p&gt;
&lt;p&gt;
    Private _Author As String = "Unknown" 
&lt;/p&gt;
&lt;p&gt;
    Public Property Author() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return _Author&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            _Author = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private _FileName As String = "Printed.pdf"&lt;br /&gt;
    Public Property FileName() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return _FileName&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            _FileName = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Private _Title As String = "Unknown"&lt;br /&gt;
    Public Property Title() As String&lt;br /&gt;
        Get&lt;br /&gt;
            Return _Title&lt;br /&gt;
        End Get&lt;br /&gt;
        Set(ByVal value As String)&lt;br /&gt;
            _Title = value&lt;br /&gt;
        End Set&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Public Overrides ReadOnly Property IsPreview() As Boolean&lt;br /&gt;
        Get&lt;br /&gt;
            Return True&lt;br /&gt;
        End Get&lt;br /&gt;
    End Property 
&lt;/p&gt;
&lt;p&gt;
    Public Overrides Function OnStartPage(ByVal document As System.Drawing.Printing.PrintDocument, ByVal e As System.Drawing.Printing.PrintPageEventArgs) As System.Drawing.Graphics&lt;br /&gt;
        bm = New Bitmap(e.PageBounds.Width, e.PageBounds.Height)&lt;br /&gt;
        Dim g As Graphics = Graphics.FromImage(bm)&lt;br /&gt;
        g.Clear(Color.White)&lt;br /&gt;
        Return g&lt;br /&gt;
    End Function 
&lt;/p&gt;
&lt;p&gt;
    Public Overrides Sub OnStartPrint(ByVal document As System.Drawing.Printing.PrintDocument, ByVal e As System.Drawing.Printing.PrintEventArgs)&lt;br /&gt;
        pdf = New pdfDocument(Title, Author)&lt;br /&gt;
        MyBase.OnStartPrint(document, e)&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Public Overrides Sub OnEndPage(ByVal document As System.Drawing.Printing.PrintDocument, ByVal e As System.Drawing.Printing.PrintPageEventArgs)&lt;br /&gt;
        Dim p As pdfPage = pdf.addPage(e.PageBounds.Height, e.PageBounds.Width)&lt;br /&gt;
        p.addImage(bm, 0, 0)&lt;br /&gt;
        MyBase.OnEndPage(document, e)&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Public Overrides Sub OnEndPrint(ByVal document As System.Drawing.Printing.PrintDocument, ByVal e As System.Drawing.Printing.PrintEventArgs)&lt;br /&gt;
        pdf.createPDF(FileName)&lt;br /&gt;
        MyBase.OnEndPrint(document, e)&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
End Class 
&lt;/p&gt;
&lt;p&gt;
Here is a sample which creates a pdf of the Northwind product list. 
&lt;/p&gt;
&lt;p&gt;
Imports System.Data.SqlClient 
&lt;/p&gt;
&lt;p&gt;
Public Class Form1&lt;br /&gt;
    Public WithEvents p As New Printing.PrintDocument&lt;br /&gt;
    Dim iRecord As Integer = 0&lt;br /&gt;
    Dim fntPrice As New Font("Arial", 12)&lt;br /&gt;
    Dim ds As New DataSet 
&lt;/p&gt;
&lt;p&gt;
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load&lt;br /&gt;
        Dim strConn As String&lt;br /&gt;
        Dim conn As SqlConnection&lt;br /&gt;
        Dim da As SqlDataAdapter 
&lt;/p&gt;
&lt;p&gt;
        strConn = "Server = .\SQLEXPRESS;"&lt;br /&gt;
        strConn &amp;amp;= "Database = Northwind; Integrated Security = SSPI;"&lt;br /&gt;
        conn = New SqlConnection(strConn)&lt;br /&gt;
        da = New SqlDataAdapter("Select ProductName, UnitPrice From Products", conn)&lt;br /&gt;
        da.Fill(ds, "Products") 
&lt;/p&gt;
&lt;p&gt;
        DataGridView1.DataSource = ds.Tables("Products")&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Sub p_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles p.BeginPrint&lt;br /&gt;
        iRecord = 0&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Sub p_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles p.PrintPage&lt;br /&gt;
        Dim g As Graphics = e.Graphics&lt;br /&gt;
        Dim iPageHeight As Integer = e.PageBounds.Height&lt;br /&gt;
        Dim iPageWidth As Integer = e.PageBounds.Width&lt;br /&gt;
        Dim iFntHeight As Integer = CInt(g.MeasureString("Test", fntPrice).Height)&lt;br /&gt;
        Dim iLinesPerPage As Integer = iPageHeight \ iFntHeight - 15&lt;br /&gt;
        Dim yPos As Integer = 0&lt;br /&gt;
        Dim iTop As Integer&lt;br /&gt;
        Dim iMax As Integer = ds.Tables("Products").Rows.Count&lt;br /&gt;
        Dim strDescription As String&lt;br /&gt;
        Dim x As Integer&lt;br /&gt;
        Dim xPos As Integer&lt;br /&gt;
        Dim strPrice As String&lt;br /&gt;
        Dim fntTitle As Font = New Font("Microsoft Sans Serf", 14)&lt;br /&gt;
        Dim iCount As Integer = ds.Tables("Products").Rows.Count&lt;br /&gt;
        Dim strDate As String = Trim(Now.ToLongDateString)&lt;br /&gt;
        Dim sf As New StringFormat&lt;br /&gt;
        sf.Alignment = StringAlignment.Far 
&lt;/p&gt;
&lt;p&gt;
        xPos = CInt(iPageWidth - g.MeasureString("Price List", fntTitle).Width) \ 2&lt;br /&gt;
        g.DrawString("Price List", fntTitle, Brushes.Black, xPos, 10)&lt;br /&gt;
        yPos = 10 + CInt(g.MeasureString("Price List", fntTitle).Height) 
&lt;/p&gt;
&lt;p&gt;
        xPos = CInt(iPageWidth - g.MeasureString(strDate, fntPrice).Width) \ 2&lt;br /&gt;
        g.DrawString(strDate, fntPrice, Brushes.Black, xPos, yPos)&lt;br /&gt;
        yPos += 2 * iFntHeight&lt;br /&gt;
        g.DrawString("Product", fntPrice, Brushes.Black, 50, yPos) 
&lt;/p&gt;
&lt;p&gt;
        g.DrawString("Price", fntPrice, Brushes.Black, _&lt;br /&gt;
                New Rectangle(430, yPos, 100, 2 * iFntHeight), sf) 
&lt;/p&gt;
&lt;p&gt;
        yPos += iFntHeight&lt;br /&gt;
        g.DrawLine(Pens.Black, 0, yPos, iPageWidth, yPos) 
&lt;/p&gt;
&lt;p&gt;
        e.HasMorePages = True&lt;br /&gt;
        iTop = yPos 
&lt;/p&gt;
&lt;p&gt;
        For x = 0 To iLinesPerPage&lt;br /&gt;
            If iRecord &amp;lt; imax Then&lt;br /&gt;
                With ds.Tables("Products").Rows(iRecord)&lt;br /&gt;
                    strDescription = .Item("ProductName").ToString&lt;br /&gt;
                    strPrice = Convert.ToDecimal(.Item("UnitPrice")).ToString("c")&lt;br /&gt;
                End With&lt;br /&gt;
                Dim rName As New Rectangle(5, yPos, 400, iFntHeight)&lt;br /&gt;
                Dim rPrice As New Rectangle(430, yPos, 100, iFntHeight) 
&lt;/p&gt;
&lt;p&gt;
                g.DrawString(strDescription, fntPrice, Brushes.Black, rName)&lt;br /&gt;
                g.DrawString(strPrice, fntPrice, Brushes.Black, rPrice, sf)&lt;br /&gt;
            Else&lt;br /&gt;
                e.HasMorePages = False&lt;br /&gt;
            End If&lt;br /&gt;
            yPos += iFntHeight&lt;br /&gt;
            iRecord += 1&lt;br /&gt;
        Next&lt;br /&gt;
        fntTitle.Dispose()&lt;br /&gt;
        If e.HasMorePages = False Then iRecord = 0&lt;br /&gt;
    End Sub 
&lt;/p&gt;
&lt;p&gt;
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click&lt;br /&gt;
        Dim pc As New PdfPrintController&lt;br /&gt;
        pc.Title = "Test Pdf"&lt;br /&gt;
        pc.Author = "Ken Tucker"&lt;br /&gt;
        pc.FileName = "Test.pdf"&lt;br /&gt;
        p.PrintController = pc&lt;br /&gt;
        p.Print()&lt;br /&gt;
    End Sub&lt;br /&gt;
End Class 
&lt;/p&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.onteorasoftware.net%2fpost%2fPrint-to-PDF.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.onteorasoftware.net%2fpost%2fPrint-to-PDF.aspx" border="0" alt="kick it on DotNetKicks.com" width="82" height="18" /&gt;&lt;/a&gt; 
&lt;img src="http://blog.onteorasoftware.net/aggbug/28.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ken Tucker</dc:creator>
            <guid>http://blog.onteorasoftware.net/archive/2008/04/06/print-to-pdf.aspx</guid>
            <pubDate>Sun, 06 Apr 2008 17:30:12 GMT</pubDate>
            <wfw:comment>http://blog.onteorasoftware.net/comments/28.aspx</wfw:comment>
            <comments>http://blog.onteorasoftware.net/archive/2008/04/06/print-to-pdf.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://blog.onteorasoftware.net/comments/commentRss/28.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>