This is a migrated thread and some comments may be shown as answers.

Printing from a Telerik Silverlight report automatically

7 Answers 224 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 04 Jan 2012, 02:35 PM

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

Sort by
0
Accepted
Peter
Telerik team
answered on 06 Jan 2012, 02:17 PM
Hello Adam,

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

All the best,
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!

0
Kiran
Top achievements
Rank 1
answered on 31 Jan 2012, 10:54 AM

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

0
Steve
Telerik team
answered on 31 Jan 2012, 12:45 PM
Hello 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!

0
Kiran
Top achievements
Rank 1
answered on 06 Feb 2012, 06:44 AM

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

http://www.telerik.com/community/forums/reporting/telerik-reporting/telerik-reports-huge-chart-print-issue.aspx

Thanks,

Kiran

0
Steve
Telerik team
answered on 06 Feb 2012, 05:58 PM
Hi Kiran,

We've answered your inquiry in the new forum thread. Let's continue the discussion there.

Kind regards,
Steve
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
JULIO
Top achievements
Rank 1
answered on 02 Apr 2012, 06:11 PM
I have a security error, how I can remove it?
thanks
0
Steve
Telerik team
answered on 03 Apr 2012, 09:08 AM
Hi JULIO,

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
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
Tags
General Discussions
Asked by
Adam
Top achievements
Rank 1
Answers by
Peter
Telerik team
Kiran
Top achievements
Rank 1
Steve
Telerik team
JULIO
Top achievements
Rank 1
Share this question
or