#region public static
///
/// 获取指定ReportViewer总物理页数
/// guog2009-05-06新增
///
/// ReportViewer控件
/// 总物理页数,默认值为0
public static int GetTotalPage(ReportViewer reportViewer)
{
int result = 0;
//设置为打印预览模式
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
//记录当前页
int currentPage = reportViewer.CurrentPage;
if (reportViewer.LocalReport.GetTotalPages() > 0)
{//自带的GetTotalPages()方法返回的是逻辑页面总数,所以这里重新处理
while (true)
{
try
{
reportViewer.CurrentPage += 1;
}
catch
{
reportViewer.CurrentPage -= 1;
result = reportViewer.CurrentPage;
break;
}
finally
{
//指定到原始位置
reportViewer.CurrentPage = currentPage;
}
}
}
return result;
}
#endregion public static
打印代码:
#region 打印
#region 判断是否为横向
///
/// 判断是否为横向
///
/// Rdlc文件流
///
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文件
///
/// 提供 Stream 对象以进行呈现的 CreateStreamCallback 委托指向的方法
/// 这里为将报表的每一个页面作为一个EMF图片存放,通常用于报表呈现
///
/// 流的名称
/// 创建文件流时要使用的文件扩展名
/// 指定流的字符编码的 Encoding 枚举器值。如果流不包含字符,则此值可能为 null。
/// 一个包含流的 MIME 类型的 string
/// 指示流是否需要支持查找的 Boolean 值。如果值为 false,则流将为只前推,并将按其创建顺序发送到块区中的客户端。如果值为 true,则流可以任何顺序写入。
/// ReportViewer 控件可以写入数据的 Stream 对象
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;
}
///
/// 导出报表的每一个页面到一个EMF文件
///
/// ReportViewer.LocalReport
/// 页面设置
private void Export(LocalReport report,PageSettings pageSettings)
{
StringBuilder sbDeviceInfo = new StringBuilder();
if (pageSettings != null)
{
sbDeviceInfo.AppendLine("");
sbDeviceInfo.AppendLine(" EMF ");
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.PaperSize.Width)));
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.PaperSize.Height)));
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.Margins.Top)));
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.Margins.Left)));
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.Margins.Right)));
sbDeviceInfo.AppendLine(String.Format(" {0}cm ", FromInchToCM(pageSettings.Margins.Bottom)));
sbDeviceInfo.AppendLine(" ");
}
else
{
string deviceInfo =
"" +
" EMF " +
" 21cm " +
" 29.7cm " +
" 2.5cm " +
" 2.5cm " +
" 2.5cm " +
" 2.5cm " +
" ";
sbDeviceInfo.AppendLine(deviceInfo);
}
Warning[] warnings;
m_streams = new List();
report.Render("Image", sbDeviceInfo.ToString(), new CreateStreamCallback(CreateStream), out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
///
/// 当前页打印的输出
///
///
///
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);
}
///
/// 打印开始
///
///
///
private void BeginPrint(object sender, PrintEventArgs e)
{
this.btnPrint.Enabled = false;
}
///
/// 打印结束
///
///
///
private void EndPrint(object sender, PrintEventArgs e)
{
this.btnPrint.Enabled = true;
}
调用打印代码: