在ASP.net中打印RDLC报告,无需在客户端进行预览

时间:2013-08-05 14:20:15

标签: c# asp.net rdlc

我有一个RDLC报告,在Visual Studio中运行时可以在本地打印。当我上传 我的项目到服务器它不会打印。我正在做一些研究并了解到,当项目在服务器中时,它会尝试在服务器上打印,而不是在客户端计算机上打印。现在有人如何打印到客户机?

   private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,         string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }

    private void Export(LocalReport report)
    {
        string deviceInfo =
          "<DeviceInfo>" +
          "  <OutputFormat>EMF</OutputFormat>" +
          "  <PageWidth>8.5in</PageWidth>" +
          "  <PageHeight>11in</PageHeight>" +
          "  <MarginTop>0.01in</MarginTop>" +
          "  <MarginLeft>0.02in</MarginLeft>" +
          "  <MarginRight>0.25in</MarginRight>" +
          "  <MarginBottom>0.25in</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 PrintPage(object sender, PrintPageEventArgs ev)
    {
        Metafile pageImage = new Metafile(m_streams[currentPageIndex]);
        ev.Graphics.DrawImage(pageImage,ev.PageBounds);
        currentPageIndex++;
        ev.HasMorePages = (currentPageIndex < m_streams.Count);
    }


    private void Print_Ticket()
    {

        const string printerName = "HP DeskJet";
        if (m_streams == null || m_streams.Count == 0)
            return;
        PrintDocument printDoc = new PrintDocument();
        printDoc.PrinterSettings.PrinterName = printerName;
        if (!printDoc.PrinterSettings.IsValid)
        {
            string msg = String.Format("Can't find printer \"{0}\".", printerName);
            Console.WriteLine(msg);
            return;
        }
        printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
        printDoc.Print(); //Prints Document
    }
    public void WriteTicket(string ticket_number, string queue)
    {
        LocalReport report = new LocalReport();
        //Displays ticket letter and number in ticket
        report.ReportPath = "PrintTicket.rdlc";
        ReportParameter ticket_parameter = new ReportParameter();
        ticket_parameter.Name = "Ticket";
        ticket_parameter.Values.Add(ticket_number);
        report.SetParameters(new ReportParameter[] { ticket_parameter });

        //Displays date and time in ticket
        ReportParameter date = new ReportParameter();
        date.Name = "Date_Time";
        date.Values.Add(System.DateTime.Now.ToString());
        report.SetParameters(new ReportParameter[] { date });

        //Displays branch location in ticket
        ReportParameter location_parameter = new ReportParameter();
        location_parameter.Name = "Location";
        location_parameter.Values.Add(queue);
        report.SetParameters(new ReportParameter[] { location_parameter });

        Export(report);
        currentPageIndex = 0;
        Print_Ticket();
    }
    //Clears stream
    public new void Dispose()
    {
        if (m_streams != null)
        {
            foreach (Stream stream in m_streams)
                stream.Close();
        }
    }

1 个答案:

答案 0 :(得分:0)

我一直在我的应用中使用ReportViewer控件。这样就可以在客户端计算机上打印查看的报告。我想你可以使用这个控件做同样的事情,即使它是不可见的,如果这是你想要完成的。

如果这没有帮助。此链接可能有用。 http://msdn.microsoft.com/en-us/library/ms252091.aspx