3

I am using following code in VB.NET 4.0 to print a cheque.

In printpreview dialog, print is shown correctly but when printed actually, print starts from bottom of cheque.

I want print to start from start point of cheque.

Cheque is inserted in middle of printer tray vertically.

Private print_document As PrintDocument
Private FIRST_TO_PRINT As String = "CHEQUE"
Private fontsize As Integer = 12
Private _ID As Integer = 0
Private CHEQUE_DATE As Date
Private CLIENT_NAME As String
Private CLIENT_NAME_ON_CHEQUE As String
Private AMOUNT As Decimal
Private AMOUNT_IN_WORDS As String

Public Sub New(ID As Integer)
    _ID = ID
End Sub

Public Function PreparePrintDocument() As PrintDocument
    ' Make the PrintDocument object.
    print_document = New PrintDocument




    print_document.DefaultPageSettings.PaperSize = New PaperSize("Custom", 336, 768)

    'print_document.OriginAtMargins = True
    'print_document.DefaultPageSettings.Margins.Left = 200
    'print_document.DefaultPageSettings.Margins.Right = 10
    'print_document.DefaultPageSettings.Margins.Bottom = 10
    'print_document.DefaultPageSettings.Margins.Top = 200
    AddHandler print_document.BeginPrint, AddressOf Print_BeginPrint
    ' Install the PrintPage event handler.
    AddHandler print_document.PrintPage, AddressOf Print_PrintPage

    ' Return the object.
    Return print_document
End Function
Protected Sub Print_BeginPrint(sender As Object, e As PrintEventArgs)

    GetVoucherDetails()
End Sub

Private Sub GetVoucherDetails()
    Dim objVOUCHER_BLL As VOUCHER_BLL
    Try
        objVOUCHER_BLL = New VOUCHER_BLL
        Dim temps As VOUCHERDataTable = objVOUCHER_BLL.GetVoucherByID(_ID)
        If temps.Count > 0 Then
            Dim temp As VOUCHERRow = temps(0)
            CHEQUE_DATE = temp.CHEQUE_DATE
            CLIENT_NAME = temp.CLIENT_NAME
            CLIENT_NAME_ON_CHEQUE = temp.CLIENT_NAME_ON_CHEQUE
            AMOUNT = temp.AMOUNT
            AMOUNT_IN_WORDS = numToWords.AmtInWord(temp.AMOUNT)
        End If
    Catch ex As Exception
    Finally
        objVOUCHER_BLL = Nothing
    End Try
End Sub

' Print the next page.
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
    As System.Drawing.Printing.PrintPageEventArgs)


    Dim our_brush As Brush = Brushes.Black
    Dim font As New Font("Verdana", fontsize)
    If FIRST_TO_PRINT = "CHEQUE" Then


        e.PageSettings.PaperSize = New PaperSize("Custom", 336, 768)
        e.Graphics.ResetTransform()
        e.Graphics.TranslateTransform(306, 580)
        e.Graphics.RotateTransform(90)
        e.Graphics.DrawString(CHEQUE_DATE.ToString("dd MM yyyy"), font, our_brush, New Point(0, 0))

        e.Graphics.ResetTransform()
        e.Graphics.TranslateTransform(262, 20)
        e.Graphics.RotateTransform(90)
        e.Graphics.DrawString(CLIENT_NAME_ON_CHEQUE, font, our_brush, New Point(70, 0))

        e.Graphics.ResetTransform()
        e.Graphics.TranslateTransform(232, 140)
        e.Graphics.RotateTransform(90)
        e.Graphics.DrawString(AMOUNT_IN_WORDS.Replace("Rupees ", String.Empty).ToUpper, font, our_brush, New Point(0, 0))

        e.Graphics.ResetTransform()
        e.Graphics.TranslateTransform(206, 600)
        e.Graphics.RotateTransform(90)
        e.Graphics.DrawString(AMOUNT.ToString("F2") & " /-", font, our_brush, New Point(0, 0))

        '    e.HasMorePages = True
        '    e.Graphics.ResetTransform()
        '    FIRST_TO_PRINT = "VOUCHER"
        'Else
        '    e.PageSettings.PaperSize = New PaperSize("Custom", 612, 792)

        '    e.Graphics.DrawString("VOUCHER", font, our_brush, New Point(200, 200))
        '    FIRST_TO_PRINT = "CHEQUE"
        e.Graphics.ResetTransform()
        e.HasMorePages = False
    End If

End Sub

PRINT PREVIEW OF CHEQUE

GSerg
  • 76,472
  • 17
  • 159
  • 346
Hemal
  • 3,682
  • 1
  • 23
  • 54
  • I wouldn't set the `PaperSize` in `Print_PrintPage`, other than that, I don't see anything logically wrong here than might cause this behavior. Usually when I experience something like this is because I've forgotten to reset some variables in `BeginPrint` or `EndPrint` – Keith Mifsud Oct 28 '16 at 07:17
  • I have set `PaperSize` in `Print_PrintPage` because I want first print of Cheque to be of different size from second print of voucher which is of A4 papersize. Two different papersize will be printed in single document. – Hemal Oct 28 '16 at 07:26
  • I see, can the issue be from the printpreview declaration it self? As I said, the code presented here doesn't seem wrong. instead of printing through the print preview form, try calling `PreparePrintDocument().Print()` and see if the issue is the same – Keith Mifsud Oct 28 '16 at 07:49
  • 1
    The paper is just not located inside the printer where the printer driver expects it to be. It normally expect the left side of the paper to be aligned with the left side of the feeder tray. So the print-out is shifted. If you can't relocate the paper then you may need a tweak parameter in your code so you can call e.Graphics.TranslateTransform(). You'll also have to adjust the paper size btw to prevent clipping. – Hans Passant Oct 28 '16 at 08:44
  • @Hemal, Did you make it work? – Palanikumar Sep 10 '20 at 11:12

0 Answers0