Results 1 to 2 of 2

Thread: Why PrintDocument Margins Wrong on Printed Paper ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2018
    Posts
    77

    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
    Last edited by Nicomendox; Apr 18th, 2020 at 07:38 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why PrintDocument Margins Wrong on Printed Paper ?

    Quote Originally Posted by Nicomendox View Post
    I am not rounding any margin value.
    Oh yes you are.
    Quote Originally Posted by Nicomendox View Post
    Used Math.Truncate(number) to disable Round.
    But you used it wrong, so it is actually rounding toward zero. Example:
    vb.net Code:
    1. hght = Math.Truncate(((33.9) * 100) / 100)
    If we exapnd that out it becomes this:
    vb.net Code:
    1. Dim a = 33.9
    2. Dim b = (a) * 100 'a = 339.0
    3. Dim c = (b) / 100 'b = 33.9
    4.  
    5. hght = Math.Truncate(c) 'hght = 33.0
    What you actually should be doing is this:
    vb.net Code:
    1. Dim a = 33.9
    2. Dim b = (a) * 100 'a = 339.0
    3. Dim c = Math.Truncate(b) 'c = 339.0
    4.  
    5. 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:
    1. 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?
    vb.net Code:
    1. hght = 33.9D
    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.
    Last edited by jmcilhinney; Apr 19th, 2020 at 12:03 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width