如何使用reportviewer打印rdlc报告
问题描述:
hello guys can any help如何使用controll按钮打印我的收据?如何使用reportviewer打印rdlc报告
这是我的屏幕截图[1]” https://ibb.co/kfk8SF
如果我点击‘保存打印’按钮预览对话框出现 我要的是自动打印,我想通过点击报告只有该按钮并没有对话框出现
答
你可以先得到默认的打印机名称(开题报告查看器前):
System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
defaultPrinterName = settings.PrinterName;
打印时,指定您的打印机名称的PrintDocument
PrinterName
属性:
LocalReport rep = new LocalReport();
//set your data and parameters here
//...
rep.Refresh();
ExportLandscape(rep);
PrintDocument printDoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = defaultPrinterName;
printDoc.PrinterSettings = ps;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
的事件来处理实际的打印:
//this has to declared somewhere at the "top":
private IList<Stream> m_streams;
private int m_currentPageIndex;
private void PrintPage(object sender, PrintPageEventArgs ev) {
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
这里是创建打印文档的代码:
private void ExportPortrait(LocalReport report) {
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.5in</MarginTop>
<MarginLeft>0.5in</MarginLeft>
<MarginRight>0.5in</MarginRight>
<MarginBottom>0.5in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void ExportLandscape(LocalReport report) {
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>11in</PageWidth>
<PageHeight>8.5in</PageHeight>
<MarginTop>0.5in</MarginTop>
<MarginLeft>0.5in</MarginLeft>
<MarginRight>0.5in</MarginRight>
<MarginBottom>0.5in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.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) {
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
你可以使用像this这样的代码转换为vb.net是必要的。