Windows
ComPDFKit PDF SDK
Get Started

How to Make a Windows Program in C# with ComPDFKit PDF SDK

 

This section will help you to quickly get started with ComPDFKit PDF SDK to make a windows project in C# with step-by-step instructions, which includes the following steps:

 

1. Create a new Windows project in C#.

2. Integrate ComPDFKit into your project.

3. Apply the license key.

4. Display a PDF document.

 

Create a New Windows Project in C#

 

In this guide, we use Visual Studio 2017 to create a new Windows project.

 

1. Fire up Visual Studio 2017, choose File -> New -> Project..., and then select Visual C#->Windows Desktop -> WPF App(.NET Framework) as shown in Figure 2-2.

 

Create a New Windows Project in C#

Figure 2-2

2. Choose the options for your new project as shown in Figure 2-3. Please make sure to choose .NET Framework 4.6.1 as the programming framework.

 

Create a New Windows Project in C#

 

Figure 2-3

 

3. Place the project to the location as desired. Then, click OK.

 

Integrate ComPDFKit PDF SDK into your projects

 

1. Copy the "ComPDFKit.Desk.dll" and "ComPDFKit.Viewer.dll" DLL files and "x64" or "x86" folder depending on your build configurations in the "lib" folder to the project "PdfViewer" folder.

 

2. Add ComPDFKit PDF SDK dynamic library to References. In order to use ComPDFKit PDF SDK APIs in the project, you must first add a reference to it.

 

- In Solution Explorer, right-click the "PdfViewer" project and click Add -> Reference…

 

Integrate ComPDFKit PDF SDK into your projects

 

 - In the Add Reference dialog, click Browse tab, navigate to the "PdfViewer" folder, select "ComPDFKit.Desk.dll" and "ComPDFKit.Viewer.dll" dynamic library, and then click OK

 

Integrate ComPDFKit PDF SDK into your projects

 

3. Add "ComPDFKit.dll" to the project. Include the "x64" or "x86" folder in the project. 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.

 

Integrate ComPDFKit PDF SDK into your projects

 

Apply the License Key

 

It is important that you set the license key before using any ComPDFKit PDF SDK classes.

 

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

    string licenseKey = "***";
    string licenseSecret = "***";
    LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify(licenseKey, licenseSecret);
    if (verifyResult != LicenseErrorCode.LICENSE_ERR_SUCCESS)
        return false;

    return true;
}

 

Display a PDF Document

 

So far, we have added "ComPDFKit.Desk.dll", "ComPDFKit.Viewer.dll" and "ComPDFKit.dll" to the "PdfViewer" project, and finished the initialization of the ComPDFKit. Now, let’s start building a simple PDF viewer with just a few lines of code.

 

Then, add the following code to MainWindow.xaml and MainWindow.xaml.cs to display a PDF document. It’s really easy to present a PDF on screen. All you need is to create a CPDFDocument object and then show it with a CPDFViewer object.

 

MainWindow.xaml

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

    </Grid>
</Window>

 

MainWindows.xaml.cs

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

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

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string pdfPath = "";
            CPDFViewer pdfViewer = new CPDFViewer();
            pdfViewer.InitDocument(pdfPath);

            if (pdfViewer.Document != null && pdfViewer.Document.ErrorType == CPDFDocumentError.CPDFDocumentErrorSuccess)
            {
                pdfViewer.Load();
                PDFGrid.Children.Add(pdfViewer);
            }
        }
    }
}