C # implementation code for directly printing the report file (rdlc) without using reportviewer

Source: Internet
Author: User

This article tells you how to use the report Preview Control (reportviewer) instead of using reportviewer to directly print the implementation code of the report file (rdlc ), directly print the report content on the printer.

Generally, after a report is designed, it is loaded to the reprotviewer Control for preview and printing when the program runs. However, in some cases, we do not want to preview the report content, how can I print it directly on the printer? To implement the direct printing function, we need to use two objects provided by. net, localreport and printdocument. The localreport object is used to load a report file to generate an actual report and output the report in a known format. The printdocument object is used to send the output content of the localreport to the printer for printing. The specific implementation steps are as follows:

Step 1: declare a localreport object and load a report file (assuming we have designed a report file named printme. rdlc ).

   1: LocalReport report = new LocalReport();
2: // set the name of the report to be printed.
   3: report.ReportPath = @"c:\PrintMe.rdlc";
4: // create the data source to be printed
   5: ReportDataSource source = new ReportDataSource(SourceTalbe.TableName, SourceTalbe);
   6: report.DataSources.Add(source);
7: // refresh the data to be presented in the report
   8: report.Refresh();

Step 2: output the report content to a data stream in the specified format.

   1: string deviceInfo =
   2:   "<DeviceInfo>" +
   3:   "  <OutputFormat>EMF</OutputFormat>" +
   4:   "  <PageWidth>8.5in</PageWidth>" +
   5:   "  <PageHeight>11in</PageHeight>" +
   6:   "  <MarginTop>0.25in</MarginTop>" +
   7:   "  <MarginLeft>0.25in</MarginLeft>" +
   8:   "  <MarginRight>0.25in</MarginRight>" +
   9:   "  <MarginBottom>0.25in</MarginBottom>" +
  10:   "</DeviceInfo>";
  11: Warning[] warnings;
12: // output the report content to the stream provided by the createstream function in the format specified by deviceinfo.
  13: report.Render("Image", deviceInfo, CreateStream, out warnings);

Here, the render method of the localreport object is used to output the report content to the specified stream. The first parameter of the render method specifies the format of the output stream, which is specified as the image format (image format). The second parameter is a string in XML format, this parameter is used to describe the details of the output format. The third parameter is a callback function (createstreamcallback delegate type). You need to declare a function for this parameter, the render method outputs the report content to the instance of the stream object returned by this function. This function looks similar to the following statement.

1: // declare a list of stream objects to save the output data of the report.
2: // The render method of the localreport object will output the report to multiple stream objects by page.
   3: private List<Stream> m_streams;
4: // The function used to provide the stream object. It is the third parameter of the render method of the localreport object.
   5: private Stream CreateStream(string name, string fileNameExtension,
   6:   Encoding encoding, string mimeType, bool willSeek)
   7: {
8: // if you want to save the report output data as a file, use the filestream object.
   9:     Stream stream = new MemoryStream();
  10:     m_streams.Add(stream);
  11:     return stream;
  12: }

You can use the parameters of this function to perform more operations. For details, see createstreamcallback delegation.

The fourth parameter is used to output the warning information generated during report processing.

 

Step 3: Use the printdocument object to print data.

1: // used to record the pages currently printed
   2: private int m_currentPageIndex;
   3:  
   4: private void Print()
   5: {
   6:     m_currentPageIndex = 0;
   7:  
   8:     if (m_streams == null || m_streams.Count == 0)
   9:         return;
10: // declare the printdocument object for data printing
  11:     PrintDocument printDoc = new PrintDocument();
12: // specify the name of the printer to be used. Use an empty string "" to specify the default printer.
  13:     printDoc.PrinterSettings.PrinterName = "";
14: // determine whether the specified printer is available
  15:     if (!printDoc.PrinterSettings.IsValid)
  16:     {
  17:         MessageBox.Show("Can't find printer");
  18:         return;
  19:     }
20: // declare the printpage event of the printdocument object. The specific print operation must be handled in this event.
  21:     printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
22: // execute the print operation. The print method triggers the printpage event.
  23:     printDoc.Print();
  24: }

The specific printpage event handler is as follows:

   1: private void PrintPage(object sender, PrintPageEventArgs ev)
   2: {
3: // The Metafile object is used to save images in EMF or WMF Format,
4: // We output the report content as a data stream in the EMF graphic format.

M_streams [m_currentpageindex]. Position = 0;

5: Metafile pageimage = new Metafile (m_streams [m_currentpageindex]);

6: // specify whether to print horizontally
   7:     ev.PageSettings.Landscape = false;
8: // The graphics object actually points to the printer
   9:     ev.Graphics.DrawImage(pageImage, 0, 0);
  10:     m_streams[m_currentPageIndex].Close();
  11:     m_currentPageIndex++;
12: // set whether to continue printing
  13:     ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
  14: }

At this point, our report data has been printed. In this process, you can add your own printing logic as needed.

 

For information about local printers, see printersettings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.