Print the local report without previewing

Source: Internet
Author: User
Using System; Using System. IO; Using System. Data; Using System. text; Using System. Drawing. imaging; Using System. Drawing. printing; Using System. Collections. Generic; Using System. Windows. forms; Using Microsoft. Reporting. winforms; Public   Class Demo: idisposable { Private  Int M_currentpageindex; Private Ilist <stream> m_streams; Private Datatable loadsalesdata (){ // Create a new dataset and read sales data file          // Data. xml into the first datatable. Dataset dataset = New Dataset (); dataset. readxml ( @ ".. \ Data. xml" ); Return Dataset. Tables [0];} // Routine to provide to the report Renderer, In order     // Save an image for each page of the report.      Private Stream createstream ( String Name, String Filenameextension, encoding, String Mimetype, Bool Willseek) {stream = New Memorystream (); m_streams.add (Stream ); Return Stream ;} // Export the given report as an EMF (Enhanced Metafile) file.      Private   Void Export (localreport Report ){ String Deviceinfo = @ "<Deviceinfo> <outputformat> EMF </outputformat> <pagewidth> 8.5in </pagewidth> <pageheight> 11in </pageheight> <margintop> 0.25in </margintop> <marginleft> 0.25in </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 ;} // Handler for printpageevents      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. hardmargds, 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 Print (){If (M_streams = Null | M_streams.count = 0) Throw   New Exception ( "Error: No stream to print ." ); Printdocument printdoc = New Printdocument (); If (! Printdoc. printersettings. isvalid ){ Throw   New Exception ( "Error: cannot find the default printer ." );} Else {Printdoc. printpage + = New Printpageeventhandler (printpage); m_currentpageindex = 0; printdoc. Print ();}} // Create a local report for report. rdlc, load the data,      // Export the report to an. EMF file, and print it.      Private   Void Run () {localreport report = New Localreport (); report. reportpath = @ ".. \ Report. rdlc" ; Report. CES. Add ( New Reportdatasource ( "Sales" , Loadsalesdata (); export (report); print ();} Public   Void Dispose (){ If (M_streams! = Null ){ Foreach (Stream stream In M_streams) stream. Close (); m_streams = Null ;}} Public   Static   Void Main ( String [] ARGs ){Using (Demo demo = New Demo () {demo. Run ();}}}

Article Source: http://msdn.microsoft.com/zh-cn/library/ms252091 (V = vs.100). aspx

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.