Print Problem
Hey all,
I’m new to Visual Basic dot net. I’m trying to understand printing a little better. Here’s the problem. If I create a PrintDocument and use a PrintDialog, then My Print out is to the page size specified in the PrintDialog settings. But if I try to manually set a page size and print without using the PrintDialog then my page settings are not used
The following is some code that I am using to try to understand this.
Private PrnFont As New Font("Arial", 10
Private PrnDoc As PrintDocumen
Private Sub butPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrintDialog.Clic
Tr
PrnDoc = New PrintDocument(
PrnDialog.Document = PrnDo
If PrnDialog.ShowDialog() = DialogResult.OK The
DoPrint(
End I
Catch Excep As Exceptio
MessageBox.Show(Excep.Message, "Printing Error", MessageBoxButtons.OK, MessageBoxIcon.Error
End Tr
End Su
Private Sub butPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butPrint.Clic
Tr
PrnDoc = New PrintDocument(
Dim PgSize As New PaperSize("Custom", 450, 350
PrnDoc.PrinterSettings.DefaultPageSettings.PaperSi ze = PgSiz
DoPrint(
Catch Excep As Exceptio
MessageBox.Show(Excep.Message, "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error
End Tr
End Su
Private Sub DoPrint(
Tr
AddHandler PrnDoc.PrintPage, AddressOf Me.PrnDoc_PrintPag
PrnDoc.Print(
Catch Excep As Exceptio
MessageBox.Show(Excep.Message, "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error
End Tr
End Su
Private Sub PrnDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs
Dim nLinesPerPage As Single =
Dim YPos As Single =
Dim Count As Integer =
Dim LeftMargin As Single = e.MarginBounds.Lef
Dim TopMargin As Single = e.MarginBounds.To
Dim szLine As String = Nothin
' Calculate the number of lines per page
nLinesPerPage = e.MarginBounds.Height / PrnFont.GetHeight(e.Graphics
'Print each line
While nLinesPerPage >
szLine = "This is line No: " & (Count + 1
YPos = TopMargin + Count * PrnFont.GetHeight(e.Graphics
e.Graphics.DrawString(szLine, PrnFont, Brushes.Black, LeftMargin, YPos, New StringFormat()
Count +=
nLinesPerPage -=
End Whil
e.HasMorePages = Fals
End Su