Forum Moderators: open

Message Too Old, No Replies

Server Side Printing in .Net

....anyone done it?

         

RossWal

11:11 pm on Jan 7, 2003 (gmt 0)

10+ Year Member



I'm trying to print a text string but can't access the printers on the network. I can do it from PWS (with impersonate=true in the web config, so that I'm accessing the printers under my profile on my desktop) but I don't know how to gain 'visibility' to the printers on the web server (Win2k). I have a DLL (com object) I've used for this in ASP, but I want to get away from that in .Net if possible.

Anyone have experience with this?

Thanks!

PS I googled up a reference to this issue, but that told me to look at the SamplePad application in the help files, which I can't find.

duckhunter

3:56 am on Jan 8, 2003 (gmt 0)

10+ Year Member



Try this:

Declarations

Imports System.Drawing.Printing
Imports System.ComponentModel
Imports System.Drawing
Imports System.IO

I put this in the Page_Load Event but it could be called from anywhere in the page:

Try
Dim pd As New PrintDocument()
pd.PrinterSettings.PrinterName = "\\ServerName\PrinterShareName"
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.Print()
Catch ex As Exception
Response.Write("Error: " & ex.ToString)
End Try

Finally, here's the object reference that is passed into the Handler (Notice the printed line message is set here):

Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim yPos As Single = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String
Dim printFont = New Font("Arial", 10)

line = "This is my printed Line"

ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())

End Sub

[edit]This works if the webserver is attached to the printer and everyone or ASPNet has print permissions on the share. Cross network I ran into RPC issues, probably permissions[/edit]

RossWal

5:14 pm on Jan 8, 2003 (gmt 0)

10+ Year Member



Thanks Duck! I'm pretty much doing the same thing as the code you posted. Can you elaborate on the server being "attached to the printer"? Are you talking about setting up the printer under Settings/Printers? If so, is it true that the printer is only accessible when the person who set it up is logged into the server? I need to be able to print regardless of who (it's usually no one) is logged into the server.

Thanks again,
Ross

duckhunter

7:58 pm on Jan 8, 2003 (gmt 0)

10+ Year Member



the printer is only accessible when the person who set it up is logged into the server

Good question. I'll log off and hit the page from another machine and let you know.

duckhunter

1:49 am on Jan 9, 2003 (gmt 0)

10+ Year Member



setting up the printer under Settings/Printers
Yes and creating a Share

This all works fine when I'm logged off too but I think permissions is your problem...

I reproduced your error (you have accessed the printer with Invalid settings.....) by removing the Everyone group's print permissions via the Printer Sharing dialogue.

I then granted the ASPNet user Print permission and it worked.

You must be logged on locally as an Administrator to make these changes.

RossWal

4:12 pm on Jan 9, 2003 (gmt 0)

10+ Year Member



Hmmm. This is perplexing. The printer in question is shared for everyone, in fact I use it on another server running classic asp. That's NT4, this is W2K. I'm not getting a permissions error, I simply can't see the printer. I have a snippet that loops through the installed printers response.writing their names and it's finding none. I haven't yet tried it while logged onto the server box. That's next.

RossWal

10:47 pm on Jan 9, 2003 (gmt 0)

10+ Year Member



We reinstalled some printers for using internet printing under IIS5. Don''t kknow why that fixed my server-side printing issue....but it did. Thanks Duck.