SharpShooter Reports.WinRT: export

SharpShooter Reports.WinRT export

There are several possibilities to Export a document from code using ReportViewer object:

There are three possible scenarios of using export: save to file, use share contract and get byte[] from code.

Using standard dialogs

In order to get an opportunity to get save to file and share options you need to open application manifest and specify Toast capable as true:

Open manifest Package.appxmanifest and configure it in the following way:

Exports use toast notifications in order to notify user that export’s started and finished. So, we need to enable toasts for our application.

Set Toast capable to Yes (at Application UI tab):


In order to initiate export you need to tap on Export button:


You will get a list of standard export filters:


Choose one and you will get options menu:


There are two dialogs coming with product by default:

1. OpenXmlExcel Export Settings


Export without page delimiters

Specifies whether page footers and page headers between pages are omitted

Export to one sheet

Specifies whether all pages are exported to one sheet or each page is exported to at distinct sheet

PageRange

PageRange pattern specifies which pages to export

ImageResolution

Specifies resolution for the exported images

ImageQuality

Quality loss for Jpeg images. 100 – no loss.

Open After Export

Describes whether the document will be opened after export is finished.


2. PDF Export Settings


       a.  Common Settings


PageRange

PageRange pattern specifies which pages to export

KeepOriginalImageResolutionAndQuality

Specifies whether to export images to PDF as they are in the document or apply specified image settings

Custom Resolution

Specifies resolution for the exported images

ImageQuality

Quality loss for Jpeg images. 100 – no loss.

Reuse Images

Specify whether image reusing algorithm will be used for the duplicate images.

Compress

Specifies whether compression is used for a PDF document

Open After Export

Describes whether the document will be opened after export is finished.


     b. Security settings


Encription Level

Specifies PDF security level:  None, Low40RC4 or High128RC4

Document openning password

Specifies password for the PDF document

Change permissions password

Specifies password for changing PDF document permissions

AllowPrinting

Specifies whether printing of the exported PDF will be available

AllowCommenting

Specifies whether commenting will be available for the exported document.

AllowChangingDocument

Specifies whether document changing is available for the exported document.

AllowCopyingContent

Specifies whether content copying will be available in the exported document.

Using Export method

Beside dialogs you can also call export from code:

public void Export(string exportFormat, ExportMode mode)

For example:

this.reportViewer.Export("pdf", PerpetuumSoft.WinRT.Viewer.ExportMode.SaveToFile);

There are several standard export formats:

"csv"

"excel"

"excelxml

"html"

"xlsx"

"pdf"

"rtf"

"xps"

 

It’s possible to use SaveToFile and Share ExportMode from code.

Using ExportToStream async method

There are 2 overloads of this method:

async Task<ExportResult> ExportToStream(string exportFormat)
async Task<ExportResult> ExportToStream(string exportFormat, ExportOptions exportOptions)

In order to use these methods let’s wait until the document is loaded and put async modifier for this method:

       

async void reportViewer_DocumentLoaded(object sender, PerpetuumSoft.WinRT.Viewer.Managing.DocumentLoadedEventArgs e)
        {
            var result = await this.reportViewer.ExportToStream("pdf");
        }

ExportResult will contain the following information:

IsSucceed

Gets value that determines if export completed successfully

Error

Error description or null if export completed successfully.

Data

Bytes of exported document

FileName

Document name plus extension name separated with dot.

MimeType

File mimetype

 

ExportOptions is a class which represents different export settings: PageRange, OpenAfter and so on.

There are two inherited classes: PDFExportOptions and ExcelExportOptions.

Default export formats are listed above. As for options, let’s take a deeper look at them.

ExportOptions class describes common export filter settings:

OpenAfter

Describes whether the document will be opened after export is finished.

PageRange

PageRange pattern that specifies which pages we want to export

 

ExcelExportOptions class describes xlsx export settings:

ExportWithoutPageDelimeters

Specifies whether page footers and page headers between pages are omitted

ExportToOneSheet

Specifies whether all pages are exported to one sheet or each page is exported to a distinct sheet

ImageResolution

Specifies resolution for the exported images

ImageQuality

Quality loss for Jpeg images. 100 – no loss.

 

PDFExportOptions class describes PDF export settings:

EmbedFonts

Embeds fonts into a PDF document during export

Compress

Specifies whether compression is used for a PDF document

KeepOriginalImageResolutionAndQuality

Specifies whether to export images into PDF as they are in the document or apply specified image settings

ImageResolution

Specifies resolution for the exported images

EmbedPrivateCharacters

Specifies whether private characters will be embedded into a PDF document

ImageQuality

Quality loss for Jpeg images. 100 – no loss.

SecurityLevel

Specifies PDF security level:  None, Low40RC4 or High128RC4

AllowPrinting

Specifies whether printing of the exported PDF document will be available

AllowCommenting

Specifies whether commenting in the exported document will be available

AllowChangingDocument

Specifies whether changing of the exported document will be available

AllowCopyingContent

Specifies whether content copying in the exported document will be available

UserPassword

Specifies password for a PDF document

ChangePermissionsPassword

Specifies password for changing PDF document permissions

ReuseImage

Specify whether image reusing algorithm for the duplicate images will be used.

IsUserPasswordUsed

Specifies whether password will be used

IsAutomaticGenerationUsed

Specifies whether password will be automatically generated

 

Specifying default settings for PDF and Open Xml Excel exports

If you want to specify export settings from code you will need the ProcessExportParameters event:

this.reportViewer.ProcessExportParameters += reportViewer_ProcessExportParameters;

We can specify default settings from our event handler like it’s shown below:

     

void reportViewer_ProcessExportParameters(object sender, PerpetuumSoft.WinRT.Viewer.ProccessExportParametersEventArgs e)
      {
          if (e.FormatName == "pdf")
          {
              e.ExportOptions = new PDFExportOptions()
              {
                  OpenAfter = true,
                  SecurityLevel = SecurityLevel.High128RC4,
                  AllowPrinting = true,
                  AllowCopyingContent = true,
                  AllowCommenting = true,
                  AllowChangingDocument = true
              };
             // e.Handled = true;
          }
          if (e.FormatName == "xlsx")
          {
              e.ExportOptions = new ExcelExportOptions()
              {
                  OpenAfter = true,
                  ExportToOneSheet = false,
                  ExportWithoutPageDelimeters = false,
                  ImageQuality = 100,
                  ImageResolution = 600,                  
              };
          }
      }

You can even skip export settings window by specifying e.Handled property as true in the way shown above in the commented code.

List of export filters

Report viewer has some pre-set export filters. You can delete one or several export filters. You will need to override GetExportFilters
method in the service, get a list of export filters from the base implementation and delete unnecessary ones:

    

public override List< ExportFilterDefinition>
               GetExportFilters(out ExceptionDetailBase ReportError)
     {
          var filters = base.GetExportFilters(out ReportError);
          return filters
              .Where(filter => filter.FilterName != "Csv")
              .ToList();
     }

In order to add export filters, it is necessary to drag and drop components of the required export filters in the service designer.
To add icon for the added export option you will need to add a new ButtonTemplateSet element with the key corresponding to the export
filter name to the ReportViewer ExportLogos dictionary. Templates for the three states should be added as well: Normal, Hover and Pressed.

For example, we’ve got the following icon for the XPS export option:


It is set with the following XAML code:

<Canvas Width="48" Height="48">
    <Ellipse Width="30" Height="30" Canvas.Top="4"
             Canvas.Left="4" Stroke="White" StrokeThickness="2"/>
    <TextBlock Canvas.Left="10" Canvas.Top="16" FontSize="20"
               Text="XPS" Foreground="Black" FontWeight="Bold"/>
    <TextBlock Canvas.Left="11" Canvas.Top="17" FontSize="20"
               Text="XPS" Foreground="White" FontWeight="Bold"/>
</Canvas>

And we need to add or change current icon to this one. Since our theme is based on the contrast of white and black, we need to provide
at least 2 pictures: white on the black background and black on the white background to show that the button is pressed. We can certainly
create the necessary object from code, but it is more convenient to set it in the page resources.

<Page.Resources>
    <customControls:ButtonTemplateSet x:Key="xpsLogo">
        <customControls:ButtonTemplateSet.NormalTemplate>
            <ControlTemplate>
                <Canvas Width="48" Height="48">
                    <Ellipse Width="30" Height="30" Canvas.Top="4"
                                Canvas.Left="4" Stroke="White" StrokeThickness="2"/>
                    <TextBlock Canvas.Left="10" Canvas.Top="16" FontSize="20"
                                Text="XPS" Foreground="Black" FontWeight="Bold"/>
                    <TextBlock Canvas.Left="11" Canvas.Top="17" FontSize="20"
                                Text="XPS" Foreground="White" FontWeight="Bold"/>
                </Canvas>
            </ControlTemplate>
        </customControls:ButtonTemplateSet.NormalTemplate
        <customControls:ButtonTemplateSet.PressedTemplate>
            <ControlTemplate>
                <Canvas Width="48" Height="48">
                    <Ellipse Width="30" Height="30" Canvas.Top="4"
                                Canvas.Left="4" Stroke="Black" StrokeThickness="2"/>
                    <TextBlock Canvas.Left="10" Canvas.Top="16" FontSize="20"
                                Text="XPS" Foreground="White" FontWeight="Bold"/>
                    <TextBlock Canvas.Left="11" Canvas.Top="17" FontSize="20"
                                Text="XPS" Foreground="Black" FontWeight="Bold"/>
                </Canvas>
            </ControlTemplate>
        </customControls:ButtonTemplateSet.PressedTemplate>
    </customControls:ButtonTemplateSet>
</Page.Resources>
 

We use the following code to set the icon:

var item = this.Resources["xpsLogo"] as
    PerpetuumSoft.WinRT.Viewer.Controls.ButtonTemplateSet;
reportViewer.ExportLogos.Remove("xps");
reportViewer.ExportLogos.Add("xps", item);

Besides, it is possible to change pictures for the standard export options (and add custom ones) by overriding viewer style. It is necessary to override style of the winrt:ReportViewer control and change the set parameters in the ExportLogos property setter.

Add Feedback