在当今世界,Windows 应用程序对我们的工作至关重要。随着处理 PDF 文档的需求不断增加,将 ComPDFKit PDF 查看和编辑功能集成到您的 Windows 应用程序或系统中,可以极大地为您的用户带来美妙的体验。
在本博客中,我们将首先探索集成 ComPDFKit PDF SDK 的必要步骤,并使用 ComPDFKit 构建 Windows PDF 阅读器。
ComPDFKit 是一个功能强大的 PDF SDK。只需数行C#代码即可轻松将 ComPDFKit PDF SDK 嵌入到您的 Windows 应用程序中。让我们用几分钟时间开始使用。
以下部分介绍了配置要求、安装包的结构以及如何通过C#语言,使用 ComPDFKit PDF SDK制作 Windows PDF 阅读器。
您可以联系我们获取我们的PDF SDK安装包。
SDK包中包含以下文件:
您可以联系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文件。
将下面的代码添加到您的"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查看器已经创建完成。
LicenseVerify()
函数中出现System.IO.FileNotFoundException
,如下图:检查您的 WPF 项目并确保在创建项目时选择WPF APP(.NET Framework)而不是WPF Application。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。