printing problem

RonR

Well-known member
Joined
Nov 23, 2007
Messages
82
Programming Experience
5-10
this is a line of code to send to the printer. I want < Staff List > to be printed at the same position even when the client name has a different length. I would also like < Staff List > to be bold but not the client name.


I have experimented with Write & Print but no luck.


MyPrinter.Print(TAB(7), " Client Name> ", TAB(27), Trim$((rsIncidents.Fields("ClientName").Value)), TAB(110), " < Staff List >")
 
PrintDocument (System.Drawing.Printing namespace) is recommended instead of the VB6 Printer.
 
I do not have documents to print, just data from Access .mdb.


do you have to create a text document to print or can you just print directly from a database field??
 
PrintDocument doesn't print Word documents, the core component is the Graphics object that draws to the printer. It is the same, and is used the same, as drawing images or any custom painting that is done with VB.Net. For text you use the Graphics.DrawString method, for images DrawImage etc.
 
PrintDocument only prints full pages, but you can print only one line of text on the page if you wish.

PrintPreview control/dialog components is both assigned a PrintDocument instance. The dialog is a regular form to Show/Showdialog.
 
err... say what?
 
ok, how do i make the document to print?

if I am going to print a document I must have to make a document somehow.
 
I think you confuse the PrintDocument class with document files on file system, they have nothing to do with each other. Start by reading from the documentation for PrintDocument class and Graphics class, complement with printing/graphics tutorials if you need. I'm sure if you read a basic VB.Net book it also have a printing chapter that talks about the same.
 
So, I print to a text document and this is used to print to paper and to do a print preview?

Once you have your PrintDocument stuff set up, use the PrintPreviewDialog to show a print preview

The PrintPreviewDialog has a "Document" property in which you set it to your document.
 
very good info. thanks!


but, If I am wanting to use the "printdocument" feature, does it send one line at a time to the printer or is it done some other way? how can a print preview work if I have not sent data to print somewhere in the first place?

I would like a "block diagram" on how the printing works.
 
Here's a great example found here: http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

VB.NET:
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows.Forms

Public Class PrintingExample
    Inherits System.Windows.Forms.Form
    Private components As System.ComponentModel.Container
    Private printButton As System.Windows.Forms.Button
    Private printFont As Font
    Private streamToPrint As StreamReader

    Public Sub New()
        ' The Windows Forms Designer requires the following call.
        InitializeComponent()
    End Sub    

    ' The Click event is raised when the user clicks the Print button.
    Private Sub printButton_Click(sender As Object, e As EventArgs)
        Try
            streamToPrint = New StreamReader("C:\My Documents\MyFile.txt")
            Try
                printFont = New Font("Arial", 10)
                Dim pd As New PrintDocument()
                AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
                pd.Print()
            Finally
                streamToPrint.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub    

    ' The PrintPage event is raised for each page to be printed.
    Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing

        ' Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

        ' Print each line of the file.
        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If      
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        ' If more lines exist, print another page.
        If (line IsNot Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub


    ' The Windows Forms Designer requires the following procedure.
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.printButton = New System.Windows.Forms.Button()

        Me.ClientSize = New System.Drawing.Size(504, 381)
        Me.Text = "Print Example"

        printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        printButton.Location = New System.Drawing.Point(32, 110)
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        printButton.TabIndex = 0
        printButton.Text = "Print the file."
        printButton.Size = New System.Drawing.Size(136, 40)
        AddHandler printButton.Click, AddressOf printButton_Click

        Me.Controls.Add(printButton)
    End Sub 

    ' This is the main entry point for the application.    
    Public Shared Sub Main()
        Application.Run(New PrintingExample())
    End Sub

End Class
 
Back
Top