I am trying to trigger a report to print once it has been rendered using either the native Silverlight printing ability or the integration with Adobe Acrobat.
In my searches I have found this forum post - http://www.telerik.com/community/forums/reporting/telerik-reporting/reporting-parameters-first-time-not-applied-when-usenativeprinting-false.aspx
Which seems to describe how to do it, but when I try this approach, using the ReportServiceClient RenderAndCacheCompleted event, I get a SecurityException if using native print support (not a user triggered event so understandable) and when trying to print using non-native, apparently nothing happens at all.
When I debug I can see the PrintReportCommand execution is failing (checking CanExecute returns false), possibly due to the ReportViewerModel state being ViewerWorkInProgressState.
It almost seems like this isn't the right event, like it's still too early to trigger the print operation at this point, but there don't seem to be any other events to indicate the report has completely rendered.
Is any other way of doing this? Thanks.
7 Answers, 1 is accepted
There is a limit on the time allowed between the user initiates the dialog and when the dialog is shown. If the time limit between these actions is exceeded, an exception will occur. You may find useful Silverlight Printing MSDN article.
The True Printing requires a different approach to invoke the print operation. Check out the following code snippets that shows how to invoke the True Print operation once the report is rendered:
using
System;
using
System.Collections.Generic;
using
System.Collections.ObjectModel;
using
System.IO;
using
System.Text;
using
System.Windows.Controls;
using
System.Windows.Markup;
using
System.Windows.Printing;
using
Telerik.ReportViewer.Silverlight;
using
Telerik.Reporting.Service.SilverlightClient;
using
Telerik.Reporting.Service;
using
System.ComponentModel;
namespace
Reports
{
public
partial
class
MainPage1 : UserControl
{
string
reportName =
"Telerik.Reporting.Examples.CSharp.Invoice, CSharp.ReportLibrary"
;
List<Canvas> pages;
ReportServiceClient serviceClient;
string
instanceID;
PrintDocument printDocument;
int
pageCountToPrint;
public
MainPage1()
{
InitializeComponent();
this
.printDocument =
new
PrintDocument();
Uri serviceUri =
new
Uri(App.Current.Host.Source,
"../ReportService.svc"
);
this
.serviceClient =
new
ReportServiceClient(serviceUri);
this
.serviceClient.RenderAndCacheCompleted += RenderAndCacheCompleted;
this
.serviceClient.GetPageCompleted +=
new
EventHandler<GetPageEventArgs>(ServiceClientGetPageCompleted);
}
void
button1_Click(
object
sender, RoutedEventArgs e)
{
this
.button1.IsEnabled =
false
;
this
.pagesList =
new
Collection<Canvas>();
var parameters =
new
NameValueDictionary();
this
.serviceClient.RenderAndCacheAsync(
"XAML"
, reportName,
null
, parameters);
this
.pageCountToPrint = 0;
}
void
RenderAndCacheCompleted(
object
sender, RenderAndCacheEventArgs e)
{
this
.instanceID = e.RenderingResult.InstanceID;
int
pageCount;
for
(pageCount = 1; pageCount <= e.RenderingResult.PageCount; pageCount++, pageCountToPrint++)
{
this
.serviceClient.GetPageAsync(instanceID, pageCount);
}
this
.pages =
new
List<Canvas>(pageCount);
int
index = 0;
this
.printDocument.PrintPage += (s, arg) =>
{
var currentPrintPageNumber = ((PrintDocument)s).PrintedPageCount + 1;
arg.PageVisual =
this
.pages[currentPrintPageNumber-1];
arg.HasMorePages = (currentPrintPageNumber <
this
.pages.Count);
};
}
void
ServiceClientGetPageCompleted(
object
sender, GetPageEventArgs e)
{
var result = e.PageInfo;
using
(var ms =
new
MemoryStream(result.Buffer))
using
(var sr =
new
StreamReader(ms, Encoding.Unicode))
{
var pageRoot = XamlReader.Load(sr.ReadToEnd());
this
.pages.Add((Canvas)pageRoot);
}
pageCountToPrint--;
System.Diagnostics.Debug.WriteLine(e.PageInfo.PageNumber);
if
(
this
.pageCountToPrint == 0)
{
button2.IsEnabled =
true
;
}
}
void
button2_Click(
object
sender, RoutedEventArgs e)
{
this
.button1.IsEnabled =
true
;
this
.button2.IsEnabled =
false
;
this
.printDocument.Print(
"document"
);
}
}
}
Peter
the Telerik team
Q3’11 of Telerik Reporting is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!
Dear Telerik team ,
I have implemented the above code for external print button functionality in silverlight with a report having more than 20 pages. I have tested this using XPS printer I have found few of the pages are not printed correctly. Out of 20 pages I have found 2 pages are printing partially every time.(Page 7 and page 14). I have attached the screen shot for your reference. Please help on this issue.
Thanks ,
Kiran
We have not seen such a problem and could not reproduce it in our local tests. That is why we would appreciate if you open a support ticket and attach a sample project that exhibits the problem. Once we review it, we would be able to provide more information.
Greetings,
Steve
the Telerik team
Q3’11 of Telerik Reporting is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!
Hi Steve,
I am not able to attach the whole project it doesn’t allow. I have copied the sample code to this below post please help
Thanks,
Kiran
We've answered your inquiry in the new forum thread. Let's continue the discussion there.
Kind regards,
Steve
the Telerik team
This is a generic Silverlight error that states that the print should be user initiated. This information is available in the Silverlight Printing msdn article.
Greetings,
Steve
the Telerik team