这个示例演示了如何进行一个最简单的打印预览工作,为此需要引入两个dll:System.Windows.Forms,System.Drawing
/// <summary>
/// 预览类
/// </summary>
public class PrintService
{
//实例化打印预览
System.Windows.Forms.PrintPreviewDialog ppd = new System.Windows.Forms.PrintPreviewDialog();
/// <summary>
/// 预览类
/// </summary>
public PrintService()
{
//打印文档
System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例
docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
//打印预览的打印文档设置为被打印文档
ppd.Document = docToPrint;
ppd.ShowDialog();
}
//设置打印机开始打印的事件处理函数
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString("Hello, world!", new System.Drawing.Font("Arial", 16, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 100, 100);
}
}