We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to view the PDF pages as thumbnail view in WPF PDFViewer?

Platform: WPF |
Control: PdfViewer |
Published Date: July 20, 2018 |
Last Revised Date: July 28, 2021
Tags: wpf, c#

WPF PdfViewerControl does not support thumbnail view. However, as a workaround, the PDF document pages can be viewed as thumbnail by exporting the pages of the PDF document as images using the PdfLoadedDocument.ExportAsImage() API. Clicking the thumbnail image will navigate to the corresponding page in PdfViewerControl.

Refer to the following code snippet.
C#

int thumbnailZoomFactor = 4;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
  //Loads the document in PdfViewerControl
  PdfViewer.Load(@"..\..\Data\HTTP Succinctly.pdf");
  ThumbnailGrid.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(((int)(((byte)(237)))), ((int)(((byte)(237)))), ((int)(((byte)(237))))));
  PdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
            
}
 
/// <summary>
/// Event triggered once the document has been loaded
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
  ThumbnailGrid.RowDefinitions.Clear();            
  ExportAsImage();
}
 
/// <summary>
/// Convert the PDF pages into images
/// </summary>
private async void ExportAsImage()
{
  int count = PdfViewer.LoadedDocument.Pages.Count;
  float height = PdfViewer.LoadedDocument.Pages[0].Size.Height / thumbnailZoomFactor;
  float width = PdfViewer.LoadedDocument.Pages[0].Size.Width / thumbnailZoomFactor;
  ParentGrid.ColumnDefinitions[0].Width = new GridLength(width + 30);
  for (int i=0;i< PdfViewer.LoadedDocument.Pages.Count;i++)
  {
     RowDefinition row = new RowDefinition();
     row.Height = new GridLength(height+5);
     ThumbnailGrid.RowDefinitions.Add(row);
     Bitmap bitMap = new Bitmap(await Task.Run(() => PdfViewer.LoadedDocument.ExportAsImage(i)), (int)width,(int)height);
     var memory = new MemoryStream();
     bitMap.Save(memory, ImageFormat.Png);
     memory.Position = 0;
     BitmapImage bitmapImage=new BitmapImage();
     bitmapImage.BeginInit();
     bitmapImage.StreamSource = memory;
     bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
     bitmapImage.EndInit();
     bitmapImage.Freeze();
     System.Windows.Controls.Image image = new System.Windows.Controls.Image()
     {
       Source = bitmapImage
     };
     image.Height = height;
     image.Width = width;
     image.MouseUp += Image_MouseUp;
     Grid.SetRow(image, i);
     ThumbnailGrid.Children.Add(image);
               
  }            
}
 
/// <summary>
/// Event triggered once click the thumbnail images
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Image_MouseUp(object sender, MouseButtonEventArgs e)
{
  System.Windows.Controls.Image img = sender as System.Windows.Controls.Image;
  int pageNumber = (int)img.GetValue(Grid.RowProperty);
  PdfViewer.GoToPageAtIndex(pageNumber+1);
} 

XAML

<Window x:Class="SampleWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SampleWPF"
        mc:Ignorable="d"
        xmlns:Syncfusion="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid x:Name="ParentGrid"  >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="103*"></ColumnDefinition>
            <ColumnDefinition Width="414*"></ColumnDefinition>
 
        </Grid.ColumnDefinitions>
        <Syncfusion:PdfViewerControl Grid.Column="1" x:Name="PdfViewer" />
        <ScrollViewer  Grid.Column="0">
             <Grid  x:Name="ThumbnailGrid" ScrollViewer.VerticalScrollBarVisibility="Visible" >        
             </Grid>
          </ScrollViewer>
    </Grid>
</Window>

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
sanjyt
Apr 25, 2019

enter image description here

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile