How to Print directly without the help of the browser using C# in web application in visual studio 2005

Posted on 2009-02-13
Last Modified: 2013-12-17
Currently we print the pages using the File> Print option in the browser.  Is there any way to use a print button in the page and when clicking on the print button i should be able to connect to the printer.
Question by:shenoyroopesh
    LVL 29

    Assisted Solution

    by:Gautham Janardhan
    you can use th javascript window.print() . this wil popup the windows print dialog to select th printer.
    LVL 16

    Expert Comment

    Try this as gauthampj pointed out
    <input type="button" value="Print Page" onclick="javascript:window.print();">

    Open in new window

    LVL 15

    Expert Comment

    Using window.print() will pop up the same dialog that you get when clicking File>Print in the menu.

    If you want to connect directly to your printer to print something out, it will need to be on the same network as your server, and you will have to generate the page on the fly.  

    You could combine this with a WebClient that can download the page into a String, and print that out, it's really up to you how you use it.
        Protected Sub PrintButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PrintButton.Click
            Dim pd As New Drawing.Printing.PrintDocument
            pd.PrinterSettings.PrinterName = "\\serverName\printerName"
            AddHandler pd.PrintPage, AddressOf PrintPage
        End Sub
        Private Sub PrintPage(ByVal sender As Object, ByVal ev As Drawing.Printing.PrintPageEventArgs)
            Dim leftMargin As Single = ev.MarginBounds.Left
            Dim topMargin As Single = ev.MarginBounds.Top
            Dim printFont = New Drawing.Font("Arial", 10)
            Dim sb As New StringBuilder
            sb.Append("Content goes Here")
            ev.Graphics.DrawString(sb.ToString(), printFont, Drawing.Brushes.Black, leftMargin, topMargin, New Drawing.StringFormat())
        End Sub

    Open in new window


    Author Comment


    Sorry, forgot to mention that we are using C#..could you please translate that?


    Best Regards,

    LVL 15

    Accepted Solution

    Sure, here is what it would look like in C#

    using System.Drawing.Printing;
    using System.Drawing;
    using System.Text;
    protected void PrintButton_Click(object sender, EventArgs e)
       PrintDocument pd = new PrintDocument();
       pd.PrinterSettings.PrinterName = "\\servername\printername";
       pd.PrintPage += new PrintPageEventHandler(this.PrintPage);
    protected void PrintPage(object sender, PrintPageEventArgs ev)
       Single leftMargin = ev.MarginBounds.Left;
       Single topMargin = ev.MarginBounds.Top;
       Font printFont = new Font("Arial", 10);
       StringBuilder sb = new StringBuilder();
       sb.Append("This is a test");
       ev.Graphics.DrawString(sb.ToString(), printFont, Brushes.Black, leftMargin, topMargin, new StringFormat());

    Open in new window


    Author Comment

    Hey.. thanks!

    I think this will work just right - since it directly tries to connect to the printer... but i dont have access to my printer right now.. il let you know once I try out again from office in the morning!

    This looks to have hit the nail! There was a small modification though, an extra slash was needed in that text.. mentioning it just for benefit of the readers!

    Best Regards,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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

    18 Experts available now in Live!

    Get 1:1 Help Now