Printing in .NET

  • Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/jGuenther/printinginnet.asp

  • I have noticed a major flaw in this code.  It prints great when using the Tahoma font, but anything else seems to really mess it up.

  • 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)) < 30 Then ' make sure to dispose of odd char's in the array

    ' See if any characters will fit.

    ElseIf characters_fitted > 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).

  • Another update, I have perfected the variable character spacing problem when the font size and type changed.  Add the following line somewhere above The 'Static i As Integer' line:

    ' Prepare the fixed spacing for each character

    Dim fixedSpace As SizeF = e.Graphics.MeasureString("W", the_font)

    Then replace the following line:

    ' Increase the location where we can start.

    xmin += CInt(text_size.Width) - 4

    with:

    ' Increase the location where we can start.

    xmin += CInt(text_size.Width) - (fixedSpace.Width * 0.25)

  • Do you know of a simple way of making a screen-shot (winforms) with .Net code, using the framework (not extra referring to the gdi32.dll ) ?

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Hi Jereme

    Thanks for this nice piece of work!

    I have tried to get it to work in ASP.NET 1.1 (vb) but get a run time error "No printers installed."

    I do have a deault printer but it is a network printer and not on LPT1.

    Any ideas?

    Many Thanks

    Howard Rybko

     

  •  

    Hi Howard, that is my fault for not giving you all the code to properly initialize the class.  Try the following:
     

           ' Create object, passing in text

            Dim MyPrintObject As New TextPrint("<B>this will be bold</B>" + vbCrLf + "<ST=400>this will start at 400 pixels", "MyPrinterName")

    Also modify the printing class itself to accept two parameters:

    ' New constructor

    Public Sub New(ByVal Text As String, ByVal pName As String)

      ' Sets the file stream

      MyBase.New()

      varStart = 0

      strText = Text

      MyBase.PrinterSettings.PrinterName = pName ' This is the key line for setting the printer

      MySplitLine = strText.Split(vbCrLf)

    End Sub

    I too use exclusively network printers not local LPT1 ports.

  • Hi alzdba,

      I have never tried taking a screen shot in .net.  Here is a link on how to do it but I does appear that you will need to use API functions.

    http://www.developerfusion.co.uk/show/4630/

  • I've not tried your code, but I just want to say thank you for sharing your hardwork with everyone. It's very generous of you.

  • Thanks for sharing.  It is very generous of you.

  • Hi Min and jkli,

      I always like helping others out.  I have received enough support throughout my life that I like handing out some assistance myself every once in a while.

  • Hi Jereme

    Thanks for the update for my printing.

    Thanks again for your contirbution!

    Howard

  • Nice to see a wide variety of articles on the site

    Having said that, two comments.

    1. The comment earlier about "no default printer" when the user had a network printer - if ASP.NET is running under a service account it will not have network printers already established - only local printers (LPT, USB, TCP, LPR, etc) printers are, by default, available to service apps.  You'll need to do one of the following

    a) call the windows API to connect to a network printer when your app starts

    b) change the printer to print to LPTx and then issue a NET USE LPTx: \\computer\printer when your app starts

    c) run the service under a user account that you have logged on to and set up some printers

    d) Install the "Print services for Unix" on the computer that is hosting the printer (and then set the service to automatically start!) - you can then set up a TCP printer port printing to that computer - the wizard for this part will say that it cannot detect the type of network card for the printer - don't worry, choose generic   Then configure the port, change it to LPR and enter in the printer's share name.  Then the printer will be a local printer rather than a network printer and it will be available to all users on the machine - including service accounts

    The above has come in very useful when configuring terminal server environments - ensuring all users get all printers without messy scripts and ensuring that permissions on printers and PCs not in the domain do not get in the way (TCP printers don't have any permissions - no shared print queue).

    Second point....  hahahaha

    I'm not a big .NET programmer but know my way around C# pretty well (I use delphi 99% of the time) - If I had a similar requirement I would probably format my text using RTF and print it using the RichText control?  If I needed HTML syntax then I guess I could spawn explorer - wouldn't this be a lot easier???

    Full marks though for sharing the code and working out the nitty gritty stuff!

  • Hi Ian, thanks for your comments they really add value to this thread.  You have some idea's in there which I didn't think of.

    As far as setting up the printers to the asp.net account can use them, I used an account with administrative rights (not the asp.net domain service account) and set up the printers and asp.net was able to see them fine.  This is on a Windows 2000 box, other OS's might not respond so well.  After reading your post I am not sure why my method works, I will have to think on that some more.

    I tried spawning an Internet Explorer window, if that is what you are referring to, but it had a few major formatting problems.  In particular, it would cut of the right hand side of the text when it was formatted with a table.  There were a few other security problems and annoyances as well which caused me to dump the idea.

    As to using the RichText control, that is a great idea.  I didn't know that asp.net had a native RichText control.  Don't forget that I am working in a web environment with this code.  If it was a simple vb.net application I can think of many better ways for printing nicely formatted text but when I am dealing with web pages which need to print on the server, not client, side I was unable to think of a better solution.  If you know of a way to use a native (or free) RichText control for printing text, where you can control the color of each character, on the server side then please share.

  • Hi.

    I believe there would be an ActiveX control that you could spawn hidden (needn't have a parent window) - The RTF control is standard with Windows.  I have used it in a windowless environment to do similar printing - but I haven't tried using it from .NET though...

    As for the printers for the ASP.NET account - are they network printers or local printers attached to the machine or "local printers" in that they are printing to a local printer port but happen to be on the network?

    Cheers!

Viewing 15 posts - 1 through 15 (of 39 total)

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