Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

PdfViewer.GetService<T>() Method

Gets the specified service.

Namespace: DevExpress.XtraPdfViewer

Assembly: DevExpress.XtraPdfViewer.v24.2.dll

NuGet Package: DevExpress.Win.PdfViewer

#Declaration

public T GetService<T>()
    where T : class

#Type Parameters

Name
T

#Returns

Type Description
T

A service object of the specified generic type, or a null reference (Nothing in Visual Basic) if there is no service object of this type.

#Remarks

Use this method to enable your application objects to obtain a service of the PDF Viewer control, in order to employ methods of the service. The control implements the IServiceProvider interface, so you can tell it what type of service you wish to retrieve; and if a service is available, it is offered to the caller object.

There is a set of predefined services you can retrieve. The IServiceProvider and IServiceContainer interface realization allows hiding implementation details, and provides more flexibility regarding future updates of the component.

#Example

This example shows how to modify the functionality of an existing PdfViewer command.

The PdfViewer exposes the IPdfViewerCommandFactoryService interface that enables you to substitute the default command with your own custom command.

To accomplish this task:

  • Create a custom command class (for example, a CustomNextPageCommand class), inherited from the command that you wish to replace (for example, PdfNextPageCommand).
  • Override the required method of the command. The main functionality and command specifics are located in the Execute method.
  • Create a class (for example, CustomPdfViewerCommandFactoryService) that implements the IPdfViewerCommandFactoryService. You should override the IPdfViewerCommandFactoryService.CreateCommand method to create an instance of a custom command class if an identifier of a certain command is passed as a parameter. So, instead of the default command, a custom command will be used by the PdfViewer.
  • Use the created class to substitute for the default PdfViewer’s service.

View Example

using DevExpress.XtraPdfViewer;
using System;
using System.Windows.Forms;

namespace ViewerCustomCommand {
    public partial class Form1 : Form {

        void ReplacePdfViewerCommandFactoryService(PdfViewer control) {
            IPdfViewerCommandFactoryService service = control.GetService<IPdfViewerCommandFactoryService>();
            if (service == null)
                return;
            control.RemoveService(typeof(IPdfViewerCommandFactoryService));
            control.AddService(typeof(IPdfViewerCommandFactoryService), new CustomPdfViewerCommandFactoryService(control,service));
        }

        public Form1() {
            InitializeComponent();
            pdfViewer1.LoadDocument("..\\..\\Demo.pdf");
            ReplacePdfViewerCommandFactoryService(pdfViewer1);
        }     
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace ViewerCustomCommand
    Friend NotInheritable Class Program

        Private Sub New()
        End Sub

        ''' <summary>
        ''' The main entry point for the application.
        ''' </summary>
        <STAThread> _
        Shared Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)
            Application.Run(New Form1())

        End Sub
    End Class
End Namespace

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetService<T>() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also