Hide

SftPrintPreview/OCX 1.0 - ActiveX Print Preview Control

Display
Print

PreviewPages Sample (VB.NET)

This sample illustrates application-generated output.

The source code is located at C:\Program Files (x86)\Softelvdm\SftPrintPreview OCX 1.0\Samples\Visual Studio - VB.NET\PreviewPages\Form1.vb or C:\Program Files\Softelvdm\SftPrintPreview OCX 1.0\Samples\Visual Studio - VB.NET\PreviewPages\Form1.vb (on 32-bit Windows versions).

Private Sub MenuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuExit.Click
    Application.Exit()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AxSftPrintPreview1.Dock = DockStyle.Fill

    ' Use the RenderContent event to render pages
    AxSftPrintPreview1.ContentProviderCallback(0, IntPtr.Zero, IntPtr.Zero)

    Show() ' Show this form so we get the form and the messagebox at the same time

    MessageBox.Show("This example demonstrates application-generated output." & vbCr & vbLf & vbCr & vbLf & "It prints one bitmap and 50 lines of text a total of 20 times.  " & _
        "The included source code illustrates how an application can print one page at a time, with an easy " & _
        "mechanism to keep track of restart positions.")
End Sub

Private Sub AxSftPrintPreview1_PageSetupWanted(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxSftPrintPreview1.PageSetupWanted
    AxSftPrintPreview1.PageSetup(0)
End Sub

Private Sub AxSftPrintPreview1_HelpWanted(ByVal sender As Object, ByVal e As AxSftPrintPreviewLib10._ISftPrintPreviewEvents_HelpWantedEvent) Handles AxSftPrintPreview1.HelpWanted
    MessageBox.Show("Sorry, this sample doesn't offer a help file")
End Sub

Private Sub AxSftPrintPreview1_CloseWanted(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxSftPrintPreview1.CloseWanted
    Application.Exit()
End Sub

Private Sub AxSftPrintPreview1_RenderContent(ByVal sender As Object, ByVal e As AxSftPrintPreviewLib10._ISftPrintPreviewEvents_RenderContentEvent) Handles AxSftPrintPreview1.RenderContent
    ' This function is called by SftPrintPreview to print pages, to initialize and
    ' terminate printing (to prepare and free resources) and for page positioning
    If e.function = SftPrintPreviewRenderContentConstants.renderSftPrintPreview_Init Then
        m_PrintFont = New Font("Arial", 10)
    ElseIf e.function = SftPrintPreviewRenderContentConstants.renderSftPrintPreview_Term Then
        m_PrintFont = Nothing
    ElseIf e.function = SftPrintPreviewRenderContentConstants.renderSftPrintPreview_1Page Then
        PrintOnePage(e.visitedPageInfo, e.lastPage)
    End If
End Sub

Private Sub PrintOnePage(ByRef VisitedPageInfo As Object, ByRef LastPage As Integer)
    ' Print one page (can occur multiple times)
    ' RenderingInfo.CurrentPage has the absolute page # to print (0..n)
    ' RenderingInfo.PreviousVisitedPageInfo contains the value you returned in
    ' RenderingInfo.VisitedPageInfo when you finished printing the previous page
    ' This allows you to simply restart where you left off.
    ' The information you return in RenderingInfo.VisitedPageInfo is of type
    ' object, so it can be a simple Counter or even a complex object, with cached
    ' information.

    ' In this example we print 1 bitmap, then 50 lines of text,
    ' 20 times

    ' We use RenderingInfo.PreviousVisitedPageInfo to resume printing.
    ' We simply start at 0 and add 1 for each bitmap or line we
    ' printed. Using a simple formula, we can determine where we
    ' left off.  In an application, the concept remains the same, but
    ' instead of a simple Counter, you could use RenderingInfo.VisitedPageInfo
    ' to store more complex information.

    ' Set up a Graphics object where we render the page
    Dim g As Graphics = Graphics.FromHdc(New IntPtr(AxSftPrintPreview1.RenderingInfo.hDC))
    g.PageUnit = GraphicsUnit.Pixel ' set it to pixel mode
    ' It is also possible to set it to GraphicsUnit.Inch or GraphicsUnit.Millimeter or the other
    ' unit modes.  Information in RenderingInfo is available in pixels and inches.

    Dim Counter As Integer = 0
    Dim brush As SolidBrush = New SolidBrush(Color.Black) ' Color printers support other colors too
    Dim outRect As Rectangle = New Rectangle(AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.left, AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.top, _
        AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.right - AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.left, AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.bottom - AxSftPrintPreview1.RenderingInfo.OutputRectDisplayPix.top)

    If AxSftPrintPreview1.RenderingInfo.PreviousVisitedPageInfo <> Nothing Then
        Counter = AxSftPrintPreview1.RenderingInfo.PreviousVisitedPageInfo ' we left off here on the previous page
    End If

    ' starting output position on this page
    Dim xPrinter As Integer, yPrinter As Integer
    xPrinter = outRect.Left
    yPrinter = outRect.Top

    While True
        If Counter >= (1 + 50) * 20 Then ' 1 bitmap, 50 lines of text, 20 times
            ' we have printed 1 bitmap and 50 lines 20 times, we're done
            LastPage = AxSftPrintPreview1.RenderingInfo.CurrentPage ' return that this is the last page
            Exit While
        End If

        If (Counter Mod (50 + 1)) = 0 Then
            ' Print a bitmap

            ' get bitmap size
            Dim bmp As Bitmap = New Bitmap(pictureBox1.Image)
            Dim wPrinterPix As Integer, hPrinterPix As Integer, wDispPix As Integer, hDispPix As Integer
            Dim hBmp As IntPtr = bmp.GetHbitmap()
            AxSftPrintPreview1.GetDDBSize(hBmp.ToInt32(), AxSftPrintPreview1.RenderingInfo.hDC, wPrinterPix, hPrinterPix, wDispPix, hDispPix)
            ' make sure entire image fits on remaining page
            If yPrinter + hPrinterPix > outRect.Bottom Then
                DeleteObject(hBmp)
                Exit While
            End If
            ' print bitmap, horizontally centered
            AxSftPrintPreview1.PrintDDB(hBmp.ToInt32(), AxSftPrintPreview1.RenderingInfo.hDC, xPrinter + (outRect.Width - wPrinterPix) / 2, _
                yPrinter, wPrinterPix, hPrinterPix, wDispPix, hDispPix)
            DeleteObject(hBmp)
            yPrinter += hPrinterPix ' next available space
        Else
            ' Print line
            ' get text size
            Dim s As SizeF = g.MeasureString(m_DrawText, m_PrintFont)
            ' make sure text fits on remaining page
            If yPrinter + s.Height > outRect.Bottom Then
                Exit While
            End If

            Dim pt As PointF = New PointF(outRect.Left + (outRect.Width - CInt(s.Width)) / 2, yPrinter)
            g.DrawString(m_DrawText, m_PrintFont, brush, pt)
            yPrinter += CInt(s.Height)
        End If

        Counter = Counter + 1  ' one more item done
    End While
    VisitedPageInfo = Counter ' we left off here
End Sub

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.