Printing text in VB.net

Cy_Chosis

New member
Joined
Aug 11, 2006
Messages
2
Programming Experience
3-5
I've programmed in vb 5 & 6 for years, but am new to vb.net. I'm currently working on a contract that I have to finish by the beginning of next week. Everything went smoothly until it came time to send a report to the printer.


I know how to use crystal reports, but it did not come with my trial version for some reason, so that is not an option. I must print reports manually.
In vb6, it was easy...

printer.print "Name:"; tab(10); employee.name

in vb.net they want me to specify all this junk: the actual string, all font information, the color, and the exact coordinates on the page to put the line of text. They want me to do this ON EVERY LINE???? For instance:

e.Graphics.DrawString("Friends For Learning Course Enrollment Roster",
New Font("Arial", 10, FontStyle.Regular), Brushes.Black, 100, 100)

PLEASE tell me there is an easier way to do this!!! This will take me DAYS to write code just to send reports to the printer if I have to do it this way.

Also, how am I supposed to know the coordinates for every line of text???? I want to print one line, then go to the next line, and so on. I don't know how many twips from the top of the page that the 14th line of text is!!!

PLEASE HELP!!! IS THERE ANY STRAIGHTFORWARD WAY TO PRINT TEXT DOCUMENTS IN VB.NET????
(I could write text to a file, then print the file, but that seems like a hack job way of doing things to me. There has to be a better way to print text.)
PLEASE HELP! Thank You!!!
 
what i do is:
VB.NET:
    Private Sub prtReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prtReport.PrintPage
        Static fntPrintFont As New Font("Arial", 10, FontStyle.Regular)
        Static fntHeadingFont As New Font("Arial", 16, FontStyle.Bold)
        Static sngLineHeight As Single = Convert.ToSingle(fntPrintFont.GetHeight * 1.4)
        Dim strTitle As String = gstrTitleVersion
        Dim stzStringSize As SizeF
        Dim sngCenterPage As Single
        Dim y As Single = 25
        With e.Graphics
            stzStringSize = .MeasureString(Window.DateMade, fntPrintFont)
            .DrawString(Window.DateMade, fntPrintFont, Brushes.Black, e.PageBounds.Width - (stzStringSize.Width + 55), 15)
            'Calculate the X coordinate for centering the page no
            sngCenterPage = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(strTitle, fntHeadingFont).Width / 2)
            'Draw the Page # on the document
            .DrawString(strTitle, fntHeadingFont, Brushes.Black, sngCenterPage, y)
            y = 50
            'So on and so forth, y is used to know which line on the page the text will be put
    End Sub

basically, once the font is selected i store it's height in a variable and i multiply the height by 1.4 to give it some space between lines
there's even some code in there for centering text on a page as well

hope it helps, you can always ask questions if you're not sure how that code up there works
 
As in many aspects of VB.Net the core is the .Net framework and OOP inheritance the building platform. .Net printing facilities is barebone and let you do anything, but general purpose ready print-my-text or print-my-datagrid etc is not available because there are unlimited possibilities as to exactly how anyone would want their content to look on a printed page. Printing is all about personal preference report templates for different type content. So you start creating your own classes to do these tasks as to how you want it done. If you create a PrintPlainText class the job is done once and for all, every time you need to have a document printed with just plain text you get your own ready made PrintPlainText class adds the text and call print. Job done in 2 seconds. Here is a plain text printing class 'TextPrint' by Karl Moore http://www.developer.com/net/net/article.php/3102381 (perhaps you want to modify that to include a first page title? common headers? or add page numbering in footers? ... ;))
 
Thank you

Thank You, Thank You.

You were both very helpful.

After looking at the code you sent, I went ahead and wrote another program that would tell me how many twips from the top of the page to the bottom (1100), from the left to the right (850), and in the height of different sizes of fonts (12*1.4 is 32.1972656 and 16*1.4 is 42.9296837).

I used that information to create a class that would make it possible to efficiently code reports.

In a way it is kind of neat to create your own classes to interface with the bare bones printing interface in vb.net, since you can personalize the functions to your coding style. It sucks, however, when you have a contract to fill and you didn't allow time for to code your own interface in your time estimate. Boo to Microsoft for not already including any kind of reasonably efficient interface.

Thanks again.
 
Back
Top