Export (0) Print
Expand All
22 out of 125 rated this helpful - Rate this topic

Walkthrough: Printing a Local Report without Preview 

This walkthrough shows how to programmatically print a report without viewing it, using the LocalReport object and the CreateStreamCallback callback function.

You must have access to the sample report and data source. For more information, see Sample Data and Report for Print Walkthrough.

Perform the following steps to create a Microsoft Visual Studio Console application project.

  1. On the File menu, point to New, and then select Project.

  2. In the Project Types pane, choose Visual C# or Visual Basic and Windows.

  3. In the Templates pane, choose Console Application to create a Microsoft Console application.

  4. In the Name box, type the name of the project: PrintLocalReport.

  5. In the Location box, enter the directory in which you want to save your project, or click Browse to navigate to it. The project opens with the Code window showing the Program code file.

  1. From the Project menu, select Add Reference. The Add Reference dialog box will appear.

  2. From the list box displayed on the .NET tab, select System.Drawing, System.Windows.Forms, and Microsoft.Reporting.Winforms.

  1. From the Project menu, select Add Existing Item. The Add Existing Item dialog box will appear.

  2. Navigate to the folder where you saved report.rdlc and data.xml. Select both files.

  3. Click Add. The two files appear in Solution Explorer as part of the project.

  1. The Program code file should already be open for editing. If it is not, double-click on the Program.cs or Module1.vb file in the Solution Explorer window.

  2. Replace existing code in the Program file with the following code of your programming language choice.

    For Visual C#, use the following code.

    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 to
        //    save an image for each page of the report.
        private Stream CreateStream(string name,
          string fileNameExtension, Encoding encoding,
          string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(@"..\..\" + name +
               "." + fileNameExtension, FileMode.Create);
            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]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);
            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }
    
        private void Print()
        {
            const string printerName =
               "Microsoft Office Document Image Writer";
            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);
                MessageBox.Show(msg, "Print Error");
                return;
            }
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            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.DataSources.Add(
               new ReportDataSource("Sales", LoadSalesData()));
            Export(report);
            m_currentPageIndex = 0;
            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();
            }
        }
    }
    

    For Visual Basic, use the following code.

    Imports System.IO
    Imports System.Data
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Drawing.Printing
    Imports System.Collections.Generic
    Imports Microsoft.Reporting.WinForms
    
    Public Class Demo
        Implements IDisposable
    
        Private m_currentPageIndex As Integer
        Private m_streams As IList(Of Stream)
    
        Private Function LoadSalesData() As DataTable
            Dim dataSet As New DataSet()
            dataSet.ReadXml("..\..\data.xml")
            Return dataSet.Tables(0)
        End Function
    
        Private Function CreateStream(ByVal name As String, _
           ByVal fileNameExtension As String, _
           ByVal encoding As Encoding, ByVal mimeType As String, _
           ByVal willSeek As Boolean) As Stream
            Dim stream As Stream = _
                New FileStream("..\..\" + _
                 name + "." + fileNameExtension, FileMode.Create)
            m_streams.Add(stream)
            Return stream
        End Function
    
        Private Sub Export(ByVal report As LocalReport)
            Dim deviceInfo As String = _
              "<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>"
            Dim warnings() As Warning = Nothing
            m_streams = New List(Of Stream)()
            report.Render("Image", deviceInfo, AddressOf CreateStream, _
               warnings)
    
            Dim stream As Stream
            For Each stream In m_streams
                stream.Position = 0
            Next
        End Sub
    
        Private Sub PrintPage(ByVal sender As Object, _
        ByVal ev As PrintPageEventArgs)
            Dim pageImage As New Metafile(m_streams(m_currentPageIndex))
            ev.Graphics.DrawImage(pageImage, ev.PageBounds)
    
            m_currentPageIndex += 1
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count)
        End Sub
    
        Private Sub Print()
            Const printerName As String = _
              "Microsoft Office Document Image Writer"
    
            If m_streams Is Nothing Or m_streams.Count = 0 Then
                Return
            End If
    
            Dim printDoc As New PrintDocument()
            printDoc.PrinterSettings.PrinterName = printerName
            If Not printDoc.PrinterSettings.IsValid Then
                Dim msg As String = String.Format( _
                    "Can't find printer ""{0}"".", printerName)
                Console.WriteLine(msg)
                Return
            End If
            AddHandler printDoc.PrintPage, AddressOf PrintPage
            printDoc.Print()
        End Sub
    
        Private Sub Run()
            Dim report As LocalReport = New LocalReport()
            report.ReportPath = "..\..\Report.rdlc"
            report.DataSources.Add(New ReportDataSource("Sales", _
               LoadSalesData()))
    
            Export(report)
    
            m_currentPageIndex = 0
            Print()
        End Sub
    
        Public Overloads Sub Dispose() Implements IDisposable.Dispose
            If Not (m_streams Is Nothing) Then
                Dim stream As Stream
                For Each stream In m_streams
                    stream.Close()
                Next
                m_streams = Nothing
            End If
        End Sub
    
        Public Shared Sub Main(ByVal args As String())
            Using demo As Demo = New Demo()
                demo.Run()
            End Using
        End Sub
    End Class
    
  1. On the Build menu, click on Build Solution to build the application. As part of the build process, the report is compiled and any errors found (such as a syntax error in an expression used in the report) are added to the Task List.

  2. Press F5 to run the application.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.