Printing Directly From ASP.NET

This is clearly something you wouldn’t often do, but I have a project where the server is going to be a local machine in a remote and temporary jobsite location and the client needs the server to print directly to a special label printer.

Here is some code that accomplishes this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing.Printing;
using System.Drawing;

namespace TestingPrinting.Controllers
{
public class HomeController : Controller
{
public ActionResult Print()
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.Margins.Left = 30;
pd.DefaultPageSettings.Margins.Top = 10;
pd.DefaultPageSettings.Margins.Right = 10;
pd.DefaultPageSettings.Margins.Bottom = 10;
pd.OriginAtMargins = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Set the printer name.
//pd.PrinterSettings.PrinterName = "\\NS5\hpoffice

pd.PrinterSettings.PrinterName = "HP LaserJet 200 color M251 PCL6 Class Driver";
pd.Print();

return View();
}

void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Font printFont = new Font("3 of 9 Barcode", 18);
Font printFont1 = new Font("Times New Roman", 11, FontStyle.Bold);
SolidBrush br = new SolidBrush(Color.Black);
ev.Graphics.DrawString("hello", printFont, br, 10, 65);
ev.Graphics.DrawString("world", printFont1, br, 10, 85);
}
}
}

Advertisements

One thought on “Printing Directly From ASP.NET

  1. Dear ChrisMay, it takes almost a week i’m looking for this kind of program which could directly print… tq for posting this…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s