Printing report programmatically


07-13-2007, 6:31 AM
I am struggling to find how to print a report programmatically without going through the Report Preview control.

Can this be done as we have a task to be able to print reports from a back end service process?

Re: Printing report programmatically


07-13-2007, 12:53 PM
Basically, you need to create a ReportDefinition object and load the rdlx report file. Create a ReportRuntime object using the ReportDefinition object as a parameter.

You'll need to make sure your project references both the DataDynamics.Reports and DataDynamics.Reports.Rendering.Graphics assemblies.

Use a PrintDocumentRenderingExtension to render the report to, then call the Print method off of the rendering extension's Document object.

C#:

ReportDefinition rd = new ReportDefinition(new FileInfo(Application.StartupPath + "\\..\\..\\Report1.rdlx"));
ReportRuntime rt = new ReportRuntime(rd);

PrintDocumentRenderingExtension renderExt = new PrintDocumentRenderingExtension();
FileStreamProvider fsp = new FileStreamProvider(new DirectoryInfo(Application.StartupPath), "doc");
NameValueCollection nvc = new NameValueCollection();
nvc.Add("PrintLayoutMode", "OneLogicalPageOnSinglePhysicalPage");

rt.Render(renderExt, fsp, nvc);

renderExt.Document.Print();

This code assumes the following using directives have been added to the top of the file:

using System.IO;
using DataDynamics.Reports;
using DataDynamics.Reports.Rendering.IO;
using DataDynamics.Reports.Rendering.Graphics;

-Jon

Re: Printing report programmatically


07-17-2007, 7:42 AM
Thanks Jon, this works a treat.

One further question, when printing my report needs to print in landscape but all my output comes out in portrait. Is there a way I can change this?

TIA

John

Re: Printing report programmatically


07-17-2007, 10:58 AM
John,

This is currently a known issue with a tracking number of CR# 25032. The Report Definition Language has no concept of page orientation, however, you can currently mimic Landscape orientation by setting the page width to 11" and page height to 8.5". This is one of our highest priority tasks and should be fixed in the next release.

Thanks,
Jon

Re: Printing report programmatically


07-24-2007, 12:13 AM
John,

We have added a fix to this change request in our internal code base. This issue will definitely be resolved in the next release.

If you have any more question, feel free to ask.

Thanks,
-Jon

Re: Printing report programmatically


07-24-2007, 5:22 AM
Thanks Jon.

Re: Printing report programmatically


03-02-2008, 8:01 PM
Jon -

In my situation I have to print any number of RDLs sequentially and present a Print Dialog box to the user the before the print. Would you recommend that I follow the code snippet you posted above? And set the forceDataRefresh parameter to True?

Thanks,
Roland

Re: Printing report programmatically


03-02-2008, 10:06 PM
Roland,

I've created a small sample which demonstrates a method of printing similar to your requirements. This sample was created with the latest build 1.0.342.0 which is available here:

http://www.datadynamics.com/forums/77/ShowForum.aspx

If you have any more questions, feel free to ask.

Thanks,
Jon

Re: Printing report programmatically


02-09-2009, 3:00 PM
Hi,

I am using WPF. How would I go about using the System.Windows.Controls.PrintDialog as opposed to the old System.Windows.Forms.PrintDialog? In the old dialog, you set renderer.Document.PrinterSettings = printDialog.PrinterSettings, but the new dialog does not appear to have PrinterSettings, only a PrintQueue. Maybe I am missing something obvious. Any help would be appreciated.

Thanks in advance,
Pauly

Re: Printing report programmatically


02-10-2009, 1:45 AM
Hello,
MSDN says: "Do not confuse this class, System.Windows.Controls.PrintDialog, with System.Windows.Forms.PrintDialog. The latter is used with Windows Forms applications. System.Windows.Controls.PrintDialog is used with Windows Presentation Foundation (WPF) applications." AFAIK there is no way to use WPF PrintDialog to collect the printer settings needed for printing by using PrintDocument class. As far as I inderstand you are using the custom report viewer toolbar which is built by using WPF and want to print the report programmatically once user selects the print settings in the custom print dialog(WPF)? If so, the only way to meet this goal is using custom print dialog which can retrive the PrinterSettings class or using
System.Windows.Controls.PrintDialog form...

HTH


Sergey Abakumoff
GrapeCity

Re: Printing report programmatically


05-21-2009, 11:25 AM
Jon,

I tried your printing.zip example and it works fine for portrait reports, but does not work for landscape. I changed one of the test reports to landscape and it still prints portrait. The code settings["Orientation"] = "Landscape" seems to do nothing.

I am using version 1.5.1052.0. I attached the changed report.

Please help. Thanks,
Pauly

Re: Printing report programmatically


05-27-2009, 2:47 PM
Hello,

We are working on this issue and will get back to you ASAP with our findings and observations.

Regards,
Sandeep

Re: Printing report programmatically


05-28-2009, 12:54 PM
Hello,

Please find attached a sample application that shows how you can print a report programatically in Landscape mode.

Regards,
Sandeep

Re: Printing report programmatically


08-05-2010, 4:18 PM
FYI,

This doesn't work with master reports. The page width/height and PaperOrientation is not set from the master. You have to manually open the master to get this info.

Code is below for others having problems.

Pauly


// orientation is not set if it has a master!
ReportDefinition orientationDefinition;
if (reportDefinition.Report.MasterReport != null && reportDefinition.Report.MasterReport != "")
orientationDefinition = new ReportDefinition(new FileInfo(_reportPath + reportDefinition.Report.MasterReport));
else
orientationDefinition = reportDefinition;

// get the default rendering settings
NameValueCollection settings = ((IConfigurable)_renderer).GetSupportedSettings().GetSettings();
settings["PrinterName"] = printerSettings.PrinterName;

// get orientation from report
if (orientationDefinition.Report.PaperOrientation == PaperOrientation.Landscape)
settings["Orientation"] = "Landscape";
else
settings["Orientation"] = "Portrait";

// render
reportRuntime.Render(_renderer, null, settings);

// force orientation in printer settings
if (orientationDefinition.Report.PaperOrientation == PaperOrientation.Landscape)
printerSettings.DefaultPageSettings.Landscape = true;
else
printerSettings.DefaultPageSettings.Landscape = false;

// print
_renderer.Document.PrinterSettings = printerSettings;
_renderer.Document.Print();