How to print only certain page(s) in Microsoft Reports rdlc



My company wants me to switch from using Crystal Reports to Microsoft Reports
and the requirements for my current project is as follows:

1. The program should be able to print directly to printer without the need
to load it to report viewer first.
2. The users should be able to select which page(s) they want to print.

For item no. 2 I tried to use this code, but it didn't work: instead of
printing only page 2 & 3, it prints all pages.

private void Print()
{
const string printerName = "HP LaserJet 1020";
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrintRange = PrintRange.SomePages;
printDoc.PrinterSettings.FromPage = 2;
printDoc.PrinterSettings.ToPage = 3;
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();
}

Below is the complete the modified sample code that I took from MSDN sample
to achieve the above requirements. I appreciate if any body can show me
what's wrong with this particularly in regards to the use of
PrintDocument.PrinterSettings.PrintRange.

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];
}

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.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;
}

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 = "HP LaserJet 1020";
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrintRange = PrintRange.SomePages;
printDoc.PrinterSettings.FromPage = 2;
printDoc.PrinterSettings.ToPage = 3;
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();
}

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();
}
}
}



.



Relevant Pages

  • Re: StreamReader not reading all lines
    ... > trying to write a client application to communicate with a server (that I ... > timer to poll the stream. ... > private bool netClientStart = false; ... > private void StreamWrite ...
    (microsoft.public.dotnet.languages.csharp)
  • XLANGPart.LoadFrom(stream) failing - UnauthorizedAccessException
    ... My component then generates a PDF stream using Crystal Reports, ... attempts to load the XLANGMessage's 1st data part with the stream generated ... ReportDocument report = new ReportDocument; ... Exception thrown from: segment 1, ...
    (microsoft.public.biztalk.general)
  • Problem mit LocalReport im MemoryStream
    ... Von dem 6 Seiten langen Report werden jedoch nur 4 Seiten ... deviceInfo, out mimeType, out encoding, out extension, out streamIDs, ... string encoding; ... //set the position of the stream to 0 to make sure ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: memcmp() checker: memory access errors
    ... I do ungetcall 256 bytes in databack to the stream ... because ungetc() can fail to push back the bytes to the input ... my 256-byte memory leak although it did not report any other error. ... Checker does not report the source-line every time. ...
    (comp.lang.c)
  • Macrovision
    ... How is my video capture driver supposed to report that the incoming signal ... in the dwFrameFlags in the stream header of every VBI packet I send. ... I also tried setting those flags in the dwFrameFlags of every video frame, ... If anyone has succeeded in reporting Macrovisionness and had it be ...
    (microsoft.public.win32.programmer.directx.video)