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

Print report pragmaticaly Silverlight

4 Answers 140 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrei
Top achievements
Rank 1
Andrei asked on 19 Jul 2010, 08:56 AM
Hello, i want to print report pragmaticaly from silverlight... i already have code:

 private void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            Collection<Canvas> pagesList = new Collection<Canvas>();
            Model.PageRoot.UpdateLayout();
            pagesList.Add((Canvas)Model.PageRoot);
            while (Model.PageNumber < Model.PageCount)
            {
                Model.MoveToNextPageCommand.Execute(null);
                Model.PageRoot.UpdateLayout();
                pagesList.Add((Canvas)Model.PageRoot);
            }
        
            int index = 0;
            pd.PrintPage += (s, arg) =>
            {
                if (index >= pagesList.Count)
                {
                    arg.HasMorePages = false;
                    return;
                }
                arg.PageVisual = pagesList.ElementAt(index);
                arg.HasMorePages = true;
                index++;
            };
            pd.Print("");
        }

but when i use  Model.MoveToNextPageCommand.Execute(null); PageRoot is not updated and i have the same page each time... can you help me? how can i go through pages and collect data in images array for print them next? what should i do to update pageroot?

4 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 22 Jul 2010, 01:12 PM
Hello Andrei,

Your goal cannot be achieved in this way as the request for retrieving the page is performed asynchronously.This means that your code continues and finishes to execute before the next page has been loaded. For this reason the page root stays always the same.

In order to render and retrieve pages manually you have to use the ReportServiceClient class and use its methods and events to render the report and get pages. To render the report you have to call first RenderAndCacheAsync method and then GetPageAsync method to collect the pages. The page root is available as a result in the GetPageCompleted event handler arguments. You can get it in the following way:
 
void GetPageCompletedHandler(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());
  }
}

Hope this helps.

Please, note the this class and its methods are for internal use only and can be changed or modified in future so you if you decide to use it, it will be at your own risk.

Kind regards,
Chavdar
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrei
Top achievements
Rank 1
answered on 23 Jul 2010, 03:20 PM
 
private void OnPrintButtonClick(object sender, RoutedEventArgs e)
        {
  
            ReportServiceClient rsc = new ReportServiceClient(this.ReportServiceUri);
 
            pagesList = new Collection<Canvas>();
            pagesList.Add((Canvas)Model.PageRoot);
 
            rsc.RenderAndCacheAsync("", "COMWReports.EmigrantsWorkCertificates", null, null);
            rsc.RenderAndCacheCompleted += getInstanceID;
 
            while (Model.PageNumber < Model.PageCount)
            {
                Model.MoveToNextPageCommand.Execute(true);
                rsc.GetPageAsync(this.instanceID, Model.PageNumber);
                rsc.GetPageCompleted += getReportPage;
            }
 
            PrintDocument pd = new PrintDocument();
           
 
            int index = 0;
 
 
            pd.PrintPage += (s, arg) =>
            {
                if (index >= pagesList.Count)
                {
                    arg.HasMorePages = false;
                    return;
                }
 
                arg.PageVisual = pagesList.ElementAt(index);
                arg.HasMorePages = true;
                index++;
            };
 
 
 
            pd.Print("");
 
        }
 
        private void getReportPage(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.pagesList.Add((Canvas)pageRoot);
            }
        }
 
        private void getInstanceID(object sender, RenderAndCacheEventArgs e)
        {
            this.instanceID = e.RenderingResult.InstanceID;
        }


My code looks like this but i'm having Exception: Report is unavailable or session has expired.

What can you advice me to handle it?
0
Chavdar
Telerik team
answered on 26 Jul 2010, 08:06 AM
Hello Andrei,

You have to pass as a first argument to the RenderAndCacheAsync method the name of the rendering extension. In this case it will be "XAML".

Sincerely yours,
Chavdar
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
danparker276
Top achievements
Rank 2
answered on 07 Apr 2011, 07:42 PM
I'm trying to print a report without showing the report viewer and without showing the printing dialog box (in oob trusted).   Many clients want an automatic printout. It looks like this will work for printing without the report viewer, but the dialog box will still show up. 
Maybe I can use this and add use AutomationFactory to create a work document and print like below:
http://forums.silverlight.net/forums/p/180499/415358.aspx

I'm not sure how well this would work though.  Since those are internal calls that might change, maybe I should wait to do something like this.  Are there any plans to make a direct print function for the silverlight reports?  Maybe when silverlight 5 comes out and supports vector printing?  Would it be worth it to put this up as a future issue, or is this out of the realm of what the silverlight report viewer would do?
Tags
General Discussions
Asked by
Andrei
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
Andrei
Top achievements
Rank 1
danparker276
Top achievements
Rank 2
Share this question
or