c#直接横向打印LocalReport

环境说明:VS2012,打印机型号:Zebra  ZP Series-200 dpi

 

申明变量:

private int m_currentPageIndex;
private List<Stream> m_streams;

 

private PaperSize _paperSize;

private ReportPageSettings _reportPageSettings;

打印按钮代码:

Public void Print(PaperSize paperSize,ReportPageSettings settings,string printerName)
{

_paperSize= paperSize;

_reportPageSettings=settings;
RenderReport();
m_currentPageIndex = 0;
Print(printerName);
}

 

private void RenderReport()
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
string.Format(" <PageWidth>{0}in</PageWidth>", _paperSize.Height/100) +
string.Format(" <PageHeight>{0}in</PageHeight>", _paperSize.Width/100) +
string.Format(" <MarginTop>0in</MarginTop>", _reportPageSettings.Margins.Top / 100) +
string.Format(" <MarginLeft>0in</MarginLeft>", _reportPageSettings.Margins.Left / 100) +
string.Format(" <MarginRight>0in</MarginRight>", _reportPageSettings.Margins.Right / 100) +
string.Format(" <MarginBottom>0in</MarginBottom>", _reportPageSettings.Margins.Bottom / 100) +
"</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
//将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
this.rptViewer.LocalReport.Render("Image", deviceInfo, 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)
{
//如果需要将报表输出的数据保存为文件,请使用FileStream对象。
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}

 

private void Print(string printName)
{
if (m_streams == null || m_streams.Count == 0)
return;

//声明PrintDocument对象用于数据的打印
PrintDocument printDoc = new PrintDocument();

//指定需要使用的打印机的名称,使用空字符串""来指定默认打印机
printDoc.PrinterSettings.PrinterName = printName;
printDoc.DefaultPageSettings.Landscape = true;
printDoc.DefaultPageSettings.Margins = _reportPageSettings.Margins;
printDoc.DefaultPageSettings.PaperSize = _paperSize;
//判断指定的打印机是否可用
if (!printDoc.PrinterSettings.IsValid)
{

  throw new InvalidPrinterException(_reportPageSettings);

  return;

}

//声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.EndPrint += printDoc_EndPrint;
//执行打印操作,Print方法将触发PrintPage事件。
printDoc.Print();
}

 

private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);

//这里的Graphics对象实际指向了打印机
ev.Graphics.DrawImage(pageImage,ev.PageBounds);
//pageImage.Save(string.Format("{0}.jpg", m_currentPageIndex));

m_currentPageIndex++;
//设置是否需要继续打印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}

private void printDoc_EndPrint(object sender, PrintEventArgs e)
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();

m_streams = null;
}
}

 

你可能感兴趣的:(local)