前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用C#基于ComPDFKit SDK快速构建PDF阅读器

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

原创
作者头像
ComPDFKit
发布2023-07-27 16:26:25
2750
发布2023-07-27 16:26:25
举报
文章被收录于专栏:PDF 开发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, 单击创建新项目
  1. 选择“WPF APP (.NET Framework)”,然后单击“下一步”。
  1. 配置您的项目:设置您的项目名称并选择存储程序的位置。在本示例中,项目名称称为“ComPDFKit Demo”。此示例项目使用 .NET Framework 4.6.1 作为编程框架。
  1. 点击“创建”按钮,至此项目创建完成。

第二步:添加ComPDFKit PDF SDK包

  1. 打开您的项目解决方案,右击“引用”,在右键菜单项中选择“管理Nuget程序包”,这将打开您的项目的NuGet包管理器。
  1. 点击“浏览”,设置程序包源为nuget.org,搜索ComPDFKit.NetFramework,您将搜索到“ComPDFKit.NetFramework”包。
  1. 选中包后,在右侧包的详情面板中,点击“安装”来下载包。
  1. 安装完成后,您现在可以在“解决方案资源管理器”->“引用”中找到对应的包的引用。

第三步,应用许可证密钥

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

代码语言:txt
复制
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):

代码语言:txt
复制
<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”文件应该如下所示。

请注意:您需要输入许可证密钥,代码中需要修改的部分已使用注释进行了标注。您只需将注释下方的字符串内容自行替换即可。

代码语言:txt
复制
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 团队

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ComPDFKit SDK for Windows 入门
    • 要求
      • Windows包结构
      • 使用C#构建Windows PDF查看器
        • 第一步:创建一个新项目
          • 第二步:添加ComPDFKit PDF SDK包
            • 第三步,应用许可证密钥
              • 第四步,显示PDF文档
              • 故障排除
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档