Viewing PDF Files in WPF Pdf Viewer

13 Apr 202411 minutes to read

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. WPF PDF Viewer 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 a string file path, a Stream, and a PdfLoadedDocument object.

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
    }
}

Refer the below code snippet to load a Stream using the ItemSource property

//Load PDF file as Stream using the `ItemSource` property.
pdfViewer.ItemSource = new FileStream(@"HTTP Succinctly.pdf",FileMode.Open);

Refer the below code snippet to load a PdfLoadedDocument object using the ItemSource property.

//Load PDF file as PdfLoadedDocument object using the `ItemSource` property.
PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(@"HTTP Succinctly.pdf"); 
pdfViewer.ItemSource = pdfLoadedDocument;

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. WPF PDF Viewer File in PdfDocumentView

Avoid exception while loading PDF Document

When opening a PDF document in PdfViewer and attempting to open a new one, unloading the previously opened document is not required. This is due to PdfViewer’s internal process: when loading a new document, it automatically unloads and disposes of any previously opened document. Therefore, attempting to explicitly unload the document may lead to an exception, as this action is already handled internally within the WPF PDF Viewer.

For instance, when attempting to open a document through a button click event, utilize the following code snippet:

private void test_Click(object sender, RoutedEventArgs e)
 {
     //No need to close the existing document because it is internally implemented.
     //pdfViewer.Unload();
     pdfViewer.Load("Document.pdf");    
 }

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;
        }
    }
}

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.