在文档查看器中显示文件- HTML5 JavaScript

本教程展示了如何创建一个HTML5 JavaScript应用程序,该应用程序使用LEADTOOLS SDK在HTML5 JavaScript文档查看器中加载和注释文档。

概述
总结 本教程将介绍如何在HTML5 JavaScript应用程序中使用LEADTOOLS文档查看器SDK技术。
完成时间 30分钟
Visual Studio项目 下载教程项目(3kb)
平台 JS的Web应用程序
IDE Visual Studio代码-客户端
开发许可 下载LEADTOOLS
用另一种语言试试

所需的知识

来熟悉创建项目的基本步骤添加引用和设置许可证- HTML5 JavaScript教程,在工作之前在文档查看器中显示文件- HTML5 JavaScript教程。

本教程将使用http服务器,用于运行静态文件服务器的控制台命令。安装http服务器第一次安装node . js然后安装http服务器

创建项目并添加LEADTOOLS引用

中创建的项目的副本开始添加引用和设置许可证- HTML5 JavaScript教程。如果该项目不可用,请按照该教程中的步骤创建它。

在项目文件夹中创建一个新文件,style.css,它将处理主体布局。

所需的参考资料取决于项目的目的。可以通过添加引用. js文件位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ JS

对于这个项目,需要以下参考资料:

  • Leadtools.js
  • Leadtools.Annotations.Automation.js
  • Leadtools.Annotations.Designer.js
  • Leadtools.Annotations.Engine.js
  • Leadtools.Annotations.Rendering.JavaScript.js
  • Leadtools.Controls.js
  • Leadtools.Document.js
  • Leadtools.Document.Viewer.js
  • Leadtools.ImageProcessing.Main.js
  • Leadtools.ImageProcessing.Color.js
  • Leadtools.ImageProcessing.Core.js
  • Leadtools.ImageProcessing.Effects.js

确保将这些文件复制到项目的文件中自由文件夹中。

有关应用程序需要哪些JS文件的完整列表,请参阅与您的应用程序包含的文件

设置License文件

许可证解锁项目所需的特性。它必须在调用任何工具包函数之前设置。有关详细信息,包括不同平台的教程,请参阅设置运行时License

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

  • 评估license,下载评估工具包时获取。它允许对工具包进行评估。
  • 部署许可证。如果需要部署许可文件和开发人员密钥,请参见获得许可证

请注意

中更详细地介绍了添加LEADTOOLS引用和设置许可证添加引用和设置许可证- HTML5 JavaScript教程。

编写HTML文件

创建了项目、添加了依赖项并设置了许可证之后,就可以开始编码了。

打开index . html位于项目文件夹中的文件。中添加以下行< >头标签来导入JS文件并将依赖项附加到index . html页面。然后添加以下内容<选择>元素到体的index . html页,该页将用于选择当前用户模式和各种类型的注释对象。

<><元charset =“utf - 8”/><元名称="viewport" content="width=device-width, initial-scale=1.0" /><脚本src = " https://code.jquery.com/jquery-2.2.4.min.js "诚信= " sha256-BbhdlvQf / xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44 = " crossorigin =“匿名”></脚本><脚本src = " / lib / Leadtools.js”。></脚本><脚本src = " / lib / Leadtools.Controls.js”。></脚本><脚本src = " / lib / Leadtools.Annotations.Engine.js”。></脚本><脚本src = " / lib / Leadtools.Annotations.Designers.js”。></脚本><脚本src = " / lib / Leadtools.Annotations.Rendering.Javascript.js”。></脚本><脚本src = " / lib / Leadtools.Annotations.Automation.js”。></脚本><脚本src = " / lib / Leadtools.ImageProcessing.Main.js”。></脚本><脚本src = " / lib / Leadtools.ImageProcessing.Color.js”。></脚本><脚本src = " / lib / Leadtools.ImageProcessing.Core.js”。></脚本><脚本src = " / lib / Leadtools.ImageProcessing.Effects.js”。></脚本><脚本src = " / lib / Leadtools.Document.js”。></脚本><脚本src = " / lib / Leadtools.Document.Viewer.js”。></脚本><链接rel="样式表" type="text/css" href="style.css"><JS文件将加载HTML5/JS文档查看器到显示——><脚本src = " app.js " type = " text / javascript”></脚本></头><身体><div类= "容器"><div类= "工具栏"><div class = " vcenter push-right”><标签= " interactiveSelect ">交互模式:</标签><选择id = " interactiveSelect "></选择></ div><div class = " vcenter push-right”><标签= " annotationsSelect ">注释对象:</标签><选择id = " annotationsSelect "></选择></ div><Div id="output" class="vcenter push-right"></ div><div id="serviceStatus" class="vcenter push-right"></ div></ div><div类= " docContainer "><div类= " sidepanel " id =“缩略图”></ div><div类= " centerpanel " id =“查看器”></ div><div类= " sidepanel " id =“书签”></ div></ div></ div></身体>

编写CSS文件

打开style.css位于项目文件夹中的文件。删除默认身体样式,并将下面的代码行添加到身体

/*删除默认的主体样式。设置身体弯曲为一个柱;* /身体{保证金:0;显示:flex;flex-direction:列;.container {保证金:10 px;宽度:calc(100% - 20px);高度:calc(100vh - 20px);.toolbar {高度:5%;宽度:100%;Border-bottom: 2px solid #333;flex-direction:行;显示:flex;#{书签溢出:隐藏;.vcenter {margin-top:汽车;margin-bottom:汽车;.hcenter {margin-left:汽车;margin-right:汽车;.push-right {margin-left: 10 px;.docContainer {高度:95%;宽度:100%;显示:flex;flex-direction:行;.sidepanel {宽度:15%;高度:100%;.centerpanel {宽度:100%;高度:100%;/*元素模式的样式。* /.lt-item .lt-image-border {/*框阴影(视图,项目,图像边框)*/Box-shadow: #333 2px 2px 5px 1px;.lt-view。lt-thumb-item {/ * * /保证金:5 px;填充:5 px;.lt-item {* / / *项边框:2px solid #6ecaab;background - color: # b2decf;填充:10 px;.lt-image-border {/*图像边框*/边框:3px solid #444;背景颜色:白色;.lt-thumb-item {/*缩略图项*/边框:2px solid #6ecaab;background - color: # b2decf;.lt-thumb-item。lt-thumb-item-selected {/*选择缩略图项*/边框:2px solid #59b2ba;background - color: # 8 ce1e1;.lt-thumb-item-hovered {/*悬停缩略图项*/边框:2px solid #52b996;background - color: # 87 c7b1;.small-modal {max-width: 200 px;宽度:200 px;

编写JS文件

打开app.js位于项目文件夹中的文件。添加下面的代码以创建一个主题是对象,添加自动注释支持,并加载默认文档。

窗口。onload =函数() {varlicenseUrl =”。/ Leadtools / LEADTOOLS.lic.txt”vardeveloperKey ="添加LEADTOOLS.lic.key.txt文件的内容"lt.RasterSupport。setLicenseUri (licenseUrl developerKey,函数setLicenseResult) {//查看license的状态如果(setLicenseResult.result) {console.log ("LEADTOOLS客户端许可证设置成功");其他的var味精="LEADTOOLS许可证丢失,无效或过期\nError:\n"console.log(味精);警报(味精);});ViewerInitializer {构造函数(回调){.callback =回调;.init ();静态showServiceError = (jqXHR, statusText, errorthrow) => {alert ("从服务返回错误。详情请参见控制台。”常量serviceError = lt.Document.ServiceError。parseError (jqXHR statusText errorThrown);console.error (serviceError);Init = () => {.initFactory ();.testConnection ();initFactory = () => {//要与DocumentService通信,它必须正在运行!//修改这些参数以匹配服务的路径。lt.Document.DocumentFactory.serviceHost =“http://localhost: 40000”lt.Document.DocumentFactory.servicePath =""lt.Document.DocumentFactory.serviceApiPath =“api”testConnection = () => {常量serviceStatus = . getelementbyid (“serviceStatus”);serviceStatus。innerHTML =“连接到服务:”+ lt.Document.DocumentFactory.serviceUri;lt.Document.DocumentFactory.verifyService ().done ((serviceData) = > {serviceStatus。innerHTML =“服务连接验证!”.createDocumentViewer ();}).fail((jqXHR, statusText, errorthrow) => {serviceStatus。innerHTML =“服务连接不可用。”ViewerInitializer。showServiceError (jqXHR statusText errorThrown);});createDocumentViewer = () => {//初始化用户界面常量interactiveSelect = . getelementbyid (“interactiveSelect”);//在interactiveSelect下拉菜单中添加交互式命令常量panZoomOption = document.createElement (“选项”);panZoomOption。innerHTML =“锅/缩放”panZoomOption。值= lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom;interactiveSelect.appendChild (panZoomOption);常量textOption = document.createElement (“选项”);textOption。值= lt.Document.Viewer.DocumentViewerCommands.interactiveSelectText;textOption。innerHTML =“选择文本”interactiveSelect.appendChild (textOption);我们是=interactiveSelect。onchange = (e) =>);常量annotationsSelect = . getelementbyid (“annotationsSelect”);//在annotationsSelect下拉菜单中添加注释常量annSelectOption = document.createElement (“选项”);annSelectOption。innerHTML =“选择注释”annSelectOption。值= lt.Annotations.Engine.AnnObject.selectObjectId.toString ();annotationsSelect.appendChild (annSelectOption);常量annLineOption = document.createElement (“选项”);annLineOption。innerHTML =“对象”annLineOption。值= lt.Annotations.Engine.AnnObject.lineObjectId.toString ();annotationsSelect.appendChild (annLineOption);常量annRectOption = document.createElement (“选项”);annRectOption。innerHTML =“矩形对象”annRectOption。值= lt.Annotations.Engine.AnnObject.rectangleObjectId.toString ();annotationsSelect.appendChild (annRectOption);annotationsSelect。Onchange = (e) => {常量值= + e.currentTarget.value;documentViewer.annotations.automationManager.currentObjectId =价值;//初始化文档查看器,传递面板常量createOptions =lt.Document.Viewer.DocumentViewerCreateOptions ();//我们不打算在这个例子中使用元素模式createOptions.viewCreateOptions.useElements =createOptions.thumbnailsCreateOptions.useElements =//视图的中间面板createOptions。viewContainer = . getelementbyid (“观众”);//缩略图的左侧面板createOptions。thumbnailsContainer = . getelementbyid (“缩略图”);//书签的右边面板createOptions。bookmarksContainer = . getelementbyid (“书签”);createOptions。useAnnotations =真正的//创建文档查看器是= lt.Document.Viewer.DocumentViewerFactory.createDocumentViewer (createOptions);//我们更喜欢SVG查看documentViewer.view.preferredItemType = lt.Document.Viewer.DocumentViewerItemType.svg;//创建html5渲染引擎documentViewer.annotations.automationManager.renderingEngine =lt.Annotations.Rendering.AnnHtml5RenderingEngine ();//初始化documentViewer注释documentViewer.annotations.initialize ();documentViewer.annotations.automationManager.currentObjectIdChanged.add (函数(发送者,e) {//绘图完成后,管理器将返回到所选对象;所以我们需要强制annotationsSelect元素返回到选择对象选项annotationsSelect。值= sender.currentObjectId;});interactiveSelect .loadDefaultDoc(是)loadDefaultDoc = async(查看器,interactiveSelect) => {//加载PDF文档试一试常量url =“https://demo.leadtools.com/images/pdf/leadtools.pdf”lt.Document.DocumentFactory.loadFromUri(url,);医生。isReadOnly =viewer.setDocument (doc);常量panZoom = lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom;interactiveSelect。值= panZoom;viewer.commands.run (panZoom如果.callback).callback(观众);如果(医生。isStructureSupported && !doc.structure.isParse) {等待doc.structure.parse ()(错误){console.error(错误)ViewerInitializer ();

运行该项目

运行前端HTML5/JS文档查看器之前,先运行文档服务。LEADTOOLS SDK安装为以下平台提供了三个文档服务示例:

  • net框架
  • net核心
  • Java

有关如何在前面列出的三个平台中设置和配置Document Service的说明,请参阅开始使用文档查看器演示- HTML5 JavaScript教程。

在本教程中,使用了.NET框架文档服务,可以在这里找到它:< INSTALL_DIR > \ LEADTOOLS22 \ \例子文档\ JS \ DocumentServiceDotNet \外汇

运行后端文档服务之后,打开命令行和cd到项目文件夹中。使用以下命令运行静态文件服务器:http服务器

服务器应该启动并运行http://localhost:8080.控制台中应该出现一条消息,指示服务器可用的所有端口。

Http服务器运行的截图。

要进行测试,请从要在文档上绘制和显示的任何不同的文档查看器命令和注释中进行选择。

项目运行的截图,并绘制了注释。

总结

本教程展示了如何在HTML5/JS文档查看器应用程序中初始化、加载和显示文档。它还展示了如何添加自动化注释支持。

另请参阅

net
iOS