C# PDF Viewer for Windows Application (Code Samples Tutorial)

Tutorials | PDF Viewer · C# · Windows Fri. 12 Apr. 2024

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.

 

How to Create a C# PDF Viewer for Windows

1. Download C# PDF Library for Windows

2. Create a New Windows Application in Visual Studio

3. Install C# PDF Library using NuGet Package Manager

4. Apply License Key before Using any Classes from the Library

5. Create a PDF Viewer and Display a PDF Document

By following these steps, developers can effectively leverage the ComPDFKit library to build a Windows PDF Viewer using C#.

 

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.

 

Create a new project with Visual Studio 2022

 

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

 

choose WPF App (.NET Framework) in Visual studio

 

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.

 

Configure your project

 

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.

 

Manage NuGet Packages

 

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.

 

install compdfkit csharp pdf library

 

Step 4: Once that is complete, you’ll see a reference to the package in the Solution Explorer under References.

 

a reference to the package in Solution Explorer .png

 

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!

 

Adding More PDF Capabilities

With broad support for popular programming languages such as C#, ASP.NET, ASP.NET Core, VB.NET, C/C++, Python, and Java, ComPDFKit offers a versatile and powerful PDF SDK for all your document management needs on Windows platforms.

     • Default and customizable UI.

     • Annotate and collaborate: Markup, comment, and reply.

     • Create and fill out forms.

     Edit PDF text, images, and pages.

     • Sign documents with electronic or digital signatures.

     • Convert PDF to/from Office files or other format.

     • Extract data from PDFs (text, tables, and images)

     • Redact and remove sensitive and private information.

     • Extensive PDF processing capabilities.

     • Independently developed PDF technologies with a proven track.

     • Keeping technical updates. Ensure that your application remains at the cutting edge of market requirements.

 

Conclusion

In conclusion, by following the steps outlined above, you can effectively integrate a PDF viewer into your Windows application using the ComPDFKit C# PDF Library. After installing the library and applying the license key, you can easily create a PDF viewer by adding the PdfViewer control to your Windows Form, loading a PDF document, and customizing the viewer as needed. This enables you to provide users with the ability to view PDF documents seamlessly within your application. 

 

If you have any questions regarding the integration of the C# PDF library, detailed information can be found in the developer documentation. Additionally, our technical support team is readily available to assist you with any inquiries or issues you may encounter during the integration process. Don't hesitate to reach out to us for prompt assistance and guidance.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!