Results 1 to 3 of 3

Thread: [RESOLVED] How to define Centimeters unit in printdocument

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    283

    Resolved [RESOLVED] How to define Centimeters unit in printdocument

    Hello everyone,

    I am designing a cheque printing software, different banks have different cheque dimensions, so I created a form and asked for each field location both x & y in pixels, my client set 1st bank cheque accurately by placing x & y values, but when he open the same form on another computer whose computer resolution is different the whole setting lost. basically setting of pixels depend on screen resolution, if 2 computers have different resolutions the placing of fields will be different.

    Here is my code, please provide me some solution to give x & y values in cms not in pixels.

    Thank you
    Ladak

    Code:
     Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
           
    
            '' Number Format
            Dim Numformat As StringFormat = New StringFormat
            Numformat.Alignment = StringAlignment.Near
            Numformat.LineAlignment = StringAlignment.Center
            Numformat.FormatFlags = StringFormatFlags.NoWrap
    
            ' Text Format
            Dim TextFormat As New System.Drawing.StringFormat
            TextFormat.LineAlignment = StringAlignment.Near  ' .Center ' .Near    ' center horizontally
            TextFormat.Alignment = StringAlignment.Near   ' center vertically
            TextFormat.Trimming = StringTrimming.Word
            'TextFormat.FormatFlags = StringFormatFlags.NoWrap
    
    
            Dim SdsFont As Font = New Font("Tahoma", 8, FontStyle.Regular)
    
            ' Counter Side
    
            Dim rectF1 As New RectangleF
    
            If ChkPrintCounter.Checked = True Then
    
                rectF1 = New RectangleF(Val(txtCounterDateX.Text), Val(txtCounterDateY.Text), 100, 50)
                e.Graphics.DrawString(lblChequeDate.Text, SdsFont, Brushes.Black, rectF1, TextFormat)
    
                rectF1 = New RectangleF(Val(txtCounterPayX.Text), Val(txtCounterPayY.Text), 100, 50)
                e.Graphics.DrawString(txtPayTo.Text, SdsFont, Brushes.Black, rectF1, TextFormat)
    
    
                rectF1 = New RectangleF(Val(txtCounterAmountX.Text), Val(txtCounterAmountY.Text), 100, 50)
                e.Graphics.DrawString(lblChequeAmount.Text, SdsFont, Brushes.Black, rectF1, Numformat)
    
            End If
    
            '
            ' End of Counter
    
    
    
            ' Main Side
            rectF1 = New RectangleF(Val(txtMainDateX.Text), Val(txtMainDateY.Text), 100, 50)
            e.Graphics.DrawString(lblChequeDate.Text, SdsFont, Brushes.Black, rectF1, TextFormat)
    
    
            rectF1 = New RectangleF(Val(txtMainPayX.Text), Val(txtMainPayY.Text), 500, 50)
            e.Graphics.DrawString(txtPayTo.Text, SdsFont, Brushes.Black, rectF1, TextFormat)
    
    
            rectF1 = New RectangleF(Val(txtMainRupeesX.Text), Val(txtMainRupeesY.Text), 100, 50)
            e.Graphics.DrawString("=" & lblChequeAmount.Text, SdsFont, Brushes.Black, rectF1, Numformat)
    
    
            rectF1 = New RectangleF(Val(txtMainRupeesInWordsX.Text), Val(txtMainRupeesInWordsY.Text), 375, 50)
            e.Graphics.DrawString(lblChequeAmountInWords.Text, SdsFont, Brushes.Black, rectF1, TextFormat)
    
    
            ' End of Main Side
    
            Dim X1 As Integer = Val(txtCrossX.Text)
            Dim Y1 As Integer = Val(txtCrossY.Text)
    
            If ChkCrossCheque.Checked = True Then
                'e.Graphics.DrawImage(PictureBox1.Image, e.MarginBounds.Left, e.MarginBounds.Top, 100, 25)
                e.Graphics.DrawImage(PictureBox1.Image, X1, Y1, 100, 25)
            End If
    
    
    
    
    
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,317

    Re: How to define Centimeters unit in printdocument

    I don't have time to setup a working example for you, but I can detail the principles for you:
    1. Set the Graphic's PageUnit property to Millimeter by using: e.Graphics.PageUnit = GraphicsUnit.Millimeter
    2. Convert your cm values to mm by multiplying the value by 10
    3. Use the same DrawString methods that you're using now, only with the mm values from step 2


    It's worth mentioning that you should either replace your TextBox controls with a NumericUpDown control -or- validate that the text is a valid number using the TryParse method before you start any of this.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    283

    Re: How to define Centimeters unit in printdocument

    Hello dday9

    Many thanks for your reply, after getting your tip, I found the solution and added following code.
    My issue is now resolved.

    Code:
    Dim g As Graphics = e.Graphics
    g.PageUnit = GraphicsUnit.Millimeter
    Thank you
    Ladak

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