using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Diagnostics;
namespace PrintTest
{
public class ReportPrint
{
private int m_currentPageIndex;
private IList<Stream> m_streams;
/// <summary>
/// 创建流
/// </summary>
/// <param name="name"></param>
/// <param name="fileNameExtension"></param>
/// <param name="encoding"></param>
/// <param name="mimeType"></param>
/// <param name="willSeek"></param>
/// <returns></returns>
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;
}
/// <summary>
///
/// </summary>
/// <param name="report"></param>
private void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
//" <PageWidth>8.5in</PageWidth>" +
//" <PageHeight>11in</PageHeight>" +
//" <MarginTop>0.25in</MarginTop>" +
//" <MarginLeft>0.25in</MarginLeft>" +
//" <MarginRight>0.25in</MarginRight>" +
//" <MarginBottom>0.25in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
try
{
report.Render("Image", deviceInfo, CreateStream, out warnings);
}
catch (Exception ex)
{
Exception innerEx = ex.InnerException;//get the InnerException
while (innerEx != null)
{
MessageBox.Show(innerEx.ToString());
innerEx = innerEx.InnerException;
}
throw innerEx;
}
foreach (Stream stream in m_streams)
stream.Position = 0;
}
/// <summary>
/// Print the event handler function
/// </summary>
/// <param name="sender"></param>
/// <param name="ev"></param>
private void PrintPage(object sender, PrintPageEventArgs ev)
{
//<<<EDT
//Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
//ev.Graphics.DrawImage(pageImage, 0, 0);
//m_currentPageIndex++;
//ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
//>>>EDT
// 画面解像度(dpi)を取得
Control ctrl = new Control();
Graphics g = ctrl.CreateGraphics();
float DisplayDpiX = g.DpiX;
float DisplayDpiY = g.DpiY;
// 印刷対象を取得
PrintDocument p = (PrintDocument)sender;
int DisplayPixelX = 0;
int DisplayPixelY = 0;
if (p.DefaultPageSettings.Landscape == true)
{
// 画面のピクセルを取得
//横向きの帳票
DisplayPixelX = (int)(297 * DisplayDpiX / 25.4 + 0.5);
DisplayPixelY = (int)(210 * DisplayDpiY / 25.4 + 0.5);
}
else
{
//縦向きの帳票
DisplayPixelX = (int)(210 * DisplayDpiX / 25.4 + 0.5);
DisplayPixelY = (int)(297 * DisplayDpiY / 25.4 + 0.5);
}
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, 0, 0, DisplayPixelX, DisplayPixelY);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
/// <summary>
/// Print dialog box to configure
/// </summary>
private void Print()
{
//const string printerName = "";//Printer Name 如果不指定打印机,则默认是当前系统的默认打印机
if (m_streams == null || m_streams.Count == 0)
{
return;
}
PrintDocument printDoc = new PrintDocument();
//printDoc.PrinterSettings.PrinterName = printerName; //Specify the printer name
//printDoc.PrinterSettings.PaperSizes.
if (!printDoc.PrinterSettings.IsValid)
{
string msg = "Can't find printer!";
Debug.WriteLine(msg);
return;
}
//Print the event handler function
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//Show the Print Dialog Box
PrintDialog printDia = new PrintDialog();
printDia.AllowSomePages = true;
printDia.ShowHelp = true;
printDia.Document = printDoc;
DialogResult result = printDia.ShowDialog();
if (result == DialogResult.OK)
{
printDoc.Print(); //Print
}
}
/// <summary>
/// 执行打印(竖向)
/// </summary>
/// <param name="dDataTable">数据源(Datatable或list等)</param>
/// <param name="RDLCName">rdlc名称</param>
/// <param name="parameters">参数集合</param>
public void Run(DataTable dDataTable, string RDLCName, ReportParameter[] parameters)
{
try
{
LocalReport report = new LocalReport();
report.ReportPath = Application.StartupPath + "//" + RDLCName + ".rdlc";//Loading path report
report.DataSources.Add(new ReportDataSource(dDataTable.TableName, dDataTable));//Add Data Source
if (parameters != null)
{
report.SetParameters(parameters);
}
report.Refresh();
Export(report);
m_currentPageIndex = 0;
//run
Print();
Dispose();
}
catch (Exception e)
{
//Debug.Print(e.InnerException.Message);
throw e;
}
}
/// <summary>
/// 执行打印(横向)
/// </summary>
/// <param name="dDataTable">数据源(Datatable或list等)</param>
/// <param name="RDLCName">rdlc名称</param>
/// <param name="parameters">参数集合</param>
/// <param name="isLandscape">横向打印</param>
public void Run(DataTable dDataTable, string RDLCName, ReportParameter[] parameters, bool isLandscape)
{
try
{
LocalReport report = new LocalReport();
report.ReportPath = Application.StartupPath + "//" + RDLCName + ".rdlc";//Loading path report
report.DataSources.Add(new ReportDataSource(dDataTable.TableName, dDataTable));//Add Data Source
if (parameters != null)
{
report.SetParameters(parameters);
}
report.Refresh();
Export(report);
m_currentPageIndex = 0;
//run
Print(isLandscape);
Dispose();
}
catch (Exception e)
{
//Debug.Print(e.InnerException.Message);
throw e;
}
}
/// <summary>
/// </summary>
/// <param name="isLandscape">横向设置</param>
private void Print(bool isLandscape)
{
if (m_streams == null || m_streams.Count == 0)
{
return;
}
PrintDocument printDoc = new PrintDocument();
//printDoc.PrinterSettings.PrinterName = printerName; //Specify the printer name
//printDoc.PrinterSettings.PaperSizes.
if (!printDoc.PrinterSettings.IsValid)
{
string msg = "Can't find printer!";
Debug.WriteLine(msg);
return;
}
printDoc.DefaultPageSettings.Landscape = isLandscape;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
/// <summary>
/// 释放buffer
/// </summary>
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
}
***************************************************************
以下不属于打印类内容
//添加并引用命名空间
using Microsoft.Reporting.WinForms;
/// <summary>
/// 给报表参数赋值(前提是RDLC报表中有这些参数)
/// </summary>
/// <returns></returns>
private ReportParameter[] SetReportParameters()
{
string TTOrder = "";
string TTOrderDate = "";
string TTCustomerID = "";
string TTCustomerName = "";
string TTDirectSendName = "";
string TTSendDate = "";
string TTDayt = "";
string TTSaffName = "";
string TTArrivePlaceCode = "";
string TTArrivePlaceName = "";
string TTUseCartName = "";
string TTPostCartKind = "";
string TTMemo = "";
ReportParameter[] parameters ={
new ReportParameter("TTOrder", TTOrder,true),
new ReportParameter("TTOrderDate", TTOrderDate,true),
new ReportParameter("TTSaffName", TTSaffName,true),
new ReportParameter("TTCustomerID", TTCustomerID,true),
new ReportParameter("TTCustomerName", TTCustomerName,true),
new ReportParameter("TTDirectSendName", TTDirectSendName,true),
new ReportParameter("TTSendDate", TTSendDate,true),
new ReportParameter("TTDay", TTDay,true),
new ReportParameter("TTArrivePlaceCode", TTArrivePlaceCode,true),
new ReportParameter("TTArrivePlaceName", TTArrivePlaceName,true),
new ReportParameter("TTUseCartName", TTUseCartName,true),
new ReportParameter("TTPostCartKind", TTPostCartKind,true),
new ReportParameter("TTMemo", TTMemo,true)
};
return parameters;
}