Results 1 to 16 of 16

Thread: Printing form on 8 1/2 x 11 paper

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Printing form on 8 1/2 x 11 paper

    I found this code somewhere in my google search and it works very nicely.
    Code:
    Private Sub btnPrintForm_Click(sender As Object, e As EventArgs) Handles btnPrintForm.Click
            'Print Form Fit to Page 8 1/2 X 11
            Dim preview As New PrintPreviewDialog
            Dim pd As New System.Drawing.Printing.PrintDocument
            pd.DefaultPageSettings.Landscape = True
            pd.DefaultPageSettings.PaperSize = New PaperSize("A", 850, 1100)  '8 1/2 X 11   100's of an inch
    
            AddHandler pd.PrintPage, AddressOf OnPrintPage
            preview.Document = pd
            preview.ShowDialog()
    End Sub
    
    Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            'create a memory bitmap and size to the form
            Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
    
                'draw the form on the memory bitmap
                Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height))
    
                'draw the form image on the printer graphics sized and centered to margins
                Dim ratio As Single = CSng(bmp.Width / bmp.Height)
    
                If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                    e.Graphics.DrawImage(bmp,
                    e.MarginBounds.Left,
                    CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
                    e.MarginBounds.Width,
                    CInt(e.MarginBounds.Width / ratio))
                Else
                    e.Graphics.DrawImage(bmp,
                    CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
                    e.MarginBounds.Top,
                    CInt(e.MarginBounds.Height * ratio),
                    e.MarginBounds.Height)
                End If
           
    End Sub
    I think I understand it until I get to this code:
    Code:
               If ratio > e.MarginBounds.Width / e.MarginBounds.Height Then
                    e.Graphics.DrawImage(bmp,
                    e.MarginBounds.Left,
                    CInt(e.MarginBounds.Top + (e.MarginBounds.Height / 2) - ((e.MarginBounds.Width / ratio) / 2)),
                    e.MarginBounds.Width,
                    CInt(e.MarginBounds.Width / ratio))
                Else
                    e.Graphics.DrawImage(bmp,
                    CInt(e.MarginBounds.Left + (e.MarginBounds.Width / 2) - (e.MarginBounds.Height * ratio / 2)),
                    e.MarginBounds.Top,
                    CInt(e.MarginBounds.Height * ratio),
                    e.MarginBounds.Height)
                End If
    What I don't understand is this "e.MarginBounds". There is Width, Height, Top...of what?
    Can someone tell me what "e.MarginBounds" is? Is it the paper size?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Printing form on 8 1/2 x 11 paper

    Easily found by searching for what e is.

    https://docs.microsoft.com/en-us/dot...t-plat-ext-5.0

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    e.MarginBounds is a rectangle within the page bounds, located and sized as specified by the margins properties

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Re: Printing form on 8 1/2 x 11 paper

    paul, what you are saying is e.MarginsBounds.Width and e.MarginsBounds.Height is the size of the paper I am printing on.
    Then what is e.MarginsBounds.Top? I mean how was it calculated? There is nothing obvious to that.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    No assuming you have e.marginbounds.top and bottom of 100px each and left and right both 100px each, then the rectangle described by e.maginbounds is your paper size minus those borders

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Printing form on 8 1/2 x 11 paper

    Review the documentation that OptionBase1 linked to. The property value is a Rectangle. The Rectangle has two constructors, both of which specify the initial size and position.

    Looking at the documentation for PrintDocument (here), there appears to be a DefaultPageSettings property (here).

    The property value for DefaultPageSettings is a PageSettings object (here).

    Look at that documentation, there appears to be a Margins property (here) with this comment:
    A Margins that represents the margins, in hundredths of an inch, for the page. The default is 1-inch margins on all sides.
    All in all it took me longer to write out this post than it did to visit the link that OptionBase1 provided and to traverse my way to the appropriate documentation.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    The inner dashed rectangle in this image shows e.marginbounds...

    Attachment 180391

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    Attachments don't always work great in this forum. Any mods who see this, can you fix my image upload?

    http://www.scproject.biz/marginbounds.png

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Printing form on 8 1/2 x 11 paper

    I haven't figured out why the attachments work half the time either. I believe Steve is aware of it.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Re: Printing form on 8 1/2 x 11 paper

    Thanks to your help I have seen that things like like MarginBounds.Top or Left or Right etc is nothing more than a default of 100,
    and that it refers to a rectangle(I had already concluded) that starts out as the paper size and is scaled to make the BMP scaled to fit the paper size.

    Also I found by looking at some of the Microsoft documents that paper size is the default printer size.
    Microsoft documentation for someone looking for answers the first time is very difficult, once you learn it then it is a good reference.

    I wish there was a book that would take the Microsoft Documentation and explain it. The books you can buy are just too basic.

    To this day I can't figure out how to find the Microsoft documents that refer to what I need. If you responders didn't show me with your post I would not have found the documents I was looking for, even then the explanations in the Documents are lacking.
    Yes I can google them and maybe it would be what I was looking for.

    In earlier Visual Basic program you could follow the program and know where it came from. Now it is very difficult.

    Thanks OK for all your help.

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Re: Printing form on 8 1/2 x 11 paper

    After using msgbox I find that e.MarginBounds refers to the bmp rectangle that fits on the paper size not the actual form size. I guess that's what paul was trying to tell me.

    Where did that get changed from the original form size (Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height)

    I found this at Microsoft:
    Parameters
    bitmap
    Bitmap
    The bitmap to be drawn to.

    targetBounds
    Rectangle
    The bounds within which the control is rendered.

    That does not tell me what it got sized too, obvious must be less than the paper.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    e.marginbounds is a rectangle within the physical printed page. It isn’t the paper size. The size of your form is irrelevant. If you want to print your form on 8 1/2 x 11 paper, you might have to scale your form image down, or if your form image is smaller than a printed page, you might want to scale it up or just centre the unscaled image.

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Re: Printing form on 8 1/2 x 11 paper

    Thank you paul, I am getting closer to understanding it.
    Early in the code the physical printed page was set to 1100 x 850 (8 1/2 x 11 landscape) and the form was larger than that
    so the If statement scaled it to fit.

    I wish I had copied who I got the code from so I could thank them. It works very nicely I am just trying to understand it.

  14. #14

    Thread Starter
    Member
    Join Date
    Sep 2020
    Posts
    62

    Re: Printing form on 8 1/2 x 11 paper

    Because the Form is set in size and will always be printed on Landscape 11 x 8 1/2 and maybe someone else can use the code.
    I have shortened the code to this:
    Code:
    Private Sub btnPrintForm_Click(sender As Object, e As EventArgs) Handles btnPrintForm.Click
            Dim preview As New PrintPreviewDialog
            Dim pd As New System.Drawing.Printing.PrintDocument
            pd.DefaultPageSettings.Landscape = True
            pd.DefaultPageSettings.PaperSize = New PaperSize("A", 850, 1100)  'MAKE SURE IT IS 8 1/2 X 11   100's of an inch
            AddHandler pd.PrintPage, AddressOf OnPrintPage
            preview.Document = pd
            preview.ShowDialog() 'SHOWSS HOW IT LOOKS BEFORE PRINTING FROM HERE YOU CAN PRINT OR CANCEL
        End Sub
        Private Sub OnPrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
            Using bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
                Me.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Width, Me.Height)) 'DRAW THE FORM ON A BMP
                e.Graphics.DrawImage(bmp, 100, 100, 900, 650) 'SINCE I KNOW THE PAPER SIZE OF 1100 X 850
            End Using
        End Sub
    Any comments would be appreciated.

  15. #15
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Printing form on 8 1/2 x 11 paper

    To this day I can't figure out how to find the Microsoft documents that refer to what I need.
    This might help... here's how I search for things: go to google, then use [language] [class] [method] so in this case "vb.net printdocument marginbounds" this usually brings up a pretty good set of results, and usually the documentation is close to the top if not the top result.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing form on 8 1/2 x 11 paper

    You can also set the WindowState of the PrintPreviewDialog, before showing it...

    Code:
    preview.WindowState = FormWindowState.Maximized

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