Adding PDF Export button in WPFViewer for ActiveReports 12

Posted by: jennifer.reusser on 19 November 2019, 3:37 pm EST

    • Post Options:
    • Link

    Posted 19 November 2019, 3:37 pm EST

    I am trying to follow the example in the blog posting for ActiveReports 8 (https://www.grapecity.com/blogs/adding-exporting-functionality-to-wpf-viewer/) but there is no link to download the sample solution and the posted code seems to be missing some steps - no mention of how or where to define the “MainWindow.reportDocument”. I was able to get a little bit further when I found the forum posting here https://www.grapecity.com/forums/ar-dev/adding-exporting-to-wpf-vi. However, I am using version 12 not 7 and I also have it set up to where the user picks the report to load at runtime using a combobox and preview button.

    My problems:

    1. how to I set the value for reportDocument?
    2. how do I get the “PdfCommand” name to exist in the namespace “clr-namespace:AXReports”

    In my MainWindow.xaml.cs file I have the following:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace AXReports
    {
        /// <summary>
        /// 
        /// </summary>
        public partial class MainWindow : Window
        {
            static readonly string CurrentFileLocation = AppDomain.CurrentDomain.BaseDirectory + @"\Reports\";
            public static GrapeCity.ActiveReports.Document.PageDocument reportDocument { get; set; }
            public MainWindow()
            {
                InitializeComponent();
            }
            /// <summary>
            /// >Preview Button Click- Load selected report on click.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnPreview_Click(object sender, RoutedEventArgs e)
            {
                ComboBoxItem _report = (ComboBoxItem)CmbListBox.SelectedItem;
                switch (_report.Content.ToString())
                {
                    case "AXDocOverview.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                    case "AXGlobalUDLSpec.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                    case "AXAppSpecReview.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                    case "AXAppSpecSign.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                    case "AXUserAccessRequestBasic.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                    case "AXUserAccessRequestAdobeSign.rdlx":
                        ReportViewer.LoadDocument(CurrentFileLocation + _report.Content);
                        break;
                }
            }
            /// <summary>
            ///In the SelectionChanged event of the combo box, disable 'Preview' button, when no report is selected.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void CmbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (CmbListBox.SelectedIndex == 0)
                {
                    btnPreview.IsEnabled = false;
                }
                else
                {
                    btnPreview.IsEnabled = true;
                }
            }
        }
    }
    

    My PdfCommand.cs file has this:

    using System;
    using System.Windows;
    using System.Windows.Input;
    
    namespace AXReports
    {
        class PdfCommand : ICommand
        {
            public event EventHandler CanExecuteChanged;
    
            public bool CanExecute(object parameter)
            {
                return true;
            }
    
            public void Execute(object parameter)
            {
                GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport _exporter = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();
                _exporter.Export(MainWindow.reportDocument, "..\\..\\Test.pdf");
                MessageBox.Show("Exporting of Report to PDF Complete!");
            }
        }
    }
    

    My DefaultWPFViewerTemplates.xaml has this:

    <ResourceDictionary  ... xmlns:AXReports="clr-namespace:AXReports">
        <AXReports:PdfCommand x:Key="PdfCommand" />
    
    ...
    
      <!-- Custom buttons-->
      <Button Command="{StaticResource PdfCommand}" ToolTip="Export to PDF" >
        <StackPanel>
           <Image Source="Image\\pdf.gif" />
         </StackPanel>
      </Button>
    </ToolBar>
    
    
  • Posted 20 November 2019, 3:13 am EST

    Hello,

    Please refer to the attached sample to implement the same.

    1. how to I set the value for reportDocument?

      You can set the Report Document value as follow:

      case “AXUserAccessRequestAdobeSign.rdlx”:
      GrapeCity.ActiveReports.PageReport rpt = new GrapeCity.ActiveReports.PageReport();
                   rpt.Load(new System.IO.FileInfo(CurrentFileLocation + _report.Content);
                   rpt.Run();
                   GrapeCity.ActiveReports.Document.PageDocument MyDocument = new GrapeCity.ActiveReports.Document.PageDocument(rpt);
                   Viewer1.LoadDocument(MyDocument);
                   reportDocument = MyDocument;
     break;
    
    1. how do I get the “PdfCommand” name to exist in the namespace “clr-namespace:AXReports”

      On MainWindow.xaml before the opening tag, add the following code.
    <Window.Resources>                                
    <ResourceDictionary Source="DefaultWPFViewerTemplates.xaml" />
    </Window.Resources>
    

    Hope it helps.

    Thanks,

    Mohit

    PDFButton_AR.zip

  • Posted 20 November 2019, 12:24 pm EST

    That works great. Thanks much!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels