jamesrhaynes
Programmer
To practice using printer controls, I have employed a very simple example from "Mastering Visual Basic.Net" by Evangelous Petroutsos. This sample uses the printdialog, print preview, and page setup and just prints a rectangle around the page at the margins. Using one inch margins all around, the margins appear perfect in print preview but on actual paper are quite different (Its all shifted right by about 1/4 inch and down 1/8 inch in potrait and more than 1/2 inch to the right in planform.
I tried several different printers with the same result. Also, I can use the same printer and draw a rectangle from Microsoft word and the margins are perfect. How can Microsoft word control the margins accurately, but I can't do it from VB.net?
The sample code (PrintPage event) is shown below:
Private Sub MainPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles MainPrintDocument.PrintPage
Dim LM, TM, BM, RM, PageWidth, Pageheight As Integer
Dim PrintWidth, PrintHeight As Integer
Dim itsLandscape As Boolean
With MainPrintDocument.DefaultPageSettings
LM = .Margins.Left
TM = .Margins.Top
BM = .Margins.Bottom
RM = .Margins.Right
Pageheight = .PaperSize.Height
PageWidth = .PaperSize.Width
itsLandscape = .Landscape
If itsLandscape Then
PrintWidth = .PaperSize.Height - TM - BM
PrintHeight = .PaperSize.Width - LM - RM
Else
PrintHeight = .PaperSize.Height - TM - BM
PrintWidth = .PaperSize.Width - LM - RM
End If
End With
Dim R As New Rectangle
R.X = LM
R.Y = TM
R.Width = PrintWidth
R.Height = PrintHeight
e.Graphics.DrawRectangle(Pens.Black, R)
End Sub
I tried several different printers with the same result. Also, I can use the same printer and draw a rectangle from Microsoft word and the margins are perfect. How can Microsoft word control the margins accurately, but I can't do it from VB.net?
The sample code (PrintPage event) is shown below:
Private Sub MainPrintDocument_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles MainPrintDocument.PrintPage
Dim LM, TM, BM, RM, PageWidth, Pageheight As Integer
Dim PrintWidth, PrintHeight As Integer
Dim itsLandscape As Boolean
With MainPrintDocument.DefaultPageSettings
LM = .Margins.Left
TM = .Margins.Top
BM = .Margins.Bottom
RM = .Margins.Right
Pageheight = .PaperSize.Height
PageWidth = .PaperSize.Width
itsLandscape = .Landscape
If itsLandscape Then
PrintWidth = .PaperSize.Height - TM - BM
PrintHeight = .PaperSize.Width - LM - RM
Else
PrintHeight = .PaperSize.Height - TM - BM
PrintWidth = .PaperSize.Width - LM - RM
End If
End With
Dim R As New Rectangle
R.X = LM
R.Y = TM
R.Width = PrintWidth
R.Height = PrintHeight
e.Graphics.DrawRectangle(Pens.Black, R)
End Sub