SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Printing in .NET


Printing in .NET

Author
Message
jereme.guenther@gmail.com
jereme.guenther@gmail.com
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1501 Visits: 194

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.


Howard Rybko
Howard Rybko
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 22

Hi Jereme

Thanks for the update for my printing.

Thanks again for your contirbution!

Howard


Ian Yates
Ian Yates
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4694 Visits: 445

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!





jereme.guenther@gmail.com
jereme.guenther@gmail.com
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1501 Visits: 194

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.


Ian Yates
Ian Yates
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4694 Visits: 445

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!





jereme.guenther@gmail.com
jereme.guenther@gmail.com
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1501 Visits: 194

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.


Ian Yates
Ian Yates
SSCarpal Tunnel
SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)SSCarpal Tunnel (4.7K reputation)

Group: General Forum Members
Points: 4694 Visits: 445

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





jereme.guenther@gmail.com
jereme.guenther@gmail.com
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1501 Visits: 194
Hey, all of me hopes it works. True i did have fun with it but my code is far from perfect.
Veljko Zrnic
Veljko Zrnic
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 10
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!
jereme.guenther@gmail.com
jereme.guenther@gmail.com
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1501 Visits: 194

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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search