代码
#region 打印
#region 判断是否为横向
/// <summary>
/// 判断是否为横向
/// </summary>
/// <param name="stm">Rdlc文件流</param>
/// <returns></returns>
private bool IsLandSapces(Stream stm)
{
string strPageWidth = "";
string strPageHeight = "";
XmlReader xmlReader = XmlReader.Create(stm);
if (xmlReader.ReadToFollowing("PageWidth"))
{
strPageWidth = xmlReader.ReadElementString("PageWidth");
}
xmlReader.Close();
return strPageWidth == "29.7cm";
}
#endregion 读取XML文件
/// <summary>
/// 提供 Stream 对象以进行呈现的 CreateStreamCallback 委托指向的方法
/// 这里为将报表的每一个页面作为一个EMF图片存放,通常用于报表呈现
/// </summary>
/// <param name="name">流的名称</param>
/// <param name="fileNameExtension">创建文件流时要使用的文件扩展名</param>
/// <param name="encoding">指定流的字符编码的 Encoding 枚举器值。如果流不包含字符,则此值可能为 null。</param>
/// <param name="mimeType">一个包含流的 MIME 类型的 string</param>
/// <param name="willSeek">指示流是否需要支持查找的 Boolean 值。如果值为 false,则流将为只前推,并将按其创建顺序发送到块区中的客户端。如果值为 true,则流可以任何顺序写入。</param>
/// <returns>ReportViewer 控件可以写入数据的 Stream 对象</returns>
private Stream CreateStream(string name,string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new FileStream(Path.GetTempPath() + name +"." + fileNameExtension, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
//Stream stream = new FileStream(Path.GetTempFileName(), FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite,8000,true);
m_streams.Add(stream);
return stream;
}
/// <summary>
/// 导出报表的每一个页面到一个EMF文件
/// </summary>
/// <param name="report">ReportViewer.LocalReport</param>
/// <param name="pageSettings">页面设置</param>
private void Export(LocalReport report,PageSettings pageSettings)
{
StringBuilder sbDeviceInfo = new StringBuilder();
if (pageSettings != null)
{
sbDeviceInfo.AppendLine("<DeviceInfo>");
sbDeviceInfo.AppendLine(" <OutputFormat>EMF</OutputFormat>");
sbDeviceInfo.AppendLine(String.Format(" <PageWidth>{0}cm</PageWidth>", FromInchToCM(pageSettings.PaperSize.Width)));
sbDeviceInfo.AppendLine(String.Format(" <PageHeight>{0}cm</PageHeight>", FromInchToCM(pageSettings.PaperSize.Height)));
sbDeviceInfo.AppendLine(String.Format(" <MarginTop>{0}cm</MarginTop>", FromInchToCM(pageSettings.Margins.Top)));
sbDeviceInfo.AppendLine(String.Format(" <MarginLeft>{0}cm</MarginLeft>", FromInchToCM(pageSettings.Margins.Left)));
sbDeviceInfo.AppendLine(String.Format(" <MarginRight>{0}cm</MarginRight>", FromInchToCM(pageSettings.Margins.Right)));
sbDeviceInfo.AppendLine(String.Format(" <MarginBottom>{0}cm</MarginBottom>", FromInchToCM(pageSettings.Margins.Bottom)));
sbDeviceInfo.AppendLine("</DeviceInfo>");
}
else
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>21cm</PageWidth>" +
" <PageHeight>29.7cm</PageHeight>" +
" <MarginTop>2.5cm</MarginTop>" +
" <MarginLeft>2.5cm</MarginLeft>" +
" <MarginRight>2.5cm</MarginRight>" +
" <MarginBottom>2.5cm</MarginBottom>" +
"</DeviceInfo>";
sbDeviceInfo.AppendLine(deviceInfo);
}
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", sbDeviceInfo.ToString(), new CreateStreamCallback(CreateStream), out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
/// <summary>
/// 当前页打印的输出
/// </summary>
/// <param name="sender"></param>
/// <param name="ev"></param>
private void PrintPage(object sender, PrintPageEventArgs ev)
{
if (this.m_duplex)
{//双面打印
#region 双面打印
//获取每份页数
int pagePerCopy = GetPageCountPerCopy(this.m_streams.Count, this.m_chkedCount);
if (this.m_currentPageIndex > 0 && (this.m_currentPageIndex + 1) % pagePerCopy == 1 && this.m_isBlank && pagePerCopy % 2 != 0)
{//输出空白页
//当前页面不是第一页,且当前页面为此份报表第一页,且每份报表页数为奇数
Bitmap emptyImage = new Bitmap(10, 10);
if (ev.PageSettings.Landscape)
{
ev.Graphics.DrawImage(emptyImage, new Rectangle(0, 0, ev.PageBounds.Height, ev.PageBounds.Width));
}
else
{
ev.Graphics.DrawImage(emptyImage, ev.PageBounds);
}
//标记已经打印空白页
this.m_isBlank = false;
}
else
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
if (ev.PageSettings.Landscape)
{
ev.Graphics.DrawImage(pageImage, new Rectangle(0, 0, ev.PageBounds.Height, ev.PageBounds.Width));
}
else
{
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
}
m_currentPageIndex++;
//标记等待打印下一个空白页
this.m_isBlank = true;
}
#endregion 双面打印
}
else
{//单面打印
#region 单面打印
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
if (ev.PageSettings.Landscape)
{
ev.Graphics.DrawImage(pageImage, new Rectangle(0, 0, ev.PageBounds.Height, ev.PageBounds.Width));
}
else
{
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
}
m_currentPageIndex++;
#endregion 单面打印
}
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
/// <summary>
/// 打印开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginPrint(object sender, PrintEventArgs e)
{
this.btnPrint.Enabled = false;
}
/// <summary>
/// 打印结束
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EndPrint(object sender, PrintEventArgs e)
{
this.btnPrint.Enabled = true;
}