ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!
Home
Upgrade
Credits
Help
Search
Awards
Achievements
 4425

[VB.NET] Printing

by 1UP - 10-11-2016 - 03:52 PM
#1
This deals with setting up a page to actually print information to a printer. This was pry one of the biggest pain in the ass things I had to do (and really it's pry best to make a class out of this if you plan on using this for anything else in the future)

Lets get started:

On your form you are going to want to add 2 controls
-PrintPreviewDialouge
-PrintDocument

With that said we will now need to get the page setup for printing.


The code for the Print document is as follows

Code:
Dim PrintFont As New Font("Arial", 12)
        Dim HeadingFont As New Font("Arial", 14, FontStyle.Bold)
        Dim LineHeightSingle As Single = PrintFont.GetHeight + 2
        Dim ColumnHorizontalLocationSingle As Single = e.MarginBounds.Left
        Dim VerticlePrintLocationSingle As Single = e.MarginBounds.Top
        Dim Column2HorizontalLocationSingle As Single = 300
        Dim Column3HorizontalLocationSingle As Single = 500
        Dim PrintLineString As String
        Dim FontSizeF As New SizeF
        Dim FormattedPriceString As String

Let's explain some of this.
Printfont is pretty much explains itself. This is just where you will set your font you want to use and size.

Heading Font is the same thing, just a different name since it will be used for a different line on the paper.

Lineheight is the space between lines (much like how when you do something in word you set double line space to make the text readable)

Pretty much the rest deals with margins (so you dont have text slammed up on the edges of the paper.)

Now for a sample of what this would look like when you fill it out.

Code:
PrintLineString = "Store Shipping Summary"
        e.Graphics.DrawString(PrintLineString, HeadingFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)

        PrintLineString = "by Justin"
        VerticlePrintLocationSingle += LineHeightSingle
        e.Graphics.DrawString(PrintLineString, PrintFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)
        VerticlePrintLocationSingle += LineHeightSingle * 2

        e.Graphics.DrawString("Weight", PrintFont, Brushes.Black, ColumnHorizontalLocationSingle, VerticlePrintLocationSingle)
        e.Graphics.DrawString("Zone", PrintFont, Brushes.Black, Column2HorizontalLocationSingle, VerticlePrintLocationSingle)

        FormattedPriceString = Format("Price")
        FontSizeF = e.Graphics.MeasureString(FormattedPriceString, PrintFont)
        Column3HorizontalLocationSingle = 555 - FontSizeF.Width

        e.Graphics.DrawString(FormattedPriceString, PrintFont, Brushes.Black, Column3HorizontalLocationSingle, VerticlePrintLocationSingle)
        VerticlePrintLocationSingle += LineHeightSingle

Now this is just a small sample of the code (the rest deals with other things the program was doing and you wouldnt get alot out of it without the entire program being listed, but an image should suffice.
[Image: printup.jpg]
Reply
#2
Very nice thank you for the contribution, I will have to try this out.
Are you a guest to this site? Click the image below and sign up today!
[Image: FTYbRmR.gif]
Reply

Users browsing: 4 Guest(s)