RDL Viewer
The RDL Viewer (RdlViewer.dll) control allows you show reports in your Windows application. You should also look at the DataTests application and the RdlReader application provided as part of the source code for coding examples and practices using the RdlViewer. For your end user, some of the user available for users of the viewer might include:- Scroll thru the pages of the report
- Print the report
- Zoom in and out
- Export to various formats (html, PDF, Excel, tif, xml, rtf, mht, csv)
- Select text and images from report and copy to the clipboard
- Find text in the report
Properties
Below are a sampling of the more popular properties available.- ShowWaitDialog
- Show the Wait Dialog when retrieving and rendering report when true.
- ShowParameterPanel
- ShowFindPanel
- SelectTool
- Enables/Disables the selection tool. The selection tool allows the user to select text and images on the display and copy it to the clipboard.
- CanCopy
- True when one or more PageItems are selected.
- SourceFile
- A file containing RDL XML syntax. You should set only SourceRdl or SourceFile; not both.
- SourceRdl
- RDL XML syntax that defines the report. You should set only SourceRdl or SourceFile; not both.
- Parameters
- Parameters passed to report when run. Parameters are separated by '&'. For example, OrderID=10023&OrderDate=10/14/2002 Note: these parameters will override the user specified ones.
- Zoom
- Zoom factor. For example, .5 is a 50% reduction, 2 is 200% increase.
Export
Export allows you to save the report as previewed into another format. The fidelity of the format will vary between the various renderers. PDF and TIF renderers use the same format as the RdlViewer and so most closely match what the RdlViewer represents. Other renderers require rerunning the report (including retrieval of data) in order to export to the requested format.Below is some sample code that shows how to export a report. This code was extracted (and slightly modifed) from the RdlReader application.
private void menuExport_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter =
"PDF files (*.pdf)|*.pdf|" +
"XML files (*.xml)|*.xml|" +
"HTML files (*.html)|*.html|" +
"CSV files (*.csv)|*.csv|" +
"RTF files (*.rtf)|*.rtf|" +
"TIF files (*.tif)|*.tif|" +
"Excel files (*.xlsx)|*.xlsx|" +
"MHT files (*.mht)|*.mht";
sfd.FilterIndex = 1;
string file = rdlViewer.SourceFile;
if (file != null)
{
int index = file.LastIndexOf('.');
if (index > 1)
sfd.FileName = file.Substring(0, index) + ".pdf";
else
sfd.FileName = "*.pdf";
}
else
sfd.FileName = "*.pdf";
if (sfd.ShowDialog(this) != DialogResult.OK)
return;
// save the report in a rendered format
string ext=null;
int i = sfd.FileName.LastIndexOf('.');
if (i < 1)
ext = "";
else
ext = sfd.FileName.Substring(i+1).ToLower();
switch(ext)
{
case "pdf": case "xml": case "html": case "htm": case "csv": case "rtf":
case "mht": case "mhtml": case "xlsx": case "tif": case "tiff":
if (ext == "tif" || ext == "tiff")
{
DialogResult dr = MessageBox.Show(this, "Do you want to save colors in TIF file?", "Export", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.No)
ext = "tifbw";
else if (dr == DialogResult.Cancel)
break;
}
try {rdlViewer.SaveAs(sfd.FileName, ext);}
catch (Exception ex)
{
MessageBox.Show(this,
ex.Message, "Save As Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
break;
default:
MessageBox.Show(this,
String.Format("{0} is not a valid file type. File extension must be PDF, XML, HTML, CSV, MHT, RTF, TIF, XLSX.", sfd.FileName), "Save As Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
return;
}
Printing
Below is some sample code that shows how to print a report. This code was extracted (and slightly modified) from the RdlReader application.
using System.Drawing.Printing;
PrintDocument pd = new PrintDocument();
pd.DocumentName = rdlViewer.SourceFile == null? "untitled": rdlViewer.SourceFile;
pd.PrinterSettings.FromPage = 1;
pd.PrinterSettings.ToPage = rdlViewer.PageCount;
pd.PrinterSettings.MaximumPage = rdlViewer.PageCount;
pd.PrinterSettings.MinimumPage = 1;
if (rdlViewer.PageWidth > rdlViewer.PageHeight)
pd.DefaultPageSettings.Landscape=true;
else
pd.DefaultPageSettings.Landscape=false;
PrintDialog dlg = new PrintDialog();
dlg.Document = pd;
dlg.AllowSelection = true;
dlg.AllowSomePages = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
if (pd.PrinterSettings.PrintRange == PrintRange.Selection)
{
pd.PrinterSettings.FromPage = rdlViewer.PageCurrent;
}
rdlViewer.Print(pd);
}
catch (Exception ex)
{
MessageBox.Show("Print error: " + ex.Message);
}
}
Events and Callbacks
There are a number of events and callbacks that you may want to take advantage of in your application. If you download the source code the example application DataTests has code demonstrating their use.GetDataSourceReferencePassword Callback
Reports that use shared DataSource references need a pass phrase in order to open the encrypted file. This callback allows you to provide that pass phrase.
this.rdlViewer.GetDataSourceReferencePassword = new fyiReporting.RDL.NeedPassword(this.GetPassword);
// GetPassword asks the user to provide a pass phrase. It saves it so that subsequant requests don't prompt the user again.
string GetPassword()
{
if (_DataSourceReferencePassword != null) // Use should define _DataSourceReferencePassword locally.
return _DataSourceReferencePassword;
DataSourcePassword dlg = new DataSourcePassword(); // This dialog is provided as part of the RdlViewer dll.
if (dlg.ShowDialog() == DialogResult.OK)
_DataSourceReferencePassword = dlg.PassPhrase;
return _DataSourceReferencePassword;
}
SubreportDataRetrieval Event
The SubreportDataRetrieval event a subreport is about to retrieve data. One use is set the subreport's data.this.rdlViewer.SubreportDataRetrieval +=new EventHandler(rdlViewer_SubreportDataRetrieval); void rdlViewer_SubreportDataRetrieval(object sender, SubreportDataRetrievalEventArgs e) { int ids = 0; foreach (fyiReporting.RDL.DataSet ds in e.Report.DataSets) ids++; MessageBox.Show(string.Format("Subreport Data Retrieval: {0} datasets", ids)); }
Hyperlink Event
The Hyperlink event is triggered when a mouse clicks on a report item with a Hyperlink defined. For example,
this.rdlViewer.Hyperlink += new RdlViewer.HyperlinkEventHandler(rdlViewer_Hyperlink);
void rdlViewer_Hyperlink(object source, HyperlinkEventArgs e)
{
if (MessageBox.Show(
string.Format("Do you wish to invoke Hyperlink {0}", e.Hyperlink), "Hyperlink", MessageBoxButtons.YesNo)
== DialogResult.No)
{
e.Cancel = true;
}
}
fyiReporting Software, LLC