Link to home
Start Free TrialLog in
Avatar of foobarr
foobarrFlag for Canada

asked on

Using the Printer Changing the Print Document

I am having trouble figuring out how to print out a page with information that is changing to the printer.

Think of printing out packing slips.  Each Packing Slip has a packing slip # and the contents of each packing slip are different.  
For simplicity purposes I've pasted the basics of my code


In my Print Button Click Event I have

Dim print_Document As PrintDocument = PreparePackingSlipDocument()
print_Document.Print()


Private Function PreparePackingSlipDocument() As PrintDocument
        Dim print_document As New PrintDocument

        AddHandler print_document.PrintPage, AddressOf Print_PackingSlip
        Return print_document

    End Function

Private Sub Print_PackingSlip(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim p As New Pen(Color.Black, 4)
        Dim r As New Rectangle(200, 250, 300, 30)

        e.Graphics.DrawString("HelloWorld", New Font("Verdana", 50, FontStyle.Bold), Brushes.Black, 150, 10)

End Sub

I know in my Print_PackingSlip Sub is where all of my data will need to be setup so it can be displayed/printed to a page.  But the problem I do not understand is how I can change all that information dynamically.  Because each time I read from my database my Packing Slip information will be completely different so I need to change the text of what I want to print out.  

I suppose I could just generate a bunch of variables and then use those variables in the Print_PackingSlip Sub but I was hoping not to do that because there may be a lot of information to print so I might end up wiht a lot of variables used only for printing

These subs are currently in my mainForm class.  I guess I should moveall my printing information to its own separate class, but I could not find any sufficient material to help me move the code to a class so I just left it in the mainForm Class for now.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of foobarr

ASKER

2) Only 1 page at a time but if an order has multiple skids

I wil need to print like 1 of 20, 2 of 20, etc. etc.

3) Customer information, order information, qty on skid, skid x of y, etc.  All of that information can only be taken from querying the db
You can pass that information to class, passing in text, and {x,y} coordinates of where to print these elements.  If you have to print multiple documents, you could either print multiple pages from the same document or create 1 to n instances of the print class.  If you go down the single class instance, then you will need to set the e.HasMorePages property for the PrintPage event argument.

Bob
Avatar of foobarr

ASKER

How would I call the PrintSlips Class from my main program

I would create an instance of PrintSlips then ?
Avatar of foobarr

ASKER

"You can pass that information to class"
I'm guessing you mean the PrintSlips class


I was able to call the PrintSlips class and print from it so disregard that previous comment on howto call that class
Extending the class:

Imports System.Drawing.Printing

Public Class PackingSlipPrinter

    Private WithEvents m_packingSlip As New PrintDocument

    Private Sub Print_PackingSlip(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles m_packingSlip.PrintPage

        ' Get the print area rectangle to draw strings, lines, etc.
        Dim printArea As RectangleF = m_packingSlip.DefaultPageSettings.PrintableArea

        ' Define a pen to draw a thick line
        Dim pen As New Pen(Color.Black, 4)

        ' Define the font.
        Dim font As New Font("Verdana", 50, FontStyle.Bold)

        ' Define a format to center text within a layout rectangle
        Dim format As New StringFormat
        format.Alignment = StringAlignment.Center

        ' Define a layout rectangle for the title
        Dim layoutRect As New RectangleF(printArea.Left, printArea.Top, printArea.Width, font.Height)

        ' Print the title centered on the page.
        e.Graphics.DrawString(Me.Title, font, Brushes.Black, layoutRect, format)

        ' Draw a divider line
        e.Graphics.DrawLine(pen, printArea.Left, font.Height + 2, printArea.Right, font.Height + 2)

        ' Release resources.
        font.Dispose()
        pen.Dispose()

    End Sub

    Private m_title As String = ""
    Public Property Title() As String
        Get
            Return m_title
        End Get
        Set(ByVal value As String)
            m_title = value
        End Set
    End Property

    Public Sub Print()
        m_packingSlip.Print()
    End Sub

End Class

Instancing:
Dim printer As New PackingSlipPrinter
printer.Title = "Packing Slip"
printer.Print()

Bob
Avatar of foobarr

ASKER

One last question where would I go to change the paper size from 8.5 x 11 to 8.5 x 14?

1) I changed the class to this:

Imports System.Drawing.Printing
Imports System.Drawing.Printing.PrinterSettings
Imports System.ComponentModel

Public Class PackingSlipPrinter

  Private WithEvents m_packingSlip As New PrintDocument

  Private m_preview As New PrintPreviewControl

  Public Sub New()
    m_preview.Document = m_packingSlip
  End Sub

  Private Sub Print_PackingSlip(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles m_packingSlip.PrintPage

    ' Get the print area rectangle to draw strings, lines, etc.
    Dim printArea As Rectangle = m_packingSlip.DefaultPageSettings.Bounds

    ' Get the default margins
    Dim margins As Margins = m_packingSlip.DefaultPageSettings.Margins

    ' Define a pen to draw a thick line
    Dim pen As New Pen(Color.Black, 4)

    ' Define the font.
    Dim font As New Font("Verdana", 24, FontStyle.Bold)

    ' Define a format to center text within a layout rectangle
    Dim format As New StringFormat
    format.Alignment = StringAlignment.Center

    ' Define a layout rectangle for the title
    Dim layoutRect As New RectangleF(printArea.Left + margins.Left, printArea.Top + margins.Top, printArea.Width - margins.Left - margins.Right, font.Height)

    ' Print the title centered on the page.
    e.Graphics.DrawString(Me.Title, font, Brushes.Black, layoutRect, format)

    Dim x1 As Integer = printArea.Left + margins.Left
    Dim y1 As Integer = printArea.Top + margins.Top
    Dim x2 As Integer = printArea.Right - margins.Right
    Dim y2 As Integer = y1

    ' Draw title top divider line.
    e.Graphics.DrawLine(pen, x1, y1, x2, y2)

    y1 += font.Height + 2
    y2 = y1

    ' Draw title bottom divider line.
    e.Graphics.DrawLine(pen, x1, y1, x2, y2)

    ' Release resources.
    font.Dispose()
    pen.Dispose()

  End Sub

  Private m_title As String = ""
  <Description("Get/set the title for the print document.")> _
  Public Property Title() As String
    Get
      Return m_title
    End Get
    Set(ByVal value As String)
      m_title = value
    End Set
  End Property

  <Description("Start the document printing process.")> _
  Public Sub Print()
    m_packingSlip.Print()
  End Sub

  <Description("Get/set the size of the print paper.")> _
  Public Property PaperSize() As PaperSize
    Get
      Return m_packingSlip.DefaultPageSettings.PaperSize
    End Get
    Set(ByVal value As PaperSize)
      m_packingSlip.DefaultPageSettings.PaperSize = value
    End Set
  End Property

  <Description("Get the internal PrintDocument instance.")> _
  Public ReadOnly Property Document() As PrintDocument
    Get
      Return m_packingSlip
    End Get
  End Property

  <Description("Get the paper sizes supported.")> _
  Public ReadOnly Property PaperSizes() As PaperSizeCollection
    Get
      Return m_packingSlip.PrinterSettings.PaperSizes
    End Get
  End Property

End Class

2) I created a form with a PrintPreviewControl, and added this code:

    Dim printer As New PackingSlipPrinter
    printer.Title = "Packing Slip"

    ' Check to see if the printer supports Legal paper size.
    For Each paper As PaperSize In printer.PaperSizes
      If paper.Kind = PaperKind.Legal Then
        printer.PaperSize = paper
      End If
    Next

    Me.PrintPreviewControl1.Document = printer.Document

3) This wrapper class is handy to use since it will bring together all the different parts of the PrintDocument into one place, instead of trying to figure out that one setting is under the PrinterSettings, while another is under DefaultPageSettings.

4) What is your .NET version?

Bob
Avatar of foobarr

ASKER

I dont want to have teh option to open a print preview control.  I am running this off a touch screen monitor so I just want to click a print button I've created on my form and spit out everything i want.  

However, I can definitely use the rest of this for any of the printing I need to do in the office side of the software
 

I am running 2005 vb.net professional I believe
I only showed you how to use the PrintPreviewControl so that you didn't have to print out the document to see how it looks.

Bob
Avatar of foobarr

ASKER

ahh okay

well I appreciate all teh help!