Skip to main content

Get Started with WinForms PDF Viewer (.NET Framework)

  • 4 minutes to read

This tutorial illustrates how to use the WinForms PDF Viewer control to create a simple PDF Viewer application and adjust its appearance. The application targets .NET Framework.

The following controls are used in this tutorial:

Watch Video: WinForms PDF Viewer: Getting Started

The DevExpress Template Gallery contains a predefined template for building a WinForms application.

The steps below show how to quickly create a ready-to-use WinForms PDF Viewer application from the Template Gallery:

  1. Open Visual Studio.

  2. On the start window, select Create a new project.

  3. In Create a new project, select the DevExpress v24.2 WinForms App Template Gallery template.

  4. On the next page, configure your project: specify the project name, location, and solution name. Click Create.

  5. In the DevExpress Template Gallery, select PDFViewer Application in the WinForms Business Solutions category. Specify the target framework if necessary. Then, click Create Project.

    Create a PDF Viewer from a Template Gallery for .NET Framework

  6. Run the project to see the result: Result.

Create a PDF Viewer Application from Scratch

Design Time

Follow this step-by-step guide to create a PDF Viewer Application in Visual Studio:

  1. Run Microsoft Visual Studio and create a new Windows Forms App (.NET Framework) project.

  2. Drop the PdfViewer item from the DX.24.2: Common Controls toolbox tab onto the form.

    Drop the PDF Viewer Item onto the Form

  3. Click the PDF Viewer’s smart tag and select Dock in parent container in the PDFViewer Tasks menu. This allows the PDF Viewer to expand to the form’s size.

    Dock PDF Viewer in Parent Container

  4. Create the Command UI. In the PDFViewer Tasks smart tag menu, select Create Ribbon to add a RibbonControl to the PdfViewer.

    Create Ribbon

  5. In the PdfViewer Tasks smart tag menu, select the required ribbon pages or click Create All Bars to add all available ribbon pages at once.

    Create All Bars

    You can customize the created ribbon afterward: change the ribbon style, add new ribbon elements, and modify or remove the existing items.

    You can also implement a bar interface. For this, select Create Bars instead of Create Ribbon. You can upgrade it to the ribbon interface at any time (for example, when converting the entire application).

    create-bars-bar-manager-convert-to-ribbon

  6. Load a document to the PDF Viewer. Click the PdfViewer’s smart tag to invoke its actions list. In the Actions list, click the Load PDF file… link.

    smart-tag-ribbon-load-pdf-file

    The Open dialog box appears. In this dialog, locate the document you want to display and click Open.

    LoadPDF_OpenDialog

  7. Run the project to see the result: Result.

Runtime

  1. Run Microsoft Visual Studio and create a new Windows Forms App project.

  2. Use the following code to add PdfViewer to a WinForms application at runtime:

    using DevExpress.XtraPdfViewer;
    using System;
    using System.Windows.Forms;
    // ...
    private void Form1_Load(object sender,EventArgs e) {
        // Create a PDF viewer.
        PdfViewer pdfViewer = new PdfViewer();
        // Specify the viewer position on the form.
        pdfViewer.Dock = DockStyle.Fill;
        // Add the PDF viewer to the window.
        Controls.Add(pdfViewer);
    }
    
  3. To load a document, call the PdfViewer.LoadDocument() method: How to: Load a PDF Document from a File.

  4. Create the Command UI. Call PdfViewer.CreateRibbon() to add a RibbonControl to the PdfViewer at runtime.

    using DevExpress.XtraBars.Ribbon;
    using DevExpress.XtraPdfViewer;
    // ...
    //Create all PDF ribbon tabs
    pdfViewer.CreateRibbon();
    
    // You can also use an overload to specify which Ribbon tabs to add:
    // pdfViewer.CreateRibbon(PdfViewerToolbarKind.Main);
    

    You can also implement a bar interface instead of a Ribbon. For this, call the PdfViewer.CreateBars method.

Result

Run the application and try the PDF Viewer features. Load and navigate the document, highlight text, check the attachments, or print the result.

runtime-pdf-viewer-zoom-factor-fit-visible-content

What’s Next

See Also