在文档中显示文件查看器——WinForms c#

本教程展示了如何创建一个c# Windows WinForms应用程序来使用LEADTOOLS SDK文档加载到WinForms文档查看器。

概述
总结 本教程介绍了如何使用LEADTOOLS文档查看器c# Windows SDK技术WinForms应用程序。
完成时间 30分钟
Visual Studio项目 下载教程项目(8 KB)
平台 c# Windows WinForms应用程序
IDE Visual Studio 2017中,2019年
开发许可 下载LEADTOOLS
试试用另一种语言

所需的知识

熟悉基本的步骤创建一个项目的审查添加引用和设置一个许可证教程,在工作之前在文档中显示文件查看器——WinForms c#教程。

创建项目并添加LEADTOOLS引用

在Visual Studio中,创建一个新的Windows WinForms c#项目,并添加以下必要LEADTOOLS引用。

的引用需要取决于项目的目的。引用可以通过添加一个或另一个下面的两种方法(但不是全部)。对于这个项目,还需要以下引用:

如果使用NuGet引用,本教程需要以下NuGet包:

如果使用本地DLL的引用,下面的DLL是必要的。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64:

对于一个完整的列表的编解码器dll需要特定的格式,请参考文件格式的支持

设置许可文件

许可解锁项目需要的特性。它必须设置工具箱函数被调用之前。详情,包括教程为不同的平台,请参考设置一个运行时许可

有两种类型的运行时许可证:

请注意

添加LEADTOOLS NuGet和本地引用设置许可中会详细介绍添加引用和设置一个许可证教程。

初始化文档查看器

与项目创建、添加引用,许可,可以开始编码。

解决方案资源管理器,打开Form1.cs。右键单击设计窗口并选择视图代码或按F7以弹出形式背后的代码。添加以下语句的使用块顶部:

c#
/ /使用块使用系统;使用先;使用System.Drawing;使用System.Windows.Forms;使用Leadtools;使用Leadtools.Document;使用Leadtools.Caching;使用Leadtools.Document.Viewer;使用Leadtools.Controls;

添加一个新方法InitDocumentViewer ()和内部调用这个方法Form1主要方法后SetLicense ()调用。添加以下代码来初始化文档查看器。

c#
/ /添加这些全球成员私人LEADDocument virtualDocument;私人FileCache缓存;私人主题是主题是;
c#
公共Form1 (){InitializeComponent ();SetLicense ();InitDocumentViewer ();}
c#
私人无效InitDocumentViewer (){InitUI ();varcreateOptions =DocumentViewerCreateOptions ();/ /设置显示文档查看器的UI部分createOptions。ViewContainer =.Controls.Find (“docViewerPanel”,)[0];/ /设置显示缩略图的UI部分createOptions。ThumbnailsContainer =.Controls.Find (“thumbPanel”,)[0];/ /不使用注释createOptions。UseAnnotations =;/ /创建查看器是= DocumentViewerFactory.CreateDocumentViewer (createOptions);documentViewer.View.ImageViewer.Zoom (ControlSizeMode。FitAlways, 1.0, documentViewer.View.ImageViewer.DefaultZoomOrigin);缓存=FileCache{CacheDirectory = Path.GetFullPath (@”。\ CacheDir”),};virtualDocument = DocumentFactory.Create (CreateDocumentOptions(){缓存=缓存,UseCache =真正的});}

初始化用户界面

添加一个新方法InitUI ()。该方法将被称为初的InitDocumentViewer ()方法。添加以下代码来初始化文档查看器面板和程序控制。

c#
私人无效InitUI (){/ /为文档查看器添加一个面板左侧缩略图varthumbPanel =面板();thumbPanel。Name =“thumbPanel”;thumbPanel。宽度= 200;thumbPanel。码头= DockStyle.Left;thumbPanel。背景色= Color.Gray;thumbPanel。边框样式= BorderStyle.FixedSingle;.Controls.Add (thumbPanel);/ /添加一个小组来填补,为文档查看器vardocViewerPanel =面板();docViewerPanel。Name =“docViewerPanel”;docViewerPanel。背景色= Color.DarkGray;docViewerPanel。边框样式= BorderStyle.None;docViewerPanel。码头= DockStyle.Fill;.Controls.Add (docViewerPanel);/ /添加一个顶面板举办我们的应用程序控制vartopPanel =面板();topPanel。Name =“topPanel”;topPanel。身高= 30;topPanel。码头= DockStyle.Top;topPanel。边框样式= BorderStyle.FixedSingle;.Controls.Add (topPanel);docViewerPanel.BringToFront ();varloadButton =按钮();loadButton。Name =“loadButton”;loadButton。文本=“装”;loadButton。单击+ =(发送方,e) = > LoadDocument (loadButton);topPanel.Controls.Add (loadButton);}

添加负载并设置文档的代码

添加一个新方法Form1loadButton LoadDocument(按钮)。这个方法将被称为loadButton。单击+ =(发送方,e) = > LoadDocument (loadButton);行代码内部InitUi ()方法。添加以下代码内部loadButton LoadDocument(按钮)方法加载指定的文档和设置内的文档查看器。

c#
私人无效loadButton LoadDocument(按钮){OpenFileDialog海底钻井=OpenFileDialog ();海底钻井。过滤器=“所有文件| * *”。;如果(ofd.ShowDialog () = = DialogResult.OK){LEADDocument LEADDocument = DocumentFactory.LoadFromFile (ofd.FileName,LoadDocumentOptions {UseCache =真正的缓存,缓存=,LoadEmbeddedAnnotations =真正的});virtualDocument.Pages.Clear ();(int我= 0;我< 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类。

另请参阅

iOS
188金宝搏的网址客服|支持|联系我们|知识产权的通知
©1991 - 2023领先的技术公司。保留所有权利。