WPF 打印操做之PrintDocument,WPF获取打印机列表,WPF指定打印机

1、WPF 打印操做之PrintDocument,WPF获取打印机列表,WPF指定打印机html

PrintDocumentapi

定义一个可重用的对象,当从Windows Forms应用程序进行打印时,该对象将输出发送到打印机this

 

一般,您将建立PrintDocument的实例,设置诸如DocumentNamePrinterSettings之类的属性,而后调用Print方法来启动打印过程。经过使用PrintPageEventArgsGraphics Graphics属性,在您指定要打印输出的地方处理PrintPage事件url

 

更多参考:spa

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument?view=dotnet-plat-ext-3.1.net

 

2、wpf获取系统可用打印机列表code

 

//获取打印机列表
StringCollection strList = PrinterSettings.InstalledPrinters;

 

 

使用打印机名称,指定特定的打印机进行打印。orm

PrintDocument pd = new PrintDocument(); pd.PrinterSettings.PrinterName = "Canon TS3100 series (副本 1)";

 

3、打印示例htm

Font printFont = null; StreamReader streamToPrint = null; private void Button_Click_1(object sender, RoutedEventArgs e) { try { streamToPrint = new StreamReader ("G:\\桌面\\新建文本文档.txt"); try { printFont = new Font("Arial", 10); PrintDocument pd = new PrintDocument(); pd.PrinterSettings.PrinterName = "Canon TS3100 series (副本 1)"; pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage); pd.Print(); } finally { streamToPrint.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } // The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev) { float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; // Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file.
    while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null)) { yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, System.Drawing.Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page.
    if (line != null) ev.HasMorePages = true; else ev.HasMorePages = false; }

 

 

更多:对象

WPF 打印操做之PrintDialog

WPF 皮肤之MathApps.Metro UI库

WPF RichTextBox滚动条自动滚动实例、文本自动滚动实例