How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Viewer Library
How to preview, view PDF files in C# .NET Core WinForms Windows application?

How to Use C#.NET XDoc.PDF Component to open, preview PDFs in .NET WinForms (Core and Framework) Windows applications.

XDoc.PDF for .NET is an easy to use PDF C# library to read, preview, view Adobe PDF files in .NET Core Windows (WinForms, WPF) application and ASP.NET Core web apps.

In this C# tutorial will guide you how to build a PDF preview app in in your C# WinForms (.NET Core) application. You can easily select a target folder, and preview all PDFs inside.

Using PDF C# viewer library we will:

  • Create a WinForms (.NET Core) application using C#.NET
  • Preview all PDF files in one folder

Download and Install XDoc.PDF Viewer C# library

  1. Download XDoc.PDF Viewer C# library
  2. Install PDF C# library to preview PDF files in .NET Core WinForms projects
  3. Step by Step Tutorial

How to preview PDF files using C# in WinForms (.NET Core) application?

1. Create a new WinForms project with XDoc.PDF library installed

Here we will create a new Windows Forms App project with name "RasterEdgePDFWinFormDemo". We have provided detailed guide to install XDoc.PDF library on a new .NET Windows Forms (WinForms) project. Please view here: How to create a new .NET WinForms app using Visual Studio 2022 to manipulate PDFs?

2. Add WinForms controls

Use Toolbox to add controls to the form "Form1".

Before add any controls, changes the property Size of control "Form1" to "960, 600" in the Properties window first.

List of controls to add (in turn)

  • A StatusStrip: With a ProgressBar and a StatusLabel
  • A Button
  • A ListView
  • A FolderBrowserDialog

2.1 Add the controls to the form

Add a StatusStrip control with the default name "statusStrip1"

Add a ProgressBar control in the "statusStrip1" with default name "toolStripProgressBar1"; and then add a StatusLabel control with name "toolStripStatusLabel1"

add a Button control with the default name "button1" and then changes the property Text to "Browser".

And then changes its property Dock to "Bottom" and set the height of the button to 50.

add a ListView control to the form with the default name "listView1" and then changes the property Dock to "Fill".

Add a FolderBrowserDialog control with the default name "folderBrowserDialog1" to the Form1.

2.2 Add button events

Double click Button "button1" to add Click event "button1_Click".

2.3 Add code to the form

Select "View Code" from the menu to open the Form1.cs window.

Replace all contents in the Form1.cs file by following codes.

using System.ComponentModel;
using RasterEdge.XDoc.PDF;

namespace RasterEdgePDFWinFormDemo
    public partial class Form1 : Form
        // Image size of the item in the ListView.
        //  Maximum to 256 * 256.
        private Size _listItemImageSize = new Size(256, 256);

        private BackgroundWorker _bgWorker;
        private int _totalFileCount = 0;

        public Form1()

            //  Initial
            this.Text = "Folder:";
            this.toolStripStatusLabel1.Text = "";

            //  Initial ListView.
            this.listView1.BackColor = SystemColors.ControlDark;
            this.listView1.View = View.LargeIcon;
            this.listView1.LargeImageList = new ImageList();
            this.listView1.LargeImageList.ImageSize = this._listItemImageSize;

            //  Initial a BackgroundWorder object for load previews of PDF files.
            this._bgWorker = new BackgroundWorker();
            this._bgWorker.WorkerReportsProgress = true;
            this._bgWorker.WorkerSupportsCancellation = true;
            this._bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            this._bgWorker.ProgressChanged += bgWorker_ProgressChanged;
            this._bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);

        private void button1_Click(object sender, EventArgs e)
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                // Reset the ListView.
                String targetFolder = this.folderBrowserDialog1.SelectedPath;
                    if (String.IsNullOrEmpty(targetFolder))
                        throw new Exception("Selected folder path is null or empty.");
                    String[] filePaths = Directory.GetFiles(targetFolder, "*.pdf", SearchOption.TopDirectoryOnly);
                    if (filePaths == null || filePaths.Length == 0)
                        throw new Exception("No PDF file in the target folder.");
                    this.Text = "Folder: " + targetFolder + "\\";
                    this._totalFileCount = filePaths.Length;
                    this._bgWorker.RunWorkerAsync(new List<String>(filePaths));
                catch (Exception ex)
                    this.Text = "Folder: [Invalid]";
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
            if (e.Argument == null)
            int percentProgress = 0;
            List<String> filePaths = (List<String>)e.Argument;
            for (int i = 0; i < filePaths.Count; i++)
                String filePath = filePaths[i];
                String fileName = Path.GetFileName(filePath);
                Image bitmap;
                    // Load PDF file and get Bitmap of the 1st page.
                    PDFDocument doc = new PDFDocument(filePath);
                    Bitmap pageImg = doc.GetPage(0).GetBitmap();
                    // Resize the page image to fit the size of the image list item.
                    bitmap = resizeImage(pageImg, this._listItemImageSize);
                catch (Exception)
                    // Return a dummy image if load PDF file failed.
                    bitmap = new Bitmap(1, 1);
                percentProgress += 100 / filePaths.Count;
                new { Image = bitmap, FileName = fileName, Index = i });

        // Resize the source Bitmap object to the specified size with using fit height mode.
        private Image resizeImage(Bitmap origImage, Size canvasSize)
            // Get the scale ratio of the image.
            float ratio = (float)canvasSize.Height / (float)origImage.Height;
            // Get the image width after resizing.
            float w = (float)origImage.Width * ratio;
            // Center alignment
            // Get the X position of the page image in the result image.
            float xShift = ((float)canvasSize.Width - w) / 2F;
            Bitmap result = new Bitmap(canvasSize.Width, canvasSize.Height);
            using (Graphics g = Graphics.FromImage(result))
                // Set background to transparent.
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(origImage, xShift, 0, w, canvasSize.Height);
                // Draw a DarkGray border for the ViewItem.
                g.DrawRectangle(new Pen(Color.DarkGray), 0, 0, canvasSize.Width - 1, canvasSize.Height - 1);
            return result;

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            if (e.UserState != null)
                dynamic data = e.UserState;
                // Add new item to ListView.
                this.listView1.Items.Add(new ListViewItem(data.FileName, data.Index));
            // Update progress bar and status label.
            this.toolStripProgressBar1.ProgressBar.Value = e.ProgressPercentage;
            int loaded = (int)((float)e.ProgressPercentage * (float)this._totalFileCount / 100F + 0.5F);
            this.toolStripStatusLabel1.Text = loaded + " files loaded - Total " + this._totalFileCount + " files";

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            if (e.Cancelled)
                MessageBox.Show("Task Cancelled.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else if (e.Error != null)
                MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

3. It is done. Now press "Ctrl+F5" to run the project.

Screenshot for the Demo application.

Press "Browser" button to browse and select a folder contains PDF files to preview

Then, the list view control would show the preview images for all PDF files in the target folder.


Now you can easily create a simple PDF preview C# Windows application which allows users to preview all PDF files in a target folder. With XDoc.PDF C# library, you can add more PDF features such as conversion, editing, generating and printing.

RasterEdge has also developed a ASP.NET (Core and Framework) web control to view, preview PDFs in web browser. Please view the details here: How to view PDF file in web browser in C# ASP.NET Core MVC app?