Print html document from Windows Service without print dialog

后端 未结 5 967
青春惊慌失措
青春惊慌失措 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:32

    You can use the PrintDocument class in the System.Drawing.Printing namespace to give you more control over the printing, see here for more info.

    For example you can do the following:

    using (PrintDocument doc = new PrintDocument())
    {
        doc.PrintPage += this.Doc_PrintPage;
        doc.DefaultPageSettings.Landscape = true;
        doc.DocumentName = fileNameOfYourDocument;
        doc.Print();
    }
    

    Then a function is raised for each page to be printed and you are given a Graphics area to draw to

    private void Doc_PrintPage(object sender, PrintPageEventArgs ev)
    {
        ....
        ev.Graphics.DrawImage(image, x, y, newWidth, newHeight);
    }
    

    This does require you handle the actual drawing on the text/image to the page, see here for more info.

提交回复
热议问题