本教程演示如何创建一个c# Windows WinForms应用程序,该应用程序使用LEADTOOLS SDK将文档加载到WinForms文档查看器中。
| 概述 | |
|---|---|
| 总结 | 本教程介绍如何在c# Windows WinForms应用程序中使用LEADTOOLS文档查看器SDK技术。 |
| 完成时间 | 30分钟 |
| Visual Studio项目 | 下载教程项目(8kb) |
| 平台 | Windows WinForms应用程序 |
| IDE | Visual Studio 2017, 2019 |
| 开发许可 | 下载LEADTOOLS |
| 用另一种语言试试 |
|
步骤熟悉创建项目的基本步骤添加引用和设置License教程,在工作之前在文档查看器中显示文件- WinForms c#教程。
在Visual Studio中,创建一个新的c# Windows WinForms项目,并添加以下必要的LEADTOOLS引用。
所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。本项目需要参考资料如下:
如果使用NuGet引用,本教程需要以下NuGet包:
Leadtools.Document.SdkLeadtools.Document.Viewer.WinForms如果使用本地DLL引用,则需要以下DLL。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64:
Leadtools.dllLeadtools.Caching.dllLeadtools.Codecs.dllLeadtools.Controls.WinForms.dllLeadtools.Document.dllLeadtools.Document.Pdf.dllLeadtools.Document.Viewer.WinForms.dll有关特定格式所需的Codec dll的完整列表,请参阅文件格式支持.
许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时License.
有两种类型的运行时许可证:
请注意
中详细介绍了添加LEADTOOLS NuGet和本地引用以及设置许可添加引用和设置License教程。
随着项目的创建、引用的添加和许可的设置,编码就可以开始了。
在解决方案资源管理器,打开Form1.cs.右键单击设计窗口并选择视图代码或按F7来显示表单后面的代码。将下列语句添加到使用顶部的块:
//在顶部使用块使用系统;使用先;使用System.Drawing;使用System.Windows.Forms;使用Leadtools;使用Leadtools.Document;使用Leadtools.Caching;使用Leadtools.Document.Viewer;使用Leadtools.Controls;
添加一个名为InitDocumentViewer ()方法中调用此方法Form1主要方法后SetLicense ()调用。添加下面的代码来初始化Document Viewer。
//添加全局成员私人LEADDocument virtualDocument;私人FileCache缓存;私人主题是主题是;
公共Form1 (){InitializeComponent ();SetLicense ();InitDocumentViewer ();}
私人无效InitDocumentViewer (){InitUI ();varcreateOptions =新DocumentViewerCreateOptions ();//设置文档查看器显示的UI部分createOptions。ViewContainer =这.Controls.Find (“docViewerPanel”,假)[0];//设置显示缩略图的UI部分createOptions。ThumbnailsContainer =这.Controls.Find (“thumbPanel”,假)[0];//暂不使用注释createOptions。UseAnnotations =假;//现在创建查看器documentViewer = DocumentViewerFactory.CreateDocumentViewer(createOptions);documentViewer.View.ImageViewer.Zoom (ControlSizeMode。FitAlways, 1.0, documentViewer.View.ImageViewer.DefaultZoomOrigin);缓存=新FileCache{CacheDirectory =路径。GetFullPath (@”。\ CacheDir”),};virtualDocument = DocumentFactory。创建(新CreateDocumentOptions() {Cache = Cache, UseCache =真正的});}
添加一个名为InitUI ().方法的开头将调用此方法InitDocumentViewer ()方法。添加下面的代码来初始化Document Viewer面板和应用程序控件。
私人无效InitUI (){//在左侧添加一个用于文档查看器缩略图的面板varthumbPanel =新面板();thumbPanel。Name =“thumbPanel”;thumbPanel。宽度= 200;thumbPanel。Dock = DockStyle.Left;thumbPanel。BackColor = Color.Gray;thumbPanel。BorderStyle = BorderStyle. fixedsingle;这.Controls.Add (thumbPanel);//添加一个面板来填充其余部分,用于文档查看器vardocViewerPanel =新面板();docViewerPanel。Name =“docViewerPanel”;docViewerPanel。BackColor = color .深灰色;docViewerPanel。BorderStyle = BorderStyle. none;docViewerPanel。Dock = DockStyle.Fill;这.Controls.Add (docViewerPanel);//添加一个顶部面板来承载我们的应用程序控件vartopPanel =新面板();topPanel。Name =“topPanel”;topPanel。身高= 30;topPanel。Dock = DockStyle.Top;topPanel。BorderStyle = BorderStyle. fixedsingle;这.Controls.Add (topPanel);docViewerPanel.BringToFront ();varloadButton =新按钮();loadButton。Name =“loadButton”;loadButton。文本=“装”;loadButton。点击+= (sender, e) => LoadDocument(loadButton);topPanel.Controls.Add (loadButton);}
控件中添加一个新方法Form1类loadButton LoadDocument(按钮).方法中调用此方法loadButton。点击+= (sender, e) => LoadDocument(loadButton);中的代码行InitUi ()方法。将下面的代码添加到loadButton LoadDocument(按钮)方法加载指定的文档并在查看器中设置文档。
私人无效loadButton LoadDocument(按钮){OpenFileDialog ofd =新OpenFileDialog ();海底钻井。过滤器=“所有文件| * *”。;如果(ofd.ShowDialog() == dialgresult . ok){LEADDocument = DocumentFactory.LoadFromFile(ofd.FileName,新LoadDocumentOptions {UseCache =真正的, Cache = Cache, lodembeddenotations =真正的});virtualDocument.Pages.Clear ();为(intI = 0;i < leadDocument.Pages.Count;我+ +){virtualDocument.Pages.Add (leadDocument.Pages[我]);}}documentViewer.BeginUpdate ();documentViewer.SetDocument (virtualDocument);documentViewer.View.Invalidate ();如果(是。缩略图! =零)documentViewer.Thumbnails.Invalidate ();documentViewer.EndUpdate ();}
按下运行项目F5,或选择调试->开始调试.
如果正确地执行了这些步骤,应用程序将运行。要进行测试,请单击负载按钮,调出OpenFileDialog.选择一个要加载的文档,该文档应该出现在查看器中,如下所示:
本教程展示了如何初始化WinForms文档查看器、加载文档以及将文档设置到查看器中。还介绍了如何使用主题是而且LEADDocument类。