Printing in .NET

  • I will be a little annoyed with myself if the RichText box idea proves to work in a webpage environment.  That was a lot of work if something better already exists

    The printers I use print to local ports which are UNC paths to the print server.  The print server then sends the job off to the IP port.

  • Well part of me hopes it works and part now doesn't hahaha - in any case, it sounds like you had a good time developing

  • Hey, all of me hopes it works.  True i did have fun with it but my code is far from perfect.

  • Hi!

    This is one of the very rare GOOD code samples for printing I've come accross - good work!!

    But, I have problems modifying the code.... for example, if I want to add alignment option to the paragraph (center or justified), it looks nearly imposible to insert into code

    Do you have any ideas how can this be done?

    Thanks!

  • Hi Veljko,

      Bummer by first reply got erased.  Oh well here goes again.  I like your idea, it is a good challenge and one which I didn't think of at the time of development.

    For reference e is being passed as (ByVal e As Printing.PrintPageEventArgs)

    What I would suggest trying is finding the width of the printable area using something like:

    e.MarginBounds.Width

    Then you will have to find the width of the string you are printing by collecting the entire string instead of merely one word.  Use the width of the collected string and the width of the printable area to calculate the new starting location.  Once you have that you can print out the string.

    Remember:

    If you want to keep the character level formatting then you will still have to loop through the collected string printing one character at a time.

    Also, don't forget that you can't measure the entire collected string to find out the string width because of the character level formatting.  Instead you will have to collect the widths of each character as you gather the string, then use the some of these widths as your total string width.

    Hope this helps,

    Jereme

  • Thanks for your nice code and idea.

  • No problem, it is a start.  Let us know if you develop any new cool addons or if you clean it up a bit.

  • Hi,

    I have two request:

    1 ) Could you explain me more about the following comment you have already explained and let me know which line should be repleaced. ( The message below the star line)

    2) I would like to print the characters from right to left because of my native language ( arabic ), could you please give me a hand to solve it.

    thanks in advance

    ***********************************************

    Ok, I have found the problem. Apparently Microsoft has a bug in their e.Graphics.MeasureString function. To remidy this re-organize the primary if block like this:

    If Asc(smallArray(0, x).Chars(0)) 0 And xmin < (e.MarginBounds.Right - 5) Then

    Notice that I switched the if and elseif lines, that is because I was occasionally printing out a graphical representation for the char(0).

    **********************************************************

  • Sure saeed,

    1) If you will look at the code in the article you can use the following exerpt to identify the correct location for changing the code.  The code you quoted is what you are suppose to change it to.

                    ' See if any characters will fit.

                    If characters_fitted > 0 Then

                        ' start accumulating the print location

                        varWord(varWord.GetUpperBound(0) - 1) = layout_rect

                        ' ************Draw the word when finished.************

    ...

                        '' Draw a rectangle around the text (for debugging).

    ...

                        ' Increase the location where we can start.

                        xmin += CInt(text_size.Width) - 4

                        ReDim Preserve varWord(varWord.GetUpperBound(0) + 1)

                    ElseIf Asc(smallArray(0, x).Chars(0)) < 30 Then ' make sure to dispose of odd char's in the array

    2) I assume from your post that you also read right to left so you need the entire printing process reversed.  This is much simpler than reading left to right but printing right to left.  I haven't actually tested this but the solution should be as simple as replacing the red code with the green:

                        ' ************Draw the word when finished.************

                        If Asc(smallArray(0, x).Chars(0)) = 32 Or x = smallArray.GetUpperBound(1) - 1 Then

                            Dim z As Integer

                            For z = x - (varWord.GetUpperBound(0) - 1) To x

                                ' Get the font for measurement.

                                the_font = New Font(fntPrintFont.Name, fntPrintFont.Size, CInt(smallArray(1, z)), fntPrintFont.Unit)

                                ' actually print the character on the page.

                                e.Graphics.DrawString(smallArray(0, z), _

                                    the_font, Brushes.Black, _

                                    varWord((z - x) + (varWord.GetUpperBound(0) - 1)), string_format)

                        ' ************Draw the word when finished.************

                        If Asc(smallArray(0, x).Chars(0)) = 32 Or x = smallArray.GetUpperBound(1) - 1 Then

                            Dim z As Integer

                            For z = x - (varWord.GetUpperBound(0) - 1) To x

                                ' Get the font for measurement.

                                the_font = New Font(fntPrintFont.Name, fntPrintFont.Size, CInt(smallArray(1, z)), fntPrintFont.Unit)

                                ' actually print the character on the page.

                                e.Graphics.DrawString(smallArray(0, z), _

                                    the_font, Brushes.Black, _

                                    e.MarginBounds.Right - varWord((z - x) + (varWord.GetUpperBound(0) - 1)), string_format)

    One thing that does have me confused though.  If your native language reads right to left then why isn't the input string being put in right to left which would cause it to be printed out correctly?

  • How can I make certain lines of text italics, i am using this class in a WinForm, and it works really great as far as the actual printing goes. But I dont know how to format the text. for example If I want to have a Headline in a bigger font, do I instantiate another myPrintObject ?

  • Text formatting is taken care of in the checkBold method.  I know the name doesn't really fit it, but that is what happens when you are a little green when writting the code and don't properly plan it.

    For italics you would add something like this (not tested):

                ElseIf Mid(varString, varPlace, 3) = "<I>" Then

                    printStyle = FontStyle.Italic

                    varPlace += 2

                ElseIf Mid(varString, varPlace, 4) = "</I>" Then

                    printStyle = FontStyle.Regular

                    varPlace += 3

    The font size would be taken care of in the same place.  I would use a tag such as <SIZE=?> </SIZE>.  A change like this will require you to understand the code fairly well and make a few changes to the main print method to implement it.

    Unfortunately, the project this code belongs to has been put on hold for awhile so I have been unable to complete it.

  • Jereme,

    Thanks for the italics code, I added it to the checkbold method, and it does print italics, however it does not print every letter in the string. It skips the first 2 char, prints the 3rd,skips the 4th,prints the 5th and 6th, skips 7th, print 8th,skip 9th. prints 10, skips 11, prints 12, skips 13,prints 14,skips 15+16, prints 17-21, skips 22 prints 22-25 etc..

       

  • What is the string that you are trying to print?  I mean the full string including the formatting tags like <I>.

    What do you mean by "not printing every letter"?  Is it not printing the actual character, or is it simply printing the character without italics?

  • Jereme,

         Thanks for the great effort.  I tried your code, and it works.  My problem however is that I'm trying to print an HTML document in .net programmatically!  I've tried a few tutorials, but they all seem to be centered around sending text to the printer. 

    Have you come across examples where html docs can be sent to the printer easily? I'm sure there must be an easy way, but I haven't come across it yet.

    By the way, I tried to read my html as a string, and then use your class to print it, however all I got to print was the actual html code and not the rendered html document.

    Appreciate your help.

     

  • Hey kmsultan, I am a little confused about the question.  If you are the one creating the webpage which needs to be printed then you should have the data itself and can properly format it another way.  If you are going for clientside printing you can use the javascript print feature, which will have the user select a printer to print to.

    If you really need to print using the HTML code itself then you will probubly need to look at using an HTML parsing engine like gecko (which firefox uses).  I have never attempted that before and would seriously look at your options before starting something this major.

    Jereme

Viewing 15 posts - 16 through 30 (of 39 total)

You must be logged in to reply to this topic. Login to reply