Click here to Skip to main content
15,886,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

What I have tried:

VB
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
Posted
Updated 21-Apr-20 5:44am
Comments
Peter_in_2780 18-Apr-20 22:18pm    
Have a look at https://docs.microsoft.com/en-us/dotnet/api/system.drawing.graphicsunit?view=netframework-4.8 and related pages. You probably need to work in RectangleF's rather than Rectangles, Round'ing or Truncat'ing at the last moment.
Nicomendox 18-Apr-20 23:39pm    
Dim rectX As Rectangle = New Rectangle(Math.Truncate(A), Math.Truncate(B), Math.Truncate(wdth), Math.Truncate(hght))
Dim rectf As RectangleF = RectangleF.op_Implicit(rectX)
Dim rect2 As Rectangle = Rectangle.Truncate(rectf)

'grpX.DrawRectangles(pen, New RectangleF() {rectf}) '(pen, rectf)
e.Graphics.DrawRectangle(pen2, rect2)
grpX.DrawRectangles(pen, New RectangleF() {rectf})

Unfortunately same result. values are always round dawn.

1 solution

The Truncate method[^] throws away anything after the decimal point. Your code is equivalent to:
VB.NET
hght = 33.0
wdth = 99.0
horzGap = 2.0
Start by removing the Truncate calls and assigning the values you actually want to use.

NB: Your question says the problem is with the page margins, but the code you've shown sets all of the margins to 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900