none
Strange thing with PrintDocument's margins and Receipt printer. Need help. RRS feed

  • Question

  • Hi,

    as title says I have problems trying to print a receipt with a receipt printer.

    Here is a simple test code showing what I'm doing.

    Need just two declarations :

    Private WithEvents mPD As New Printing.PrintDocument
    Private mFont As New Font(FontFamily.GenericSansSerif, 14, FontStyle.Regular)

    These are the PrintDocument's Events :

        Private Sub mPD_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles mPD.BeginPrint
    
    
    
        End Sub
    
        Private Sub mPD_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPD.PrintPage
    
            With e.Graphics
    
                .InterpolationMode = Drawing2D.InterpolationMode.High
                .SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    
                .DrawString("A", mFont, Brushes.Black, 0, 5)
                .DrawString("AB", mFont, Brushes.Black, 0, 25)
                .DrawString("ABC", mFont, Brushes.Black, 0, 45)
                .DrawString("ABCD", mFont, Brushes.Black, 0, 65)
    
            End With
    
            e.HasMorePages = False
    
        End Sub

    And this is the job :

            With mPD
                .PrinterSettings.PrinterName = "CUSTOM TG2480-H"
                .DefaultPageSettings.PaperSize = New Printing.PaperSize("TG2480-H", MMToHP(80),
                                                                                    MMToHP(80))
                .DefaultPageSettings.Margins = New System.Drawing.Printing.Margins(0, 0, 0, 0)
                .DefaultPageSettings.PrinterResolution.X = 204
                .DefaultPageSettings.PrinterResolution.Y = 204
            End With
    
            'Preview
            Dim PPD As New PrintPreviewDialog
            PPD.Document = mPD
            PPD.ShowDialog()


    MMToHP is a simple Function to get hundredths of an inch from millimeters :

        Public Function MMToHP(ByVal pMM As Integer) As Integer 'HP : hundredth of an inch
    
            Return Convert.ToInt32(pMM * 100 / 25.4)
    
        End Function


    The Problem :

    If I print the same thing, building manually the same thing in MS Word, with all margins set to 0, the printer prints exactly as expected, starting from the very beginning of the paper. No hardware margin is set on printer's driver and it can't be set anywhere.

    If I print from my code, even if PrintDocument's DefaultPageSettings.Margins is set to (0, 0, 0, 0), the printer can't start from the paper's edge, but always put a 8 millimeters left margin.

    Maybe something wrong with my PrintDocument's settings ? Something I miss ?

    Thanks to anyone able to help.

    Thursday, November 2, 2017 3:21 PM

Answers

  • Thank you,

    your explanation is correct and useful, BUT how you can explain the same printer is printing the same receipt designed in MS Word without any left margin ?

    If the printer's head can't print at the very beginning of paper, it shouldn't do so with ANY software, while with MS Word it does.

    And if I print the configuration sheet, the printer starts printing from millimeter 0 on paper, and not from millimeter 9 as it happens with PrintDocument.


    Yes. Seems logical. I don't know how word does it. How do you know? Lets see the receipt? How do you know there is not 2mm in there at the edge?

    Plus, if the printer driver has a feature that is not supported by print doc you have to set it other ways. Is there a setting for it somewhere in the driver? How do you know? Maybe word uses that.

    Put the nonprintable area code in there and see what the values are? Tell me what you get for the printable area rectangle using the code I show please.

    Seems I've found the trick.

    It's not matter of PrintDocument's settings.

    It's just a stupid thing in the printer's driver : 

    There's a combobox "Format" with many options.
    By default the printer was on "PTP", and I put it on "Roll".

    Now on Roll, the printer starts from the paper's beginning, even with my code.

    Don't ask me what "PTP" or "Roll" should mean. I'm not a printer expert.

    Friday, November 3, 2017 3:11 PM

All replies

  • Many printers will not print to the edge of the paper but have a non printable area. You need to check your exact device to see what it supports.

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.printablearea(v=vs.110).aspx

    Friday, November 3, 2017 2:47 AM
  • Hi MarcoGG,

    Can you preview your print document and see if this problem will occur. Normally the result of the preview is the result of your printer's printing, and i think your code have no problem, you can see this :

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.margins.left(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 3, 2017 7:14 AM
    Moderator
  • Many printers will not print to the edge of the paper but have a non printable area. You need to check your exact device to see what it supports.

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.printablearea(v=vs.110).aspx

    Thanks.

    But, as I wrote in my first post :

    "If I print the same thing, building manually the same thing in MS Word, with all margins set to 0, the printer prints exactly as expected, starting from the very beginning of the paper. "

    So the printer actually can print on the entire printable area of the paper, without margins.

    Friday, November 3, 2017 7:44 AM
  • Hi MarcoGG,

    Can you preview your print document and see if this problem will occur. Normally the result of the preview is the result of your printer's printing, and i think your code have no problem, you can see this :

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.margins.left(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks.

    In fact in my code I open first a PrintPreviewDialog Form, and in preview the Document is just what I want and no margin is there. 
    The left margin is put only when printed on paper.

    Friday, November 3, 2017 7:47 AM
  • Here it shows the printable area as 76mm:

    PS Sorry I had the wrong link this is correct (see image below):

    http://manualzz.com/doc/7294717/tg2480h---support-technique-aures

    You can get the non printable area and then translate the graphic coordinate to the edge of the paper. Then if your graphics uses 0,0 it will be at the edge. However it will not print over the non-printable area if there is one.

    Dim paRectF As RectangleF
    paRectF = PrintDocument1.DefaultPageSettings.PrintableArea()
    e.Graphics.TranslateTransform(-paRectF.X, -paRectF.Y)

    I guess there are no settings for this in the printer driver setup?

    PS




    Friday, November 3, 2017 12:10 PM
  • PS The graphics origin is at the edge of the printable area (by default). So if you have non printable area then 0,0 is at the edge of the printable area which is not the same as 0,0 for margins. You can set the graphics origin to the margins with OriginAtMargins:

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.originatmargins%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Which puts 0,0 at the edge of paper when margins = 0 as your code has. However it will not print over the non-printable area.

    PS Print preview dialog does not use the specified printer non printable area so you will not see this in print preview.

    That is your problem. The graphics coordinates are not the same as the margin coordinates by default. Graphics 0,0 is at printable area 0,0 by default. Margins 0,0 is the paper edge or size and are not the same when the printer has non-printable area. Your printer has at lest 4mm somewhere that is not printable.

    I don't know how Word deals with it. How do you know it does? It could be off by 2mm on each side of the paper.


    PS Lots of discussion in this thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/f5aad166-60ca-40fa-9c0c-b3be3f459999/how-to-print-an-image-in-the-center-of-paper?forum=vbgeneral


    Friday, November 3, 2017 12:27 PM
  • PS The graphics origin is at the edge of the printable area (by default). So if you have non printable area then 0,0 is at the edge of the printable area which is not the same as 0,0 for margins. You can set the graphics origin to the margins with OriginAtMargins:

    https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.originatmargins%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Which puts 0,0 at the edge of paper when margins = 0 as your code has. However it will not print over the non-printable area. This is same as using the translation I showed above.

    PS Print preview dialog does not use the specified printer non printable area so you will not see this in print preview.

    That is your problem. The graphics coordinates are not the same as the margin coordinates by default. Graphics 0,0 is at printable area 0,0 by default. Margins 0,0 is the paper edge or size and are not the same when the printer has non-printable area. Your printer has at lest 4mm somewhere that is not printable.

    I don't know how Word deals with it. How do you know it does? It could be off by 2mm on each side of the paper.


    PS Lots of discussion in this thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/f5aad166-60ca-40fa-9c0c-b3be3f459999/how-to-print-an-image-in-the-center-of-paper?forum=vbgeneral

    Thank you,

    your explanation is correct and useful, BUT how you can explain the same printer is printing the same receipt designed in MS Word without any left margin ?

    If the printer's head can't print at the very beginning of paper, it shouldn't do so with ANY software, while with MS Word it does.

    And if I print the configuration sheet, the printer starts printing from millimeter 0 on paper, and not from millimeter 9 as it happens with PrintDocument.


    Friday, November 3, 2017 1:26 PM
  • Thank you,

    your explanation is correct and useful, BUT how you can explain the same printer is printing the same receipt designed in MS Word without any left margin ?

    If the printer's head can't print at the very beginning of paper, it shouldn't do so with ANY software, while with MS Word it does.

    And if I print the configuration sheet, the printer starts printing from millimeter 0 on paper, and not from millimeter 9 as it happens with PrintDocument.


    Yes. Seems logical. I don't know how word does it. How do you know? Lets see the receipt? How do you know there is not 2mm in there at the edge?

    Plus, if the printer driver has a feature that is not supported by print doc you have to set it other ways. Is there a setting for it somewhere in the driver? How do you know? Maybe word uses that.

    Put the nonprintable area code in there and see what the values are? Tell me what you get for the printable area rectangle using the code I show please.

    Friday, November 3, 2017 1:35 PM
  • Thank you,

    your explanation is correct and useful, BUT how you can explain the same printer is printing the same receipt designed in MS Word without any left margin ?

    If the printer's head can't print at the very beginning of paper, it shouldn't do so with ANY software, while with MS Word it does.

    And if I print the configuration sheet, the printer starts printing from millimeter 0 on paper, and not from millimeter 9 as it happens with PrintDocument.


    Yes. Seems logical. I don't know how word does it. How do you know? Lets see the receipt? How do you know there is not 2mm in there at the edge?

    Plus, if the printer driver has a feature that is not supported by print doc you have to set it other ways. Is there a setting for it somewhere in the driver? How do you know? Maybe word uses that.

    Put the nonprintable area code in there and see what the values are? Tell me what you get for the printable area rectangle using the code I show please.

    Seems I've found the trick.

    It's not matter of PrintDocument's settings.

    It's just a stupid thing in the printer's driver : 

    There's a combobox "Format" with many options.
    By default the printer was on "PTP", and I put it on "Roll".

    Now on Roll, the printer starts from the paper's beginning, even with my code.

    Don't ask me what "PTP" or "Roll" should mean. I'm not a printer expert.

    Friday, November 3, 2017 3:11 PM
  • Hi MarcoGG,

    It seems that you have solves your issue now, if yes, please remember to close your thread by marking your last post or helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 7, 2017 9:16 AM
    Moderator
  • Hi MarcoGG,

    It seems that you have solves your issue now, if yes, please remember to close your thread by marking your last post or helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Ok. Done.

    Thank you.

    Wednesday, November 8, 2017 8:05 AM