How to Build a Windows PDF Viewer or Editor

Tutorials | Integration · PDF Viewer · Windows Wed. 19 Apr. 2023

In today's world, Windows apps are essential to our work. And, with the increasing need to work with PDF documents, integrating ComPDFKit PDF viewing and editing features into your Windows apps or systems, can greatly bring your end users a wonderful user experience.

 

In this blog, we'll start by exploring the necessary steps to integrate the ComPDFKit PDF SDK, and build a Windows PDF Reader with ComPDFKit.

 

 

Get Started with ComPDFKit Windows PDF SDK

 

ComPDFKit is a powerful PDF SDK. It is easy to embed ComPDFKit PDF SDK in your Windows application with a few lines of C# code. Take just a few minutes and get started.

 

The following sections introduce the requirements, structure of the installation package, and how to make a Windows PDF Reader in C# with ComPDFKit PDF SDK.

 

Requirements

 

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

         - Visual Studio 2017 or higher.

         - .NET Framework 4.6.1 or higher.

 

Windows Package Structure

 

You can contact us to access our PDF SDK installation package. The SDK package includes the following files.

         - ComPDFKit.Demo - A folder containing Windows sample projects.

         - lib - Include the ComPDFKit dynamic library (x86, x64).

         - api_reference_windows.chm - API reference.

         - developer_guide_windows.pdf - Developer guide.

         - legal.txt - Legal and copyright information.

         - release_notes.txt - Release information.

 

Windows Package Structure of ComPDFKit

 

 

Make a Windows PDF Viewer in C#

 

Step 1: Create a New Project

 

1. Fire up Visual Studio 2022, click Create a new project. 

 

Create a new project

 

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

 

Create a new project 2

 

3. Configure your project: Set your project name and choose the location to store your program. The project name is called "ComPDFKit Demo" in this example. This sample project uses .NET Framework 4.6.1 as the programming framework.

 

Create a new project 3

 

Click the Create button. Then, the new project will be created.

 

Step 2: Add ComPDFKit PDF SDK Package

 

1. Open the ComPDFKit SDK package that you have extracted, and go to the lib folder. Copy the x64 folder, x86 folder, ComPDFKit.Desk.dll file, and ComPDFKit.Viewer.dll file to the folder with the same name as the project that you created in step one. In this project, the folder is named "ComPDFKit Demo". Now, your folder should look like this:  

 

Add ComPDFKit PDF SDK Package

 

2. Then click the button Show All Files in the Solution Explorer menu. Find and right click on the files you added before, and choose Include In Project.

 

Add ComPDFKit PDF SDK Package 2

 

Now the structure of your project will look like this:

 

Add ComPDFKit PDF SDK Package 2.1

 

3. To use the APIs of ComPDFKit PDF SDK in your project, follow the instructions and add them to Reference. Right click on the project that you need to add ComPDFKit PDF SDK and click Add. Then, click Reference.

 

Add ComPDFKit PDF SDK Package 3

 

In the Reference dialog, choose Browse, click another Browse button in the bottom right corner, and navigate to the ComPDFKit Demo folder which is in your project. Select ComPDFKit.Desk.dll and ComPDFKit.Viewer.dll dynamic library. Then, click OK.

 

Add ComPDFKit PDF SDK Package 3.1

 

Add ComPDFKit PDF SDK Package 3.2

 

Right click on ComPDFKit.dll -> click Properites.

 

Add ComPDFKit PDF SDK Package 3.3

 

Please make sure to set the property Copy to Output Directory of ComPDFKit.dll to Copy if newer. Otherwise, you should copy it to the same folder with the executable file manually before running the project.

 

Add ComPDFKit PDF SDK Package 3.4

 

Step 3: Apply the License Key

 

You can contact ComPDFKit team to get a trial license. Before using any ComPDFKit PDF SDK classes, a required operation is to set the license key. Add the following method LicenseVerify() to MainWindow.xaml.cs.

 

        bool LicenseVerify()
        {
            bool result = CPDFSDKVerifier.LoadNativeLibrary();
            if (!result)
                return false;
            string Key = "Input your key instead of this string";
            string Secret = "Input your secret  instead of this string";
            CPDFSDKVerifier.LicenseErrorCode verifyResult =
           	CPDFSDKVerifier.LicenseVerify(Key, Secret);
            if (verifyResult != CPDFSDKVerifier.LicenseErrorCode.LICENSE_ERR_SUCCESS)
                return false;
            return true;
        }

 

Step 4: Display a PDF Document

 

We have finished all preparations. Let's display a PDF file.

 

Add the following code to MainWindow.xaml and MainWindow.xaml.cs to display a PDF document. Please make sure to replace “ComPDFKit Demo” with the name of your project. Now, all you need to do is to create a CPDFViewer object, and then display the CPDFViewer object in the Grid (component) named PDFGrid using the Window_Loaded method.

 

Now your MainWindow.xaml should look like the following code. At this stage, I named Grid as PDFGrid.

 

<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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Loaded="Window_Loaded">
    <Grid Name="PDFGrid">
    </Grid>
</Window>

 

Now your MainWindow.xaml.cs should look like the following code. Please note: you need to enter your license key and set the path of the PDF file to be displayed. All the places that need to be modified in the code have been marked with comments in the code below. You just need to replace the string content below the comments by yourself.

 

using ComPDFKit.NativeMethod;
using ComPDFKit.PDFDocument;
using ComPDFKitViewer.PdfViewer;
using System.Windows;

namespace ComPDFKit_Demo
{
    public partial class MainWindow : Window
    {
        //Just like D:\\ComPDFKit for Windows\\developer_guide_windows.pdf
        private readonly string myPDFPath = "Input your PDF file path instead of this string";

        bool LicenseVerify()
        {
            bool result = CPDFSDKVerifier.LoadNativeLibrary();
            if (!result)
            return false;
            //you should fill in your key and secret into the string below.
            string Key = "Input your key instead of this string";
            string Secret = "Input your secret instead of this string";
            LicenseErrorCode verifyResult =
            CPDFSDKVerifier.LicenseVerify(Key, Secret);
            if (verifyResult != LicenseErrorCode.LICENSE_ERR_SUCCESS)
                return false;
            return true;
        }

        public MainWindow()
        {
            InitializeComponent();
            LicenseVerify();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CPDFViewer pdfViewer = new CPDFViewer();
            pdfViewer.InitDocument(myPDFPath);
            if (pdfViewer.Document != null &&
                pdfViewer.Document.ErrorType == CPDFDocumentError.CPDFDocumentErrorSuccess)
            {
                pdfViewer.Load();
                PDFGrid.Children.Add(pdfViewer);
            }
        }
    }
}


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

 

Display a PDF Document by ComPDFKit

 

 

Troubleshooting

 

1. If System.IO.FileNotFoundException occurred in the LicenseVerify function like this:

 

Troubleshooting 1

 

Check your WPF project and ensure that you chose WPF APP(.NET Framework) instead of WPF Application when creating the project.

 

Troubleshooting 2

 

2. Other Problems

If you meet some other problems when integrating our ComPDFKit PDF SDK for Windows, feel free to contact ComPDFKit team.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!