Xiaobai entry-level - based on C # on the Windows platform using ComPDFKit to develop a PDF reader
foreword
As the demand for handling PDF documents continues to rise, seamlessly integrating ComPDFKit PDF viewing and editing functions into your Windows applications or systems will bring an extraordinary experience to your users.
We will first explore together the key steps needed to integrate the ComPDFKit PDF SDK, and then use ComPDFKit to build an excellent Windows PDF reader. Whether you want to enhance the functionality of an existing application or create a new PDF processing tool, this article will provide you with clear guidance to help you achieve an excellent PDF processing experience on the Windows platform. Let's embark on this exciting journey together!
Before the start of the journey, you need to apply for a trial to obtain a license key. In addition, we also provide you with product introduction and development documents:
Free trial application: https://www.compdf.com/contact-salesProduct
introduction: https://www.compdf.com/pdf-sdk
development documentation: https://www.compdf.com/guides/pdf-sdk/windows/make-a-program
Step 1: Environment Construction
First, you need to download Visual Studio 2017 or later, click "Create New Project"
Then, select WPF Application (.NET Framework) and click "Next".
Configure Project: Set the project name and choose where to store the program. For example, we take the project name as "ComPDFKit Demo", use .NET Framework 4.7.2 as the programming framework, click the "Create" button, and a new project will be created.
Step 2: SDK Integration
You can add ComPDFKit to your project through Nuget integration and two methods, you can choose the method that suits your needs:
Method 1: Integration via NuGet
1.1 Integration via NuGet - Online Integration
In Solution Explorer, right-click References, click "Manage NuGet Packages...".
Search for "ComPDFKit.NetFramework" and find this package on nuget.org. It should be noted here that there are many installation packages with the same name. When selecting a package, pay attention to whether the package icon is consistent with the picture below.
Click the install button, as shown below
After clicking the install button, a pop-up window will appear, click OK to continue the installation
After the installation is successful, you can see a green check mark in the lower right corner of the package icon
1.2 Integration via NuGet - Offline Integration
Instead of targeting packages on nuget.org, you can set a configuration to point to local packages. In this way, the software package can also be downloaded offline.
First, you can find the “ComPDFKit.NetFramework…nupkg” file in the SDK package obtained after the trial application is passed.
Copy this file to your new project folder.
Create a "nuget.config" file (for example, "ComPDFKitDemo.sln") in the same directory as your solution file.
To fill in the newly created "nuget.config" file, please copy and paste the entire content of the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="ComPDFKitSource" value="path\to\directoryContainingNupkg" />
</packageSources>
</configuration>
After the copy is complete, you need to modify the value in the above code to refer to the directory location containing the "ComPDDFKit.NetFramework…nupkg" package—for example, the path "\Users\User-103\source\repos" in my example above \ComPDFKit Demo" to replace the existing value. Here you can directly copy the folder path and paste it.
Now save the file, close and reopen the solution to force Visual Studio to read the NuGet configuration.
Open the project's solution, and in the Solution Explorer, right-click on References and click on "Manage NuGet Packages...". This will open the NuGet package manager for the solution.
On the right side of the manager, in the Package Sources drop-down window, select the entry ComPDFKitSource (or whatever name you decide).
It should be noted here that it is best to enter the package source settings before selecting the package source, and uncheck other package sources, so as to avoid interference when loading the package source.
Enter the software package name, and the software will appear in the software package column below.
It should be noted here that there are many software packages with the same name. You must look for the software package icon in the figure below and choose to download
On the right, the package is described in the panel, click the "Install" button to install the package.
After clicking the install button, a pop-up window will appear, click OK to continue the installation.
After the installation is successful, there will be a green check mark in the lower right corner of the package icon.
Method 2: Manually Integrate the SDK - Works Offline
From the ComPDFKit you obtained, copy the following four files to the ComPDFKit Demo folder of your new project (if your new project is to name something else, then select it under the corresponding folder).
Copy the four files shown below.
Paste into your newly created folder, and your folder should look like this when you're done.
Then go into Visual Studio, click the "Show All Files" button in the "Solution Explorer" menu.
Include both files into the project.
In addition, the two files in the figure below also need to be included in the project, and the operation steps can be seen in the figure above.
You can see when the operation is complete.
Then add a reference.
Add the two ComPDKit.dlls in the x64 x86 folder to the reference manager browse module.
After adding successfully, you can see the picture below.
After the addition is successful, click the Confirm button to enter the next step.
Then modify the properties of the two dll files to modify, first enter the properties window.
Change the value copied to the output directory to: copy if it is newer, as shown in the figure below. After setting this step, all the steps of the entire manual integration software development kit are completed.
Step 3: Code writing
Now that you have your environment ready, let's modify the sample code to display a PDF file!
1. Sample code modification
In "MainWindow.xaml" and "MainWindow.xaml.cs", add the following code snippets.
Add the following code in "MainWindow.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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" UseLayoutRounding="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="52"/>
</Grid.RowDefinitions>
<Grid Name="PDFGrid" Grid.Row="0" />
<Button Content="Open PDF" Grid.Row="1" HorizontalAlignment="Left" Margin="10" Click="OpenPDF_Click"/>
</Grid>
</Window>
An example is shown in the figure below:
Add the following code in "MainWindow.xaml.cs"
using ComPDFKit.NativeMethod;
using ComPDFKit.PDFDocument;
using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using System.Windows;
namespace ComPDFKit_Demo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LicenseVerify();
}
bool LicenseVerify()
{
bool result = CPDFSDKVerifier.LoadNativeLibrary();
if (!result)
return false;
// You should fill in your key and secret into the string below.
string key = "Input your key instead of this string";
string secret = "Input your secret instead of this string";
LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify(key, secret);
if (verifyResult != LicenseErrorCode.LICENSE_ERR_SUCCESS)
return false;
return true;
}
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.
CPDFViewer pdfViewer = new CPDFViewer();
pdfViewer.InitDocument(dlg.FileName);
if (pdfViewer.Document != null &&
pdfViewer.Document.ErrorType == CPDFDocumentError.CPDFDocumentErrorSuccess)
{
pdfViewer.Load();
PDFGrid.Children.Add(pdfViewer);
}
}
}
}
}
An example is shown in the figure:
It should be noted here that you need to obtain it through the trial link mentioned at the beginning. After the trial application is passed, the official will give you a trial demo and license key for free.
Open the above xml file, and replace the key with the values of key and secret in the code.
2. Code display
At this point, we have set up the environment and modified the sample code. We only need to click the start button to see the running effect of the code.
Summarize
In just three simple steps, you can easily integrate brand new applications. However, the actual functions of ComPDFKit are far from limited to this. In addition to providing easy integration, it also has a wide range of PDF functions, including reading, navigation, annotation, format conversion, form processing, security protection, document editing and page editing, etc. This PDF SDK has a high degree of interface customization capability, and you can freely adjust the style of the software according to your personal needs. Of course, this is only part of the function.
If you are eager to experience more powerful functions, apply for a trial now! We will provide you with detailed technical documents and trial demos, so that you can deeply understand the infinite possibilities that ComPDFKit can bring. Look forward to working with you!