7

I have the printer installed and working on an intranet server and I want to programmatically send "hello world" to that default printer. This seems like the simplest thing but I've been googling for a couple hours with no success. (note: I am developing asp.net mvc on the deployment machine itself which is running Windows 7)

I tried to translate an example from VB here into C# but it said "no printers are installed".

public void TestPrint()
{
    var x = new PrintDocument();
    x.PrintPage += new PrintPageEventHandler(PrintPage);
    x.Print();
}
private void PrintPage(Object sender, PrintPageEventArgs e)
{
    var textToPrint = "Hello world";
    var printFont = new Font("Courier New", 12);
    var leftMargin = e.MarginBounds.Left;
    var topMargin = e.MarginBounds.Top;
    e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin);
}

I had also tried a snippet from MSDN here but it said it did not recognize the printer name.

public void TestPrint(string msg)
{
    var server = new LocalPrintServer();
    var queue = LocalPrintServer.GetDefaultPrintQueue();

    // Call AddJob
    var job = queue.AddJob();

    // Write a Byte buffer to the JobStream and close the stream
    var stream = job.JobStream;
    var buffer = UnicodeEncoding.Unicode.GetBytes(msg);
    stream.Write(buffer, 0, buffer.Length);
    stream.Close();
}
Community
  • 1
  • 1
Benjamin
  • 3,134
  • 6
  • 36
  • 57

2 Answers2

10

Print "hello world" server-side in .NET

  1. Share the printer
  2. Create a PrintDocument object
  3. Reference the printer by name
  4. Add a method to provide content
  5. Print

Code

using System.Drawing;
using System.Drawing.Printing;

public void Print()
{
  var doc = new PrintDocument();
  doc.PrinterSettings.PrinterName = "\\\\deployment-machine-name\\share-name";
  doc.PrintPage += new PrintPageEventHandler(ProvideContent);
  doc.Print();
}
public void ProvideContent(object sender, PrintPageEventArgs e)
{
  e.Graphics.DrawString(
    "Hello world",
    new Font("Arial", 12),
    Brushes.Black,
    e.MarginBounds.Left,
    e.MarginBounds.Top);
}
Benjamin
  • 3,134
  • 6
  • 36
  • 57
1

First of all give an option to select a printer. Your whole requirement is already illustrated on Microsoft Support Site.

Have a look here.

A sample from there(In case someday the page is dead):-

private void print_Click(object sender, System.EventArgs e)
{
    string s = "Hello"; // device-dependent string, need a FormFeed?

    // Allow the user to select a printer.
    PrintDialog pd  = new PrintDialog();
    pd.PrinterSettings = new PrinterSettings();
    if( DialogResult.OK == pd.ShowDialog(this) )
    {
        // Send a printer-specific to the printer.
        RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);
    }
}
perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • Thanks, I'm checking it out. BTW I'm running Windows 7. – Benjamin Sep 01 '12 at 17:32
  • What is the `ShowDialog` part? I do not want to require user interaction for this. I am actually going to have it called from asp.net when I get it working. – Benjamin Sep 01 '12 at 17:35
  • If you fail, you can try unmanaged code manipulation in [C# for printers](http://support.microsoft.com/kb/138594) – perilbrain Sep 01 '12 at 17:36
  • remove if they are not useful.....http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.printdocument is an alternative to dialog – perilbrain Sep 01 '12 at 17:42
  • Strangely I am unable to use the `System.Window.Forms` namepsace even after adding the reference. – Benjamin Sep 01 '12 at 17:59
  • try adding right version, honestly I dont know much about asp.net and its compatibility with various assemblies – perilbrain Sep 01 '12 at 18:04
  • Thanks for the help anyway. I think I need to clarify the question. I'm using asp.net mvc and asp.net is apparently not compatible with the windows forms library. – Benjamin Sep 01 '12 at 18:06