473,385 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to set the printer in C# for a web page?

103 100+
I have a test web page and a windows form which call a printer and sends a print job to a printer that is on the network. I have been able to successfully do this from the windows form, however, I can figure out how to do it from an aspx page to the server's default printer. Any help would be greatly appreciated. Sample codes are below.

Windows Form:
Expand|Select|Wrap|Line Numbers
  1.   public partial class Form1 : Form
  2.   {
  3.     private const string TEMPLATE_DIRECTORY = @"C:\Program Files\Brother bPAC3 SDK\Templates\";    // Template file path
  4.     private const string TEMPLATE_SIMPLE = "NamePlate1.LBX";    // Template file name
  5.     private const string TEMPLATE_FRAME = "NamePlate2.LBX";        // Template file name
  6.     public Form1()
  7.     {
  8.       InitializeComponent();
  9.     }
  10.  
  11.     private void btnPrint_Click(object sender, EventArgs e)
  12.     {
  13.       try
  14.       {
  15.         string templatePath = TEMPLATE_DIRECTORY;
  16.         templatePath += TEMPLATE_SIMPLE;
  17.         MessageBox.Show("Template Directory and File Set");
  18.         bpac.DocumentClass doc = new DocumentClass();
  19.         doc.Open(templatePath);
  20.         MessageBox.Show("Doc Path Opened");
  21.         doc.GetObject("objCompany").Text = "Test Company";
  22.         doc.GetObject("objName").Text = "Test Name";
  23.         MessageBox.Show("Values Add to Template Objects");
  24.         doc.StartPrint("", PrintOptionConstants.bpoDefault);
  25.         MessageBox.Show("Print Started");
  26.         doc.PrintOut(1, PrintOptionConstants.bpoDefault);
  27.         MessageBox.Show("PrintOut");
  28.         doc.EndPrint();
  29.         MessageBox.Show("Print Ended");
  30.         doc.Close();
  31.       }
  32.       catch (Exception ex)
  33.       {
  34.         MessageBox.Show("Print Failed: " + ex);
  35.       }
  36.  
  37.     }
  38.   }
  39. }

ASPX Page:
Expand|Select|Wrap|Line Numbers
  1. protected void btnPrint_Click(object sender, EventArgs e)
  2.   {
  3.     Type clientType = this.GetType();
  4.     ClientScriptManager clientManager = Page.ClientScript;
  5.  
  6.     string theTemplate = Template_File_Path.ToString();
  7.     if (ddlTemplate.SelectedIndex == 0)
  8.     {
  9.       theTemplate += TEMPLATE_SIMPLE;
  10.     }
  11.     else if (ddlTemplate.SelectedIndex == 1)
  12.     {
  13.       theTemplate += TEMPLATE_FRAME;
  14.     }
  15.     //try
  16.     //{
  17.       bpac.DocumentClass doc = new DocumentClass();
  18.       clientManager.RegisterStartupScript(clientType, "Check1", "<Script>alert('" + theTemplate + "');</script>", false);
  19.       clientManager.RegisterStartupScript(clientType, "Check2", "<Script>alert('" + doc.Open(theTemplate) + "');</script>", false);
  20.       if (doc.Open(theTemplate) != false)
  21.       {
  22.         doc.GetObject("objCompany").Text = "McGladrey LLP Test";
  23.         doc.GetObject("objName").Text = txtCompany.Text;
  24.             if(doc.Printer.IsPrinterOnline(Printer_Device))
  25. {
  26. clientManager.RegisterStartupScript(clientType, "Printer Check", "<Script>alert('Yes Printer is Online');</script>", false);
  27. }
  28. else
  29. {
  30. clientManager.RegisterStartupScript(clientType, "Printer Check", "<Script>alert('No Printer is not Online');</script>", false);
  31. }
  32.  
  33.  
  34.         doc.SetPrinter(Printer_Device, true);
  35.        clientManager.RegisterStartupScript(clientType, "Check3", "<Script>alert('" + doc.SetPrinter(Printer_Device, true) + "');</script>", 
  36.  
  37. false);
  38.           doc.StartPrint("", PrintOptionConstants.bpoDefault);
  39.     doc.PrintOut(1, PrintOptionConstants.bpoDefault);
  40.         bool res= doc.PrintOut(1, PrintOptionConstants.bpoDefault);
  41.  
  42.     clientManager.RegisterStartupScript(clientType, "Check12", "<Script>alert('" + res + "');</script>", false);
  43.         doc.EndPrint();
  44.         doc.Close();
  45.  
  46.           clientManager.RegisterStartupScript(clientType, "Print Error1", "<Script>alert('The Print Succeeded');</script>", false);
  47.       }
  48.       else
  49.       {
  50.  
  51.  
  52.         clientManager.RegisterStartupScript(clientType, "Print Error", "<Script>alert('The Print Failed');</script>", false);
  53.       }
  54.  
  55.   }
  56. }
Sep 20 '12 #1
1 4425
Brian Connelly
103 100+
I am tryinh to use the printer connected to the server not the client printer connection.
Sep 22 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Patrick Herb | last post by:
Hello, I'm trying to print the content of a RichTextBox from my VB 6 app. What I want is that the CommonDialog shows up, the user selects a printer and the content of the RichTextBox prints to...
0
by: Mike M. | last post by:
I'm trying to print the contents of a Flexgrid using printer.print. Everything works fine unless the number of entries in the grid cause it to not fit on a single 8.5x11 page anymore. The printer...
3
by: NWx | last post by:
Hi, I have a web browser control integrated into a form, which display a HTM page I generate in my program And I want to be able to print that web page (it is a actually a report) But I want it...
1
by: Scanner2001 | last post by:
I am trying to insert a printer page break inside of a form. I do not seem to be able to do this. My form contains 3 asp panels. Each of the panels have dynamically built content, and they are...
1
by: Kinetic - Paul G | last post by:
Hi All Hope someone can help out . . . I have a system that worked fine in A97 .and Win 98 with NT server . . . 10 PCs running a front end MDB and usual backend MDB with tables on the server....
2
by: jess | last post by:
Hi All, I am using the PrtDevMode property of a report open in design view to try and flip the orientation of the report between Portrait and Landscape in a user control box before the report is...
1
by: Karthic | last post by:
When i right click on the .rpt file in the VS 2003, i see a property printer setting. It says "No printer" on the top and there is option to select printer and paper settings etc.. I want to...
0
by: sameer | last post by:
Hi all, i have an ASP.Net page where the user will enters his information and when he hits on submit ,it prints on an pre-printed page ( like a bill of sale or any other application forms) Now my...
7
by: John | last post by:
Hi How can I change the printer page layout to landscape via code? Basically I would like to avoid having the user set the layout every time manually. Thanks Regards
1
by: sravan kumar | last post by:
i need java script code for control the printer page settings from browser.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.