Programmatically “hello world” default SERVER-side printer in ASP.NET MVC

后端 未结 2 1499
北恋
北恋 2020-12-15 13:08

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 thin

相关标签:
2条回答
  • 2020-12-15 13:32

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 13:34

    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);
    }
    
    0 讨论(0)
提交回复
热议问题