4,816
社区成员
发帖
与我相关
我的任务
分享
#region 打印
private PrintDocument printDocument1 = new PrintDocument();//打印窗体
string ktitle = string.Empty;
//声明一个Stream对象的列表用来保存报表的输出数据
//LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
private List<Stream> m_streams = new List<Stream>();
//用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数。
private Stream CreateStream(string name, string fileNameExtension,
Encoding encoding, string mimeType, bool willSeek)
{
//如果需要将报表输出的数据保存为文件,请使用FileStream对象。
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
protected void btnPrint_Click1(object sender, EventArgs e)
{
try
{
//LocalReport report = new LocalReport();
//设置需要打印的报表的文件名称。
//report.ReportPath = @"Print/reportOrderPrint.rdlc";
//创建要打印的数据源
//ReportDataSource source = new ReportDataSource("OrderDataset", ObjectDataSource1);
//report.DataSources.Add(source);
//刷新报表中的需要呈现的数据
//report.Refresh();
LocalReport report = this.rvOrderPrint.LocalReport;
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>29.7cm</PageWidth>" +
" <PageHeight>21cm</PageHeight>" +
" <MarginTop>0.5cm</MarginTop>" +
" <MarginLeft>0.5cm</MarginLeft>" +
" <MarginRight>0.5cm</MarginRight>" +
" <MarginBottom>0.5cm</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
//将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
report.Render("Image", deviceInfo, CreateStream, out warnings);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
Print();//打印事件
}
//用来记录当前打印到第几页了
private int m_currentPageIndex;
private void Print()
{
m_currentPageIndex = 0;
if (m_streams == null || m_streams.Count == 0)
return;
//声明PrintDocument对象用于数据的打印
PrintDocument printDoc = new PrintDocument();
//指定需要使用的打印机的名称,使用空字符串""来指定默认打印机
//printDoc.PrinterSettings.PrinterName = "";
//判断指定的打印机是否可用
//if (!printDoc.PrinterSettings.IsValid)
//{
// MessageBox.Show("Can't find printer");
// return;
//}
//声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//执行打印操作,Print方法将触发PrintPage事件。
printDoc.DefaultPageSettings.PaperSize.RawKind = 9;
//横版打印
printDoc.DefaultPageSettings.Landscape = true;
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = printDoc;
if (DialogResult.OK == ppd.ShowDialog())
{
printDoc.Print(); //打印
}
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
//Metafile对象用来保存EMF或WMF格式的图形,
//我们在前面将报表的内容输出为EMF图形格式的数据流。
m_streams[m_currentPageIndex].Position = 0;
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.PageSettings.PaperSize.RawKind = 9;
//指定是否横向打印
ev.PageSettings.Landscape = true;
//这里的Graphics对象实际指向了打印机
ev.Graphics.DrawImage(pageImage, 0, 0);
m_streams[m_currentPageIndex].Close();
m_currentPageIndex++;
//设置是否需要继续打印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
#endregion
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<link href="../ymPrompt-4.0/skin/qq/ymPrompt.css" rel="stylesheet" type="text/css" />
<script src="../ymPrompt-4.0/ymPrompt.js" type="text/javascript"></script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="right1">
<img src="../images/shouye.gif" width="18" height="18" alt="" align="middle" />
当前位置:订单打印</div>
<div class="right2" align="center">
<div class="right2_tt" align="left">
<div style="width: 760px; min-height: 400px;">
<asp:Panel ID="panelPrint" runat="server">
<rsweb:ReportViewer ID="rvOrderPrint" runat="server" Width="760px" Font-Names="Verdana"
Font-Size="8pt" InteractiveDeviceInfos="(集合)" WaitMessageFont-Names="Verdana"
WaitMessageFont-Size="14pt">
<LocalReport ReportPath="Print\reportOrderPrint.rdlc">
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
</asp:Panel>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetData" TypeName="Web.Print.OrderDatasetTableAdapters.OrderTodayPrintTableAdapter">
<SelectParameters>
<asp:Parameter Name="rantid" Type="String" />
<asp:Parameter Name="ordernolist" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
<div class="qued" style="text-align: center; margin-top: 10px;">
<asp:ImageButton ID="imgBtnLogin" runat="server" ImageUrl="~/images/back2.gif" TabIndex="4"
PostBackUrl="~/resrant/NewLunchOrders.aspx" />
<asp:Button ID="btnPrint" runat="server" Text="打印" OnClick="btnPrint_Click1" Visible="false" />
<asp:ImageButton ID="imgbtnSaveIsPrint" runat="server"
ImageUrl="~/images/btnisprint.gif" onclick="imgbtnSaveIsPrint_Click" />
<asp:ImageButton ID="imgbtnSavePrintOrder" runat="server"
ImageUrl="~/images/btnprintorder.gif" onclick="imgbtnSavePrintOrder_Click" />
</div>
</div>
<div class="right3">
<img src="../images/right_b_03.jpg" alt="" />
</div>
</div>