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

P

P_Prdn

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top