Tutorials

How to Build a Windows PDF Viewer in C#?

By ComPDFKit | Fri. 08 Dec. 2023
Windows.NETC#

Windows is the preferred working environment for both enterprise and individual users. ComPDFKit offers comprehensive integration for Windows, covering .NET (C#, ASP.NET, ASP.NET Core, and VB.NET), C/C++, Python, and Java.

 

In this article, we will use C# as an example to provide a detailed guide on how to seamlessly integrate the ComPDFKit for Windows into your Windows application.



Requirement

 

Please ensure that your system meets the following requirements:

 

        - Windows 7,8,10,11 (32-bit, 64-bit).

        - Visual Studio 2017 or higher.

        - .NET Framework 4.5 or higher.

 

 

Create a New Project

 

1. Start Visual Studio 2022, click Create a new project.

 

Create a new Windows project | ComPDFKit

 

2. Choose WPF APP (.NET Framework) and click Next.

 

Choose WPF APP (.NET Framework) | ComPDFKit

 

3. Configure your project: Set your project name and choose the location to store your program. After completing the setup, click Create to create the project.


In this example, we have chosen .NET Framework 4.5 as the framework and named the project ComPDFKit Demo.

 

Configure your project | ComPDFKit

 

 

Install the Nuget Package

 

1. Right click on the project that you need to add ComPDFKit for Windows, then click Manager Nuget Packages.

 

Manager Nuget Packages | ComPDFKit

 

2. On the NuGet Package Manager page, select Browse. Set the Package source to nuget.org. Then, search for "ComPDFKit.NetFramework".


After selecting "ComPDFKit.NetFramework" from the search results, in the package description panel, click on the Install button to initiate the package installation.

 

initiate the package installation | ComPDFKit

 

 

Create a PDF Viewer

 

We have finished all prepare steps. Let's display a PDF file: Replace the content of the "MainWindow.xaml" file in your project with the following code:

 

<Window x:Class="ComPDFKit_Demo.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:ComPDFKit_Demo"
        xmlns:compdfkitviewer="clr-namespace:ComPDFKitViewer;assembly=ComPDFKit.Viewer"
        mc:Ignorable="d"
        Focusable="True"
        Title="MainWindow" Height="600" Width="800" UseLayoutRounding="True">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="52"/>
        </Grid.RowDefinitions>
        <Grid Name="PDFGrid" Grid.Row="0">
            <ScrollViewer Focusable="False" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                <compdfkitviewer:CPDFViewer x:Name="PDFViewer"/>
            </ScrollViewer>
        </Grid>
        <Button Content="Open PDF" Grid.Row="1" HorizontalAlignment="Left" Margin="10" Click="OpenPDF_Click"/>
    </Grid>
</Window>
 

 

To use ComPDFKit SDK, you need to input a license key. You can contact the ComPDFKit team to obtain a license key. 

 

Replace the content of the "MainWindow.xaml.cs" file in your project with the following code. Then, replace the "Input your license key here" string in the code below to complete the creation of the PDF viewer.

 

using ComPDFKit.NativeMethod;
using ComPDFKit.PDFDocument;
using Microsoft.Win32;
using System.Windows;

namespace ComPDFKit_Demo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LicenseVerify();
        }

        bool LicenseVerify()
        {
            if (!CPDFSDKVerifier.LoadNativeLibrary())
                return false;

            // Input your license.
            LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify("Input your license here.");
            return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
        }

        private void OpenPDF_Click(object sender, RoutedEventArgs e)
        {
            // Get the path of a PDF file.
            var dlg = new OpenFileDialog();
            dlg.Filter = "PDF Files (*.pdf)|*.pdf";
            if (dlg.ShowDialog() == true)
            {
                // Use the PDF file path to open the document in CPDFViewer.
                CPDFDocument doc = CPDFDocument.InitWithFilePath(dlg.FileName);
                if (doc != null && doc.ErrorType == CPDFDocumentError.CPDFDocumentErrorSuccess)
                    PDFViewer.InitDoc(doc);
            }
        }
    }
}

 

 

Run the Windows PDF Viewer

 

Now run the project, you will see the PDF file that you want to display. The PDF Viewer has been created successfully.

 

ComPDFKit PDF Viewer for Windows

 

 

Adding Even More Capabilities

 

With broad support for popular programming languages such as C#, ASP.NET, ASP.NET Core, VB.NET, C/C++, Python, and Java, ComPDFKit offers a versatile and powerful PDF SDK for all your document management needs on Windows platforms.

 

         - Windows PDF Viewer SDK.

         - Annotate and collaborate: Markup, comment, and reply.

         - Create and fill out forms.

         - Edit PDF text, images, and pages.

         - Sign documents with electronic or digital signatures.

         - Redact and remove sensitive and private information.

         - Extensive PDF processing capabilities.

         - Independently developed PDF technologies with a proven track.

         - Keeping technical updates. Ensure that your application remains at the cutting edge of market requirements.

         - Default and customizable UI.

 

ComPDFKit for Windows: PDF SDK Demo

 

 

Conclusion


In this blog, we showed you how to create a WPF PDF viewer by ComPDFKit for Windows in C#. ComPDFKit for Windows is a powerful and user-friendly PDF SDK. If you have any questions about integrating ComPDFKit for Windows into your project, feel free to contact us and we'll be more than happy to help!