Viewing PDF Files in WPF Pdf Viewer

PDF Viewer allows you to easily open and view the PDF files interactively using the Open button in the built-in toolbar as well as from code behind using the available APIs.

Open PDF file from the local disk using toolbar

You can open a PDF file from the toolbar by browsing it from the local disk. You can open both the normal and password-protected PDF files. The Open button in the toolbar allows you to perform the same using the following steps.

  1. Add the PdfViewerControl in the MainWindow.xaml and run the project.

     <Window 
         x:Class="PdfViewerDemo.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         WindowState="Maximized"
         xmlns:syncfusion="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF">
         <Grid>
             <syncfusion:PdfViewerControl x:Name="pdfViewer"></syncfusion:PdfViewerControl>
         </Grid>
     </Window>
  2. Click the Open button in the toolbar, as shown in the following picture. Open PDF file
  3. In the open file dialog, enter the file name or browse the file from the local disk and select Open.

View PDF file using the file path

You can view the PDF file from code behind, by passing the file path as a parameter to the Load method of PdfViewerControl. It accepts both the absolute and relative file paths. Refer to the following code to perform the same.

using System.Windows;

namespace PdfViewerDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        # region Constructor
        public MainWindow()
        {
            InitializeComponent();

            //Load PDF file using the file path.
            pdfViewer.Load(@"HTTP Succinctly.pdf");
        }
        #endregion
    }
}

View PDF file from stream

You can view the PDF file from code behind, by passing the Stream as a parameter to the Load method of PdfViewerControl. Refer to the following code to perform the same.

using System.IO;
using System.Windows;

namespace PdfViewerDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        # region Constructor
        public MainWindow()
        {
            InitializeComponent();

            FileStream stream = new FileStream(@"HTTP Succinctly.pdf", FileMode.Open);

            //Load PDF file using stream.
            pdfViewer.Load(stream);
        }
        #endregion
    }
}

View PDF file using the ItemSource property

You can also view a PDF file using the ItemSource property of PdfViewerControl. The property accepts the string file path as well as Stream.

using System.Windows;

namespace PdfViewerDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        # region Constructor
        public MainWindow()
        {
            InitializeComponent();

            //Load PDF file using the` ItemSource` property.
            pdfViewer.ItemSource = @"HTTP Succinctly.pdf";
        }
        #endregion
    }
}

View PDF files without using the toolbar

The PdfDocumentView control allows you to view the PDF files without toolbar using the Load methods from code behind. Refer to the following steps to perform the same.

  1. Add the PdfDocumentView control in the MainWindow.xaml.

     <Window 
         x:Class="PdfViewerDemo.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         WindowState="Maximized"
         xmlns:syncfusion="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF">
         <Grid>
             <syncfusion:PdfDocumentView x:Name="pdfViewer"></syncfusion:PdfDocumentView>
         </Grid>
     </Window>
  2. Load the file using the Load method as mentioned in the following code snippet in MainWindow.xaml.cs.

     using System.Windows;
    	
     namespace PdfViewerDemo
     {
         /// <summary>
         /// Interaction logic for MainWindow.xaml
         /// </summary>
         public partial class MainWindow : Window
         {
             # region Constructor
             public MainWindow()
             {
                 InitializeComponent();
    
                 //Load PDF file using file path.
                 pdfViewer.Load(@"HTTP Succinctly.pdf");
             }
             #endregion
         }
     }
  3. Run the project.

The following picture illustrates how the PDF file being displayed in PdfDocumentView control. PDF file in PdfDocumentView

Obtain the PDF file information

You can get the information on the PDF file that is being displayed in the control using the DocumentInfo property of PdfViewerControl. This property provides you the information such as file name and the folder name from that the PDF file is opened using the FileName and FilePath properties respectively.

Refer to the following code to obtain the document information using the DocumentInfo property.

using System.IO;
using System.Windows;

namespace PdfViewerDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        # region Constructor
        public MainWindow()
        {
            InitializeComponent();

            //Load PDF file with the absolute file path.
            pdfViewer.Load(Path.GetFullPath(@"HTTP Succinctly.pdf"));

            //Get the file name
            string fileName = pdfViewer.DocumentInfo.FileName;
            string folder = pdfViewer.DocumentInfo.FilePath;

        }
        #endregion
    }
}

If you open a file using the toolbar, you can obtain the information in the DocumentLoaded event. Refer to the following code to achieve the same.

using System.Windows;

namespace PdfViewerDemo
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        # region Constructor
        public MainWindow()
        {
            InitializeComponent();

            //wire the DocumentLoaded event
            pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
        }
        #endregion

        private void PdfViewer_DocumentLoaded(object sender, System.EventArgs args)
        {
            //Get the file name
            string fileName = pdfViewer.DocumentInfo.FileName;
            string folder = pdfViewer.DocumentInfo.FilePath;
        }
    }
}