Why PrintDocument Margins Wrong on Printed Paper ?
Hi. I try to Print Address Labels.
e.Graphics.PageUnit = GraphicsUnit.Millimeter
Label Margins in PrintDocument are :
Top Margin = 12.9 mm
Left Margin = 4.65 mm
Labels Gap = 2.5 mm
Label Height = 33.9 mm
Label Width = 99.1 mm
BUT when I Print on Paper Result :
Left Margin = 4.5 mm (on PrintDocument 4.65 mm)
Top Margin : 12.00 mm (on PrintDocument 12.9 mm)
Label Height = 33.00 mm (on PrintDocument 33.9 mm)
Label Width = 99.00 mm
Lagel Gap = 2.0 mm (on PrintDocument 2.5 mm)
all margins are Decimal... and I am not rounding any margin value. Used Math.Truncate(number) to disable Round.
Why all Margin Values are get lower rounded why I can not get the Real decimal Values after Printed on Paper ?
Code:
Private Sub PrintDocument6_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument6.PrintPage
e.Graphics.PageUnit = GraphicsUnit.Millimeter
PrintDocument6.DefaultPageSettings.PaperSize = New System.Drawing.Printing.PaperSize("Paper Size Name", 210, 297)
PrintDocument6.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
Dim style As FontStyle = FontStyle.Regular
Dim fonts As Font = New Font(New FontFamily("Monotype Corsiva"), 12, style)
Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black, 0.01F)
Dim grpX As Graphics = e.Graphics
Dim wdth As Decimal
Dim hght As Decimal
Dim horzGap As Decimal
Dim HorLabel As Decimal
Dim VerLabel As Decimal
hght = Math.Truncate(((33.9) * 100) / 100)
VerLabel = 8
wdth = Math.Truncate(((99.1) * 100) / 100)
HorLabel = 2
horzGap = Math.Truncate(((2.5) * 100) / 100)
For B = 12.9 To (hght * VerLabel) Step (hght)
For A = 4.65 To (wdth * HorLabel) Step (wdth + horzGap)
Dim rectX As Rectangle = New Rectangle((A), (B), (wdth), (hght))
grpX.DrawRectangle(pen, rectX)
Next
Next
End Sub
Re: Why PrintDocument Margins Wrong on Printed Paper ?
Quote:
Originally Posted by
Nicomendox
I am not rounding any margin value.
Oh yes you are.
Quote:
Originally Posted by
Nicomendox
Used Math.Truncate(number) to disable Round.
But you used it wrong, so it is actually rounding toward zero. Example:
vb.net Code:
hght = Math.Truncate(((33.9) * 100) / 100)
If we exapnd that out it becomes this:
vb.net Code:
Dim a = 33.9
Dim b = (a) * 100 'a = 339.0
Dim c = (b) / 100 'b = 33.9
hght = Math.Truncate(c) 'hght = 33.0
What you actually should be doing is this:
vb.net Code:
Dim a = 33.9
Dim b = (a) * 100 'a = 339.0
Dim c = Math.Truncate(b) 'c = 339.0
hght = (c) / 100 'hght = 33.9
If you hadn't included so many useless parentheses then you wouldn't have missed the wood for the trees:
vb.net Code:
hght = Math.Truncate(33.9 * 100) / 100
You should have been able to work this out for yourself because you should have done exactly what I did , i.e. expanded the single lines that performed multiple operations into multiple lines that performed a single operation each. Had you done that and actually debugged the code, the issue would have been obvious.
Of course, if you're using literal values then why is any of that being used at all? 33.9 is 33.9, whether it's a literal or the result of a calculation. What do you think that that code is accomplishing?
Just use a Decimal literal in the first place. You also must have Option Strict Off or else assigning a Double to a Decimal variable, as you are doing, would not be allowed. Turn Option Strict On in the project and the IDE and use Decimal literals where you need Decimal values.