private void btnPreview_Click(object sender, EventArgs e) { PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog(); //创建打印文档 PrintDocument printDocument1 = new PrintDocument(); //开始打印的时候 printDocument1.BeginPrint += new PrintEventHandler(printDocument1_BeginPrint); //输送打印数据 printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); //打印结束的时候 printDocument1.EndPrint += new PrintEventHandler(printDocument1_EndPrint); //设置纸张的大小,主要用于自定义纸张 System.Drawing.Printing.PageSettings pageSet = new System.Drawing.Printing.PageSettings(); pageSet.PaperSize = new PaperSize("First custom size", 1366, 768); printDocument1.DefaultPageSettings = pageSet; // printPreviewDialog.Document = printDocument1; printPreviewDialog.ShowDialog(); printPreviewDialog.Close(); } private void printDocument1_EndPrint(object sender, PrintEventArgs e) { //throw new NotImplementedException(); //打印结束之后进行的操作 } private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { //分页,分页数=选中节点数 HasMorePages 用来设置自动触发打印事件 //if (Count == 0) //{ // e.HasMorePages = false; //} //else //{ // e.HasMorePages = true; //} //当第二次加载的数据的时候,要把第一调用的画布,清除一遍,记得这是很重要的一句话哇!呜呜! //e.Graphics.Clear(Color.White); //绘制背景图像 //e.Graphics.DrawImage(panelPrint.BackgroundImage, 0, 0, 1366, 768); } private void printDocument1_BeginPrint(object sender, PrintEventArgs e) { //throw new NotImplementedException(); // 打印开始之前整理好数据源 } private void btnPrint_Click(object sender, EventArgs e) { PageSetupDialog pageSetupDialog1 = new PageSetupDialog(); //创建打印文档 PrintDocument printDocument1 = new PrintDocument(); //开始打印的时候 printDocument1.BeginPrint += new PrintEventHandler(printDocument1_BeginPrint); //输送打印数据 printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); //打印结束的时候 printDocument1.EndPrint += new PrintEventHandler(printDocument1_EndPrint); //设置纸张的大小,主要用于自定义纸张 System.Drawing.Printing.PageSettings pageSet = new System.Drawing.Printing.PageSettings(); pageSet.PaperSize = new PaperSize("First custom size", 1024, 768);//此参数调整纸张大小 printDocument1.DefaultPageSettings = pageSet; //将打印文档赋给打印组件 pageSetupDialog1.Document = printDocument1; //纸张及页边距设置 pageSetupDialog1.PageSettings.Margins = new Margins(0, 0, 0, 0); if (DialogResult.OK == pageSetupDialog1.ShowDialog()) { printDocument1.Print(); } printDocument1.Dispose(); //打印完毕后记得清空临时数据,释放内存,不管别的地方有没有清 }