• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

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

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.
0
shenoyroopesh
Asked:
shenoyroopesh
2 Solutions
 
Gautham JanardhanCommented:
you can use th javascript window.print() . this wil popup the windows print dialog to select th printer.
0
 
sunithnairCommented:
Try this as gauthampj pointed out
<input type="button" value="Print Page" onclick="javascript:window.print();">

Open in new window

0
 
aibusinesssolutionsCommented:
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
        pd.Print()
    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

0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
shenoyroopeshAuthor Commented:
Hi,

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


Thanks.


Best Regards,

Roopesh
0
 
aibusinesssolutionsCommented:
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);
   pd.Print();
}
 
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

0
 
shenoyroopeshAuthor Commented:
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,

Roopesh
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now