PDF files are widely utilized across various fields such as education, construction, government, aviation, business, and more. Windows remains the favored operating environment for both enterprise and individual users. However, there are instances where reading PDF files within Windows applications can present challenges.
In this article, we will demonstrate how to effortlessly create a C# PDF Viewer to display PDF documents within a Windows application using ComPDFKit — a C# PDF library tailored for .NET developers.
1. Download C# PDF Library for Windows
To build a Windows PDF viewer or editor, we’ll utilize the ComPDFKit C# PDF Library. This comprehensive SDK offers a wide range of PDF functionalities, including viewing, annotations, content editing, page manipulation, conversion, data extraction, signatures, and more. Moreover, its PDF rendering capability is fast and accurate. You can easily access the SDK by downloading it from GitHub or NuGet, or by contacting our sales team for a free trial.
Requirement
Before creating a PDF viewer for Windows, please ensure that your system meets the following requirements:
• Operating System: Windows 7, 8, 10, and 11 (32-bit and 64-bit).
• Visual Studio: Version 2017 or higher (Make sure the .NET Desktop Development is installed).
• .NET Framework: Version 4.5 or higher.
• ComPDFKit Library: Downloaded and installed from GitHub, NuGet, or obtained from our sales team for a free trial.
Ensuring your system meets these requirements will ensure a smooth development process for your PDF viewer application.
2. Create a New Windows Application in Visual Studio
Step 1: Start 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. After completing the setup, click Create to create the project. In this example, we have chosen .NET Framework 4.6.1 as the framework and named the project ComPDFKit Demo.
3. Install C# PDF Library using NuGet Package Manager
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…. This will 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: On the right side, in the panel describing the package, click on the Install button to install the package.
Step 4: Once that is complete, you’ll see a reference to the package in the Solution Explorer under References.
4. Apply License Key before Using any Classes from Library
Before utilizing any classes from the library, it’s essential to apply the license key. ComPDFKit is accessible under a commercial license, but it also provides a free trial license. You can obtain the free trial license by contacting our sales team.
Here’s how you can apply the license key in your code:
bool LicenseVerify()
{
if (!CPDFSDKVerifier.LoadNativeLibrary())
return false;
LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify("Input your license here.", false);
return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
}
Replace “Input your license here.” with the actual license key provided to you by our sales team. Applying the license key ensures that you can utilize the features of ComPDFKit library within the terms of the license agreement.
Now that you’ve installed the ComPDFKit C# PDF Library in your project and applied the license key, let’s proceed with the next step to create a PDF viewer and display a PDF document on Windows.
5. Create a PDF Viewer and Display a PDF Document
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 OpenPDF_Click
method.
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.
Step 1: Replace the content of the “MainWindow.xaml” file in your project with the following code:
<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>
Step 2: Replace the content of the “MainWindow.xaml.cs” file in your project with the following code. Please note: You need to enter your license key. 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 ComPDFKitViewer.PdfViewer;
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.", false);
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.
CPDFViewer pdfViewer = new CPDFViewer();
pdfViewer.InitDocument(dlg.FileName);
if (pdfViewer.Document != null &&
pdfViewer.Document.ErrorType == CPDFDocumentError.CPDFDocumentErrorSuccess)
{
pdfViewer.Load();
PDFGrid.Children.Add(pdfViewer);
}
}
}
}
}
Step 3: Run the project and you will see the PDF file that you want to display. The PDF Viewer for Windows has been created successfully!