使用C#基于ComPDFKit SDK快速构建PDF阅读器

在当今世界,Windows 应用程序对我们的工作至关重要。随着处理 PDF 文档的需求不断增加,将 ComPDFKit PDF 查看和编辑功能集成到您的 Windows 应用程序或系统中,可以极大地为您的用户带来美妙的体验。

在本博客中,我们将首先探索集成 ComPDFKit PDF SDK 的必要步骤,并使用 ComPDFKit 构建 Windows PDF 阅读器。

ComPDFKit SDK for Windows 入门

ComPDFKit 是一个功能强大的 PDF SDK。只需数行C#代码即可轻松将 ComPDFKit PDF SDK 嵌入到您的 Windows 应用程序中。让我们用几分钟时间开始使用。

以下部分介绍了配置要求、安装包的结构以及如何通过C#语言,使用 ComPDFKit PDF SDK制作 Windows PDF 阅读器。

要求

  • Windows 7、8、10 和 11(32 位、64 位)。
  • Visual Studio 2017 或更高版本。
  • .NET Framework 4.6.1 或更高版本。

Windows包结构

您可以联系我们获取我们的PDF SDK安装包。

SDK包中包含以下文件:

  • “Examples” - 包含Windows示例项目的文件夹。
  • “lib” - 包含ComPDFKit动态库(x86, x64)的文件夹。
  • “nuget” - 包含ComPDFKit.NetFramework nuget包的文件夹。
  • “api_reference_windows.chm” - API参考文档。
  • “developer_guide_windows.pdf” - 开发者文档。
  • “legal.txt” - 法律和版权信息。
  • “release_notes.txt” - Release信息。

在这里插入图片描述

使用C#构建Windows PDF查看器

第一步:创建一个新项目

  1. 启动Visual Studio 2022, 单击创建新项目
    在这里插入图片描述

  2. 选择“WPF APP (.NET Framework)”,然后单击“下一步”。
    在这里插入图片描述

  3. 配置您的项目:设置您的项目名称并选择存储程序的位置。在本示例中,项目名称称为“ComPDFKit Demo”。此示例项目使用 .NET Framework 4.6.1 作为编程框架。
    在这里插入图片描述

  4. 点击“创建”按钮,至此项目创建完成。

第二步:添加ComPDFKit PDF SDK包

  1. 打开您的项目解决方案,右击“引用”,在右键菜单项中选择“管理Nuget程序包”,这将打开您的项目的NuGet包管理器。
    在这里插入图片描述

  2. 点击“浏览”,设置程序包源为nuget.org,搜索ComPDFKit.NetFramework,您将搜索到“ComPDFKit.NetFramework”包。
    在这里插入图片描述

  3. 选中包后,在右侧包的详情面板中,点击“安装”来下载包。
    在这里插入图片描述

  4. 安装完成后,您现在可以在“解决方案资源管理器”->“引用”中找到对应的包的引用。
    在这里插入图片描述

第三步,应用许可证密钥

您可以联系ComPDFKit团队获取试用许可证,在使用任何ComPDFKit SDK功能之前,需要进行的操作是设置许可证密钥。将以下方法“LicenseVerify()”添加到“MainWindow.xaml.cs”。

bool LicenseVerify()
{
    bool result = CPDFSDKVerifier.LoadNativeLibrary();
    if (!result)
    {
       return false;
    }
    string Key = "Input your key instead of this string";
    string Secret = "Input your secret  instead of this string";
    CPDFSDKVerifier.LicenseErrorCode verifyResult =
    CPDFSDKVerifier.LicenseVerify(Key, Secret);
    if (verifyResult != CPDFSDKVerifier.LicenseErrorCode.LICENSE_ERR_SUCCESS)
    {
        return false;
    }
    return true;
}

第四步,显示PDF文档

现在,我们已经完成了所有准备工作,接下来我们将显示一份PDF文件。

将下面的代码添加到您的"MainWindow.xaml",“MainWindow.xaml.cs”,从而显示PDF文件。请注意,确保将“ComPDFKit_Demo”替换为您的项目名称。

您的"MainWindow.xaml"代码应该如下所示(在此,我将显示PDF文件的Grid命名为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"
        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>

您的“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);
                }
            }
        }
    }
}

现在运行程序并单击“Open File”按钮,选择您需要显示的PDF文件,您将看到文件被显示在MainWindow上了。PDF查看器已经创建完成。
在这里插入图片描述

故障排除

  1. 如果在LicenseVerify()函数中出现System.IO.FileNotFoundException,如下图:
    在这里插入图片描述

检查您的 WPF 项目并确保在创建项目时选择WPF APP(.NET Framework)而不是WPF Application。
在这里插入图片描述

  1. 其他问题
    如果您在集成我们的 ComPDFKit PDF SDK for Windows 时遇到其他问题,请随时联系ComPDFKit 团队

猜你喜欢

转载自blog.csdn.net/PDFReaderPro/article/details/131959476
今日推荐