Question about Printing

Posted on 2004-11-23
Last Modified: 2008-01-09
Hello fellow experts. Quick printing question for you:

The following stupid simple code prints a box and some text on a printer.

    Dim WithEvents PrintDoc As PrintDocument
    Public Sub PrintTestDoc(ByVal Printer As String)
            PrintDoc = New PrintDocument
            With PrintDoc
                .PrinterSettings.PrinterName = Printer
            End With
        Catch ex As Exception
        End Try
    End Sub

    Private Sub PrintDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
        Dim Font As New System.Drawing.Font("Tahoma", 12, Drawing.FontStyle.Regular)
        e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)
        e.Graphics.DrawString("This is a test printing from a web service.", Font, Brushes.Black, e.MarginBounds.X + 10, e.MarginBounds.Y + 10)
        e.HasMorePages = False
    End Sub

I noticed when I run this code, a window pops up that says "Printing page 1 of 1" and then disappears after the document is spooled to the print queue.

Doc comes out just fine.

My question is this: Is there any way to supress that window from popping up? My code is popping anything up.

The underlying problem here is that I am trying to get an INTRAnet web service to print to a network printer and am getting errors printing. I am now thinking that the errors could be explained by the fact that the Web Service doesn't have any UI and possibly can't popup this little window, so if I can suppress the window, perhaps it will finally work!
Question by:ptakja
    LVL 9

    Expert Comment

    Make sure the INTRAnet web service is running under an account that has permission to use the printer in question.  From what I've seen, PrintDocuments work fine in services without having to suppress the dialog box.
    LVL 14

    Author Comment

    I am doing that (I think) through the Impersonate Attribute in my Web.Config file. I set it to true, and set a username & password that has explicit permission to print.

    When I try to print, I get the following pair of errors logged to the Event Log on the server:

    Event Type:      Error
    Event Source:      Print
    Event Category:      None
    Event ID:      6161
    Date:            11/23/2004
    Time:            5:12:13 PM
    User:            LUCY\ASPNET
    Computer:      LUCY
    The document ActiveReports Document owned by ASPNET failed to print on printer \\ejps01\dnmskinbatch. Data type: NT EMF 1.008. Size of the spool file in bytes: 65536. Number of bytes printed: 0. Total number of pages in the document: 1. Number of pages printed: 0. Client machine: \\LUCY. Win32 error code returned by the print processor: 1326.  

    Event Type:      Error
    Event Source:      Print
    Event Category:      None
    Event ID:      45
    Date:            11/23/2004
    Time:            5:16:03 PM
    User:            LUCY\ASPNET
    Computer:      LUCY
    Document failed to print due to GDI/Driver error in rendering.

    The weird thing is that the User shown above is NOT the same as the one I set up in the web service. Should it be? When I make a change to the web.config file, do I need to do anything to the server, like reboot it? Or restart the aspnet_wp?

    If I remember correctly, the Web.Config file overrides settings in the Machine.Config file. Right?

    I have tried differnet printer drivers as well including the PS, PCL5 & PCL6 for my HP laser printer.
    LVL 9

    Accepted Solution

    I think it may be a different matter when it comes to services.  I know there is a way to impersonate a process on a web server, not sure how.  (I'm more of a System Admin and Windows Forms programmer, not ASP.NET web though...)  You are however picking up on the same thing I am, the account needs permission.  The error "Document failed to print due to GDI/Driver error in rendering. " might be when your software is trying to get the page settings for the PrintDocument, but doesn't have permission to access the printer.

    The best thing I can think of is to configure IIS to run that one page as a user that has permission to the printer, or configure the printer to allow LUCY\ASPNET to print, or configure the entire application to run under the permission of a user who has permission to print...

    As far as impersonation of the pages, I found:

    Hope this helps!
    LVL 14

    Author Comment

    Not exactly the answer but you confirmed my suspicion. Found out that the permission problem lies in the print server, which is not the same box as the web server.

    Thanks anyway!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now