?
Solved

printing form information

Posted on 2004-11-19
1
Medium Priority
?
314 Views
Last Modified: 2010-08-05
Does anyone know how to  do position printing in vb.net
not printing the whole form.  I have to print on a pre formatted form and place the information on their allocated boxes.
0
Comment
Question by:genesisproject
1 Comment
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 12626960
You can do it by controlling X and Y coordinates. In the code below check the place with variables initialization
' Initialize local variables that contain the bounds of the printing
            ' area rectangle.
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right


Here is the whole example


Option Strict On
Imports System.Drawing.Printing

Public Class frmMain
    Inherits System.Windows.Forms.Form

    ' It's important that all the event procedures work with the same PrintDocument
    ' object.
    Private WithEvents pdoc As New PrintDocument()

#Region " Windows Form Designer generated code "
#End Region

    Dim sFontname As String = "Times New Roman"
    Dim iFontSize As Short = 12

    ' The PrintDialog allows the user to select the printer that they want to print
    ' to, as well as other printing options.
    Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click
        Dim dialog As New PrintDialog()
        dialog.Document = pdoc

        If dialog.ShowDialog = DialogResult.OK Then
            pdoc.Print()
        End If
    End Sub

    ' The PrintPreviewDialog is associated with the PrintDocument as the preview is
    ' rendered, the PrintPage event is triggered. This event is passed a graphics
    ' context where it "draws" the page.
    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        Dim ppd As New PrintPreviewDialog()
        Try
            ppd.Document = pdoc
            ppd.ShowDialog()
        Catch exp As Exception
            MessageBox.Show("An error occurred while trying to load the " & _
                "document for Print Preview. Make sure you currently have " & _
                "access to a printer. A printer must be connected and " & _
                "accessible for Print Preview to work.", Me.Text, _
                 MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    ' Page setup lets you specify things like the paper size, portrait,
    ' landscape, etc.
    Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
        Dim psd As New PageSetupDialog()
        With psd
            .Document = pdoc
            .PageSettings = pdoc.DefaultPageSettings
        End With

        If psd.ShowDialog = DialogResult.OK Then
            pdoc.DefaultPageSettings = psd.PageSettings
        End If
    End Sub

    ' Handles the Form's Load event, initializing the TextBox with some text
    ' for printing.
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtDocument.Text = _
        "==================================================" & vbCrLf & _
        "     Col1     |     Col2     |     Col3     |" & vbCrLf & _
        "==================================================" & vbCrLf & _
        "      1       |      2       |     Iouri    |" & vbCrLf & _
        "      1       |      2       |     Iouri    |" & vbCrLf & _
        "      1       |      2       |     Iouri    |" & vbCrLf & _
        "      1       |      2       |     Iouri    |"

    End Sub

    ' PrintPage is the foundational printing event. This event gets fired for every
    ' page that will be printed. You could also handle the BeginPrint and EndPrint
    ' events for more control.
    Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
        ' Declare a variable to hold the position of the last printed char. Declare
        ' as static so that subsequent PrintPage events can reference it.
        Static intCurrentChar As Int32
        ' Initialize the font to be used for printing.(see on the top)
        Dim font As New font(sFontname, iFontSize)

        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With pdoc.DefaultPageSettings
            ' Initialize local variables that contain the bounds of the printing
            ' area rectangle.
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

            ' Initialize local variables to hold margin values that will serve
            ' as the X and Y coordinates for the upper left corner of the printing
            ' area rectangle.
            marginLeft = .Margins.Left ' X coordinate
            marginTop = .Margins.Top ' Y coordinate
        End With

        ' If the user selected Landscape mode, swap the printing area height
        ' and width.
        If pdoc.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = intPrintAreaHeight
            intPrintAreaHeight = intPrintAreaWidth
            intPrintAreaWidth = intTemp
        End If

        ' Calculate the total number of lines in the document based on the height of
        ' the printing area and the height of the font.
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
        ' Initialize the rectangle structure that defines the printing area.
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)

        ' Instantiate the StringFormat class, which encapsulates text layout
        ' information (such as alignment and line spacing), display manipulations
        ' (such as ellipsis insertion and national digit substitution) and OpenType
        ' features. Use of StringFormat causes MeasureString and DrawString to use
        ' only an integer number of lines when printing each page, ignoring partial
        ' lines that would otherwise likely be printed if the number of lines per
        ' page do not divide up cleanly for each page (which is usually the case).
        ' See further discussion in the SDK documentation about StringFormatFlags.
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
        ' Call MeasureString to determine the number of characters that will fit in
        ' the printing area rectangle. The CharFitted Int32 is passed ByRef and used
        ' later when calculating intCurrentChar and thus HasMorePages. LinesFilled
        ' is not needed for this sample but must be passed when passing CharsFitted.
        ' Mid is used to pass the segment of remaining text left off from the
        ' previous page of printing (recall that intCurrentChar was declared as
        ' static.
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

        ' Print the text to the page.
        e.Graphics.DrawString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
            Brushes.Black, rectPrintingArea, fmt)

        ' Advance the current char to the last char printed on this page. As
        ' intCurrentChar is a static variable, its value can be used for the next
        ' page to be printed. It is advanced by 1 and passed to Mid() to print the
        ' next page (see above in MeasureString()).
        intCurrentChar += intCharsFitted

        ' HasMorePages tells the printing module whether another PrintPage event
        ' should be fired.
        If intCurrentChar < txtDocument.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            ' You must explicitly reset intCurrentChar as it is static.
            intCurrentChar = 0
        End If
    End Sub
End Class
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question