Tutorials

How to Integrate WPF PDF SDK in C# Using NuGet

By ComPDFKit | Thu. 05 Sep. 2024
WindowsViewerPDF SDK

Creating a powerful WPF application to work with PDF files can significantly enhance your project. ComPDFKit PDF SDK for Windows supports WPF and UWP, providing manual integration as well as integration of WPF PDF SDK through NuGet. This can help developers implement WPF PDF viewers in C#, enhance PDF operations, and improve user experience.

 

In this comprehensive tutorial, we will show you the basic steps to integrate ComPDFKit PDF SDK using NuGet to create a WPF PDF viewer to display PDF files.

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

 

 

Requirements

 

Before integrating ComPDFKit PDF SDK using NuGet to create WPF PDF viewer, 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, click Create a new project.

 

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

 

Step 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. And uses .NET Framework 4.6.1 as the programming framework.

 

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

 

 

2. Add ComPDFKit to Your Project by NuGet

 

In this part, you need to integrate ComPDFKit PDF SDK into your project using NuGet.

 

Step 1: Open your project’s solution, and in the Solution Explorer, right-click on References and click on the menu item Manage NuGet Packages… to open the NuGet Package Manager for your solution.

 

Step 2: Search for ComPDFKit.NetFramework, and you’ll find the package on nuget.org.
 
Step 3: Install the package by clicking the Install button in the panel on the right describing the package.
 
Step 4: Once done, you can see how the NuGet package is referenced in the References section in Solution Explorer.
 
 

3. Apply the License Key

 

Before using any class in ComPDFKit PDF SDK, you need to apply your license. ComPDFKit is available under commercial license. Of course, it provides you with a trial license, you can directly contact ComPDFKit team to get a trial license.

 

When applying a license, you need to select the corresponding scheme according to the license type (offline license or online license), offline authentication or online authentication, and apply the license to your application.

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

 

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

 

Now, we have completed all the preparation steps. Next, let's start displaying your PDF file.

 

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

 

Before that, make sure to replace "ComPDFKit_Demo" with your project name and create a `CPDFViewer` object, 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: Replace the contents of "MainWindow.xaml.cs" with the following code.

 

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

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

 

Step 3: Now 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 summary, building a Windows for C# PDF viewer using NuGet to integrate ComPDFKit PDF SDK is a streamlined and efficient process. By following the steps outlined in this guide, you can fully exploit the potential of PDF processing in your WPF applications and enhance your user experience.

 

If you have any questions about integrating the WPF PDF SDK in C# using NuGet, you can find detailed information in the developer documentation. In addition, our technical support team provides timely response services and is ready to help you solve any questions or problems you encounter during the integration process. Please feel free to contact us for timely help and guidance.

 

Get a free 30-day trial of ComPDFKit to start creating a Windows for C# PDF viewer using NuGet today.

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