locked
Server Side Printing in C# .Net RRS feed

  • Question

  • Hi,

    I want to print data without displaying it on the UI (probably inside windows service) while retaining formatting of the data. Essentially I’ll fetch data from the database and convert it in to a suitable format and then finally print it on a network printer.

    I am looking out for any formats that can be used in server side printing environment using .Net code.

    One option that I was thinking is to create HTML page from the data and launch IE using WebBrowser control to fire prints. But, it seems using a WinForm control in server side environment might not be appropriate.

    Please suggest any other way to print in a Server side environment (Windows).

    Thanks 

     

    Monday, May 10, 2010 2:32 AM

Answers

  • hi,

    try this

    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]

     

    • Proposed as answer by asthanarht Monday, May 10, 2010 5:07 AM
    • Marked as answer by SamAgain Friday, May 14, 2010 6:10 AM
    Monday, May 10, 2010 5:07 AM

All replies

  • When you're going to print from Server Side let's say indeed a Windows Service it's not going to be easy. First of all, mostly Windows Services will be running on a localsystem service account instead of a domain account. Second, it's hard to print to a network printer from a Windows Service. If the network is down, or the printer is down your files will not get spooled, because your printer isn't there.

    The best approach in that way is to make that printer local with a domain account, so that your files will getting spooled and when the printer is coming online the files will be printed.

    Monday, May 10, 2010 4:57 AM
  • hi,

    try this

    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]

     

    • Proposed as answer by asthanarht Monday, May 10, 2010 5:07 AM
    • Marked as answer by SamAgain Friday, May 14, 2010 6:10 AM
    Monday, May 10, 2010 5:07 AM
  • When printing by a Web Server and running impersonated you will need to add the user to the security policy: "Allow log on locally"

    Monday, May 10, 2010 5:34 AM
  • Hi,

    We temporarily mark a reply, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Please mark the right answer at right time.
    Thanks,
    Sam
    Friday, May 14, 2010 6:11 AM