Taking the ComPDFKit Windows PDF Library as an example, this article will guide you step by step through the entire process, from downloading and installing the SDK, referencing the PDF library, to viewing PDF documents. It ensures that developers understand the steps involved in integrating third-party libraries on the Visual Studio platform.
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
- License of ComPDFKit Windows PDF Library
Step 1: Get ComPDFKit Windows PDF Library Package
Before your integration, You can contact us or go to github to access our PDF SDK installation package. The SDK package includes the following files.
Step 2: Get License for ComPDFKit Windows PDF Library
You can get a 30 days’ trial license by downloading the package on Github. If you want to buy a license for the Windows PDF library, contact our sales.
Step 3: Create a New Windows Project
- Fire up Visual Studio 2022, click Create a new project.
- Choose WPF APP (.NET Framework) and click Next.
- 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.
- Click the Create button. Then, the new project will be created.
Step 4: Add the Package of ComPDFKit Windows PDF Library through Nuget
- Open your project’s solution, and in the Solution Explorer, right-click on References and click on the menu item Manage NuGet Packages…. This will open the NuGet Package Manager for your solution.
- Search for ComPDFKit.NetFramework, and you’ll find the package on nuget.org.
- On the right side, in the panel describing the package, click on the Install button to install the package.
- Once that is complete, you’ll see a reference to the package in the Solution Explorer under References.
Step 5: Apply the License Key for Your Windows PDF Viewer and Editor
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 6: Display PDFs with the Created Windows PDF Viewer and Editor
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 the 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.
Troubleshooting
1. If System.IO.FileNotFoundException
occurred in the LicenseVerify
function like this:
Check your WPF project and ensure that you chose WPF APP(.NET Framework) instead of WPF Application when creating the project.
2. Other Problems
If you meet some other problems when integrating our ComPDFKit PDF SDK for Windows, feel free to contact ComPDFKit team.