Tutorials

How to Manually Integrate WPF PDF SDK in C#

By ComPDFKit | Thu. 05 Sep. 2024
WindowsC#PDF SDK

PDF is a widely used document format, popular for its cross-platform consistency and high security. PDF SDK provides a set of tools and libraries to enable developers to integrate and manipulate PDF documents within their applications.

 

ComPDFKit PDF SDK for Windows specifically supports the WPF and UWP frameworks. Developers can opt for manual integration or integration via NuGet to create a WPF PDF viewer in C#.

 

This article provides a comprehensive guide to help you manually integrate ComPDFKit PDF SDK to create WPF PDF viewer to enable more PDF operations.

 

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free

 

 

Requirements for Integrating WPF PDF SDK

 

Before you begin manually integrating the Windows for C# PDF SDK, please ensure that you meet the following requirements:

 

  • Windows 7, 8, 10, and 11 (32-bit and 64-bit).
  • Visual Studio 2017 or higher (Make sure the .NET Desktop Development is installed).
  • .NET Framework 4.5 or higher.

 

Now, please follow the steps to manually integrate WPF PDF SDK!

 

 

1. Create a New WPF Project

 

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

 

Step 2: Choose WPF App (.NET Framework) and click Next.

 

Step 3: Set up your project by naming it and selecting a storage location. Once configured, click "Create" to initiate the project. For this tutorial, we've named it "ComPDFKit Demo" and chosen .NET Framework 4.6.1 as the foundation.

 

 

2. Add ComPDFKit to Your Project by Manual Integration 

 

Now, you need to manually integrate the ComPDFKit PDF SDK into your project by following the steps below.

 

Step 1: First, open the ComPDFKit SDK package that you have extracted. 

 

Step 2: Then, go to the "lib" folder. Copy the "x64" folder, "x86" folder, "ComPDFKit.NET.dll" file, and "ComPDFKit.Viewer.dll" file to the folder with the same name as your project that you created in  2.4.1. 

 

Step 3: In this sample project, we named the folder as "ComPDFKit Demo". Now, your folder should look like this:

 

Step 4: After that, click the Show All Files button in the Solution Explorer menu, find and right-click the file you added previously, and select the Include in Project option.

 

Now the structure of your project will look like the below image:

 

Step 5: To use the API of ComPDFKit PDF SDK in your project, follow the instructions to add it to References. Right-click the project to which you need to add ComPDFKit PDF SDK and click Add. Then click Reference.

 

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.NET.dll" and "ComPDFKit.Viewer.dll" dynamic library. Then, click OK.

 

Right-click  "ComPDFKit.dll"  -> click  Properties.

 

Please make sure to copy the property of "ComPDFKitNative.dll" to the "Output Directory" and set it as "Copy if newer". Otherwise you need to manually copy it to the same folder as the executable before running the project.

 

 

3. Apply the License Key

 

Before using any class in ComPDFKit PDF SDK, you must apply for a license. ComPDFKit is available under a commercial license. But it still provides a 30-day free license. To obtain a free license, you need to contact our ComPDFKit team.

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free

 

Depending on the license type you can use offline authentication or online authentication.

 

3.1 Online Authentication

 

You can perform online authentication using the following approach:

public static bool LicenseVerify()
{
	if (!CPDFSDKVerifier.LoadNativeLibrary())
	{ 
		return false;
	}
	LicenseErrorCode status = CPDFSDKVerifier.OnlineLicenseVerify("Input your license here.");
	return status == LicenseErrorCode.E_LICENSE_SUCCESS;
}

 

Note: You can confirm the communication status with the server during the online authentication process to implement the `CPDFSDKVerifier.LicenseRefreshed` callback:

CPDFSDKVerifier.LicenseRefreshed += CPDFSDKVerifier_LicenseRefreshed;

private void CPDFSDKVerifier_LicenseRefreshed(object sender, ResponseModel e)
{
    if(e != null)
    {
        string message = string.Format("{0} {1}", e.Code, e.Message);
        Trace.WriteLine(message);
    }
    else
    {
        Trace.WriteLine("Network not connected."); 
    } 
}

 

3.2 Offline Authentication

 

Here we show you the `LicenseVerify()` method that implements offline authentication:

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

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

 

 

4. Display a PDF Document 

 

So far, we have completed all the preparation steps. Now, let's start displaying PDF files!

 

Add the following code in "MainWindow.xaml" and "MainWindow.xaml.cs" to display PDF documents.

 

Please make sure to replace "ComPDFKit_Demo" with your project name. And create a `CPDFViewer` object, and then use the `OpenPDF_Click` method to display the `CPDFViewer` object in a grid (component) named "PDFGrid".

 

Step 1: Replace the content of "MainWindow.xaml" with the following code:

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

 

Step 2: Also replace the content of “MainWindow.xaml.cs” with the following code:

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

 

Note: Replace the `(Input your license here)` portion of the code with your license key.

 

Step 3: Run the project, and you will see the PDF file that you want to display.  The PDF Viewer has been created successfully! 

 

 

5. Troubleshooting

 

If you encounter the following faults, you can easily troubleshoot them according to our methods.

 

5.1 Trouble & Method

 

Trouble: If "System.IO.FileNotFoundException" occurred in the `LicenseVerify()` function like this: 

 

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

 

5.2 Other Problems

 

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

 

 

 Final Words

 

In conclusion, manually integrating WPF PDF SDK in C# is not difficult. By following our steps, from project creation to manually integrating ComPDFKit PDF SDK, and finally displaying PDF files, you can easily create a Windows for C# PDF viewer to display your PDF documents.

 

However, displaying PDF files is only part of the functionality of ComPDFKit PDF SDK. This comprehensive SDK also supports adding more PDF features, such as adding annotations, creating and editing tables, PDF conversion, etc., which can significantly improve the user experience.

 

In addition, ComPDFKit provides a 30-day free trial and responsive technical support, ready to help you solve any problems you may encounter during the integration process. If you have any questions about the integration of C# PDF library, you can find detailed information in the developer documentation.

Windows   Web   Android   iOS   Mac   Server   React Native   Flutter   Electron
30-day Free