请问大虾们如何在Winform程序中实现没有预览的打印

解决方案 »

  1.   

    我是打印报表,用了reportViewer控件,怎么能不预览
      

  2.   

    PrintDocument存在一个问题,就是如何把报表放到document中打印
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Drawing.Printing;
    using Microsoft.Reporting.WinForms;namespace DLAFMS.RVP {
      public class ReportPrintImpl {
      private List<Stream> m_streams;
      private int m_currentPageIndex;
      public int CurrentPageIndex {
      get { return m_currentPageIndex; }
      set { m_currentPageIndex = value; }
      }
      private PrintDocument pDocument;
      public PrintDocument PDocument {
      get { return pDocument; }
      set { pDocument = value; }
      }  public ReportPrintImpl() {
      pDocument = new PrintDocument();
      pDocument.PrintPage += new PrintPageEventHandler(pDocument_PrintPage);
      }  /// <summary>
      /// 呈现报表
      /// </summary>
      /// <param name="report">报表</param>
      public void Export(LocalReport report) {
      StringBuilder deviceInfo = new StringBuilder();
      deviceInfo.Append("<DeviceInfo>")
      .Append(" <OutputFormat>EMF</OutputFormat>")
      .Append(" <PageWidth>8.5in</PageWidth>")
      .Append(" <PageHeight>11in</PageHeight>")
      .Append(" <MarginTop>0.1in</MarginTop>")
      .Append(" <MarginLeft>0.2in</MarginLeft>")
      .Append(" <MarginRight>0.1in</MarginRight>")
      .Append(" <MarginBottom>0.1in</MarginBottom>")
      .Append("</DeviceInfo>");  Warning[] warnings;
      m_streams = new List<Stream>();
      report.Render("Image", deviceInfo.ToString(), CreateStream, out warnings);  foreach (Stream stream in m_streams) {
      stream.Position = 0;
      }
      }  private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) {
      Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
      m_streams.Add(stream);
      return stream;
      }  private void pDocument_PrintPage(object sender, PrintPageEventArgs e) {
      Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
      e.Graphics.DrawImage(pageImage, 0, 0);
      m_currentPageIndex++;
      e.HasMorePages = (m_currentPageIndex < m_streams.Count);
      }
      }
    }
    调用如下所示:
    ReportForm frmReport = new ReportForm();//报表窗体
    ReportInitImpl report = new ReportInitImpl();//填充报表数据的类,换成你自己的
    ReportPrintImpl reportPrint = new ReportPrintImpl();//直接打印的类
    report.InitReportData(frmReport.rv_License, license);//将数据填充到报表中,换成你自己的
    frmReport.Show();
    frmReport.rv_License.SetDisplayMode(DisplayMode.PrintLayout);
    frmReport.rv_License.ZoomMode = ZoomMode.Percent;
    reportPrint.CurrentPageIndex = 0;
    reportPrint.Export(frmReport.rv_License.LocalReport);
    reportPrint.PDocument.Print();
    frmReport.Close(); 
      

  4.   

    class ReportPrintImpl 就是直接打印的处理类。
    reportViewer控件难道没有放在窗体中吗?
      

  5.   

      public void Print()
           {
               PrintDocument pd = new PrintDocument();
               //默认纸张设计属性
               pd.DefaultPageSettings.PaperSize = new PaperSize("StuffBusicInfo", 1024, 768);
               pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
               //使用预览对话框模拟打印效果
               PrintPreviewDialog ppd = new PrintPreviewDialog();
               ppd.Document = pd;
               ppd.ShowDialog();
           }