Tutorials

How to Build a Windows PDF Viewer or Editor

By ComPDFKit | Wed. 19 Apr. 2023
WindowsC#

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()
{
    if (!CPDFSDKVerifier.LoadNativeLibrary())
        return false;

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

 

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"
        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>

 

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 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);
            }
        }
    }
}


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.