title: Printing RDLC Report Without Preview and Allowing User to Select a Page Range to Print in Print Dialog
body: <p>I have a rdlc report and I give the user the option of previewing the report in reportviewer or to print the report without preview. In reportviewer the user can seelect a page range to print when printing from the viewer. When printing without the viewer, even if the user selects a page range in the print dialog, the entire report prints. Is it possible to be able to select a page range to print in the print dialog when printing without reportviewer?</p>
  
  <p>In the window form I have a print button with a click event:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
     var message = "Do you wish to preview the report before printing?";
     var title = "Preview Report";
     DialogResult dg = MessageBox.Show(message, title, 
     MessageBoxButtons.YesNo);
          if (dg == DialogResult.Yes)
          {
  
              MainBatchListPreview mbp = new MainBatchListPreview();
  
              mbp.Show();
          }
          else if (dg == DialogResult.No)
          {
              LocalReport report = new LocalReport();
              report.ReportEmbeddedResource = 
              "Toolbar.Reports.MainBatchList.rdlc";
              report.DataSources.Add(new ReportDataSource("DataSet1", 
              dataGridView1.DataSource));
  
              report.PrintToPrinter();
          }
      }
  </code></pre>
  
  <p>Then I use the following Class to print without the report viewer:</p>
  
  <pre><code>namespace Toolbar
  {
  public static class PrintWithoutReportViewer
  {
      private static int m_CurrentPageIndex;
      private static IList&lt;Stream&gt; m_Streams;
      private static PageSettings m_PageSettings;
  
  
  
      public static Stream CreateStream(string name, string fileNameExtension, 
      Encoding encoding, string mimeType, bool willSeek)
      {
          Stream stream = new MemoryStream();
          m_Streams.Add(stream);
          return stream;
      }
  
  
  
      public static void Export(LocalReport report, bool print = true)
      {
          PaperSize paperSize = m_PageSettings.PaperSize;
          Margins margins = m_PageSettings.Margins;
  
          // The device info string defines the page range to print as well as the size of the page.
          // A start and end page of 0 means generate all pages.
          string deviceInfo = string.Format(
              CultureInfo.InvariantCulture,
              "&lt;DeviceInfo&gt;" +
              "&lt;OutputFormat&gt;EMF&lt;/OutputFormat&gt;" +
              "&lt;PageWidth&gt;{5}&lt;/PageWidth&gt;" +
              "&lt;PageHeight&gt;{4}&lt;/PageHeight&gt;" +
              "&lt;MarginTop&gt;{0}&lt;/MarginTop&gt;" +
              "&lt;MarginLeft&gt;{1}&lt;/MarginLeft&gt;" +
              "&lt;MarginRight&gt;{2}&lt;/MarginRight&gt;" +
              "&lt;MarginBottom&gt;{3}&lt;/MarginBottom&gt;" +
              "&lt;/DeviceInfo&gt;",
              ToInches(margins.Top),
              ToInches(margins.Left),
              ToInches(margins.Right),
              ToInches(margins.Bottom),
              ToInches(paperSize.Height),
              ToInches(paperSize.Width));
  
          Warning[] warnings;
          m_Streams = new List&lt;Stream&gt;();
          report.Render("Image", deviceInfo, CreateStream,
              out warnings);
          foreach (Stream stream in m_Streams)
              stream.Position = 0;
  
          if (print)
          {
              PrintDialog printDlg = new PrintDialog();
              PrintDocument printDoc = new PrintDocument();
              printDoc.DocumentName = "Report";
              printDlg.Document = printDoc;
              printDlg.AllowSelection = true;
              printDlg.AllowSomePages = true;
  
              //Call ShowDialog
  
              if (printDlg.ShowDialog() == DialogResult.OK)
  
                  printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
              m_CurrentPageIndex = 0;
              printDoc.Print();
  
          }
      }
  
      public static void RenderingCompleteEventHandler(object sender, RenderingCompleteEventArgs e)
      {
  
          PrintDialog startPrint = new PrintDialog();
          startPrint.ShowDialog();
  
      }
  
      // Handler for PrintPageEvents
      public static void PrintPage(object sender, PrintPageEventArgs e)
      {
          Stream pageToPrint = m_Streams[m_CurrentPageIndex];
          pageToPrint.Position = 0;
  
          // Load each page into a Metafile to draw it.
          using (Metafile pageMetaFile = new Metafile(pageToPrint))
          {
              Rectangle adjustedRect = new Rectangle(
                      e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                      e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                      e.PageBounds.Width,
                      e.PageBounds.Height);
  
              // Draw a white background for the report
              e.Graphics.FillRectangle(Brushes.White, adjustedRect);
  
              // Draw the report content
              e.Graphics.DrawImage(pageMetaFile, adjustedRect);
  
              // Prepare for next page.  Make sure we haven't hit the end.
              m_CurrentPageIndex++;
              e.HasMorePages = m_CurrentPageIndex &lt; m_Streams.Count;
          }
      }
  
      public static 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();
          }
      }
  
      public static void PrintToPrinter(this LocalReport report)
      {
          m_PageSettings = new PageSettings();
          ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();
  
          m_PageSettings.PaperSize = reportPageSettings.PaperSize;
          m_PageSettings.Margins = reportPageSettings.Margins;
  
          Export(report);
      }
  
  
      public static void DisposePrint()
      {
          if (m_Streams != null)
          {
              foreach (Stream stream in m_Streams)
                  stream.Close();
              m_Streams = null;
          }
      }
  
      private static string ToInches(int hundrethsOfInch)
      {
          double inches = hundrethsOfInch / 100.0;
          return inches.ToString(CultureInfo.InvariantCulture) + "in";
      }
   }
  }
  </code></pre>