Print html document from Windows Service without print dialog

后端 未结 5 980
青春惊慌失措
青春惊慌失措 2020-11-30 05:53

I am using a windows service and i want to print a .html page when the service will start. I am using this code and it\'s printing well. But a print dialog box come, how do

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 06:51

    Here's another way to print without a print dialog. You create a PrintDialog object, initialize it and then call the Print() method.

    The function below is used to print a small 2"x0.75" barcode label. You'll need to figure out a way to get an Document object from the html file.

    public void PrintToPrinter(string printerName)
    {
        PrintDialog pd = new PrintDialog();
        pd.Document = userControl11.PrintDoc; // <--- Update this line with your doc
        pd.PrinterSettings.PrinterName = printerName;
        try
        {
                pd.Document.DocumentName = "My Label";
                pd.Document.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("2-.75", 200, 75);
                pd.Document.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
                //pd.PrinterSettings.Copies = (short)mNumCopies;
                pd.Document.PrinterSettings.Copies = (short) mNumCopies;
                pd.Document.Print();
        }
        catch
        {
            MessageBox.Show("INVALID PRINTER SPECIFIED");
        }
    }
    

提交回复
热议问题