负载、编辑和导出PDF表单字段兼容的PDF文件,HTML5的JavaScript

本教程展示了如何装载、编辑和保存PDF文件,PDF表单字段兼容使用HTML5 / JS文件查看器。

概述
总结 本教程涵盖了加载、编辑和导出PDF表单字段在一个JavaScript应用程序兼容的PDF文件。
完成时间 20分钟
Visual Studio项目 下载教程项目(4 KB)
平台 JS的Web应用程序
IDE Visual Studio代码——客户端
开发许可 下载LEADTOOLS

所需的知识

熟悉创建一个项目的基本步骤和文件加载到文档查看器通过审查添加引用和设置许可证——HTML5 JavaScript在文档中显示文件查看器,HTML5 JavaScript教程,在工作之前负载、编辑和导出PDF表单字段兼容的PDF文件,HTML5的JavaScript教程。

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

创建项目并添加LEADTOOLS引用

从项目中创建的副本在文档中显示文件查看器,HTML5 JavaScript教程。如果项目是不可用,按照这个教程中的步骤创建它。

的引用需要取决于项目的目的。可以通过添加引用. 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应用程序所需文件,请参考文件包含在您的应用程序

设置许可文件

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

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

  • 评估许可,评估工具包时下载获得。它允许评估工具包。
  • 部署许可证。如果部署许可证文件和开发人员的关键是需要的,请参考获得许可证

编辑HTML

与项目创建、依赖关系添加许可证,并加载文件代码补充说,编码就可以开始了。

打开index . html文件位于项目文件夹中,并添加以下行.toolbardiv:

<按钮类= "按钮" id =“机”>加载文件</按钮><按钮类= "按钮" id =“getData”>在控制台显示字段数据</按钮><按钮类= "按钮" id =“出口”>导出PDF</按钮>

添加这些按钮将允许用户负载PDF文件,显示表单字段值,和导出到PDF文件所做的更改。

最后,添加这条线以上的结局< / div >标记线为了“房子”我们的PDF文件,当出口:

<iframe id =“父”></ iframe>

应该类似于下面的部分:

<身体><div类= "容器"><div类= "工具栏"><按钮类= "按钮" id =“机”>加载文件</按钮><按钮类= "按钮" id =“getData”>在控制台显示字段数据</按钮><按钮类= "按钮" id =“出口”>导出PDF</按钮></ div><div类= " docContainer "><div id = "是"></ div></ div><iframe id =“父”></ iframe></ div></身体>

将CSS样式添加到CSS文件

打开styles.css文件,并添加以下CSS样式。

/ *删除默认样式。集身体flex作为列;* /身体{保证金:0;显示:flex;flex-direction:列;}.container {保证金:10 px;宽度:钙(100% - 20 px);高度:calc vh - 20 px (100);}.toolbar {高度:5%;宽度:100%;边界底部:2 px固体# 333;显示:块;text-align:中心;}.button {padding-left: 2%;padding-right: 2%;宽度:20%;margin-right: 2%;text-align:中心;高度:40像素;}.docContainer {高度:95%;宽度:100%;显示:flex;flex-direction:行;}#{主题是高度:95%;宽度:100%;显示:flex;flex-direction:行;}#{母公司位置:绝对的;左:-10000 px;宽度:1 px;身高:1 px;溢出:隐藏;}/ *风格元素模式。* /.lt-item,.lt-image-border {/ *盒阴影(视图、项目、图像边界)* /不必:# 333 2 px 2 px 5 px 1 px;}.lt-view,.lt-thumb-item {/ * * /保证金:5 px;填充:5 px;}.lt-item {* / / *项边界:2 px固体# 6 ecaab;background - color: # b2decf;填充:10 px;}.lt-image-border {/ * * /图像边界边界:3 px固体# 444;背景颜色:白色;}.lt-thumb-item {/ * * /缩略图项目边界:2 px固体# 6 ecaab;background - color: # b2decf;}.lt-thumb-item。lt-thumb-item-selected {/ * * /缩略图选择项边界:2 px固体# 59 b2ba;background - color: # 8 ce1e1;}.lt-thumb-item-hovered {* / / *徘徊缩略图项目边界:2 px固体# 52 b996;background - color: # 87 c7b1;}.small-modal {max-width: 200 px;宽度:200 px;}

JS文件添加PDF表单字段代码

打开app.js文件,位于项目文件夹。

一定要第一行的顶部JS文件,这样主题是对象可以访问:

我们是=;

将下面的代码添加到初始化主题是文档服务对象,建立连接,并使本教程的按钮。这将加载一个不相容的PDF文件。

窗口。onload =异步()= > {/ /设置LEADTOOLS评估许可证常量licenseUrl =”。/ Leadtools / LEADTOOLS.lic.txt”;常量developerKey =“添加LEADTOOLS.lic.key的内容。txt文件”;lt.RasterSupport。setLicenseUri (licenseUrl developerKey,);/ /为主题是创建对象的选项常量createOptions =lt.Document.Viewer.DocumentViewerCreateOptions ();createOptions.viewCreateOptions。useElements =真正的;/ /设置viewContainer #是createOptions。viewContainer = . getelementbyid (“是”);/ /创建文档查看器是= lt.Document.Viewer.DocumentViewerFactory.createDocumentViewer (createOptions);/ /设置交互模式平移/缩放/ / documentViewer.commands.run (lt.Document.Viewer.DocumentViewerCommands.interactivePanZoom);/ /我们喜欢在图像查看SVG查看这个例子documentViewer.view。preferredItemType = lt.Document.Viewer.DocumentViewerItemType.svg;/ /使用图片:lt.Document.Viewer.DocumentViewerItemType.image;/ / documentViewer.view.imageViewer。autoCreateCanvas = true;documentViewer.annotations.automationManager。renderingEngine =lt.Annotations.Rendering.AnnHtml5RenderingEngine ();/ /初始化是注释documentViewer.annotations.initialize ();/ /调用服务之前,我们需要解释的地方(“localhost: 40000 / api”,例如:lt.Document.DocumentFactory。serviceHost =“http://localhost: 40000”;/ /或者您的主机lt.Document.DocumentFactory。servicePath =”“;/ /服务的根的路径,这对于这个示例lt.Document.DocumentFactory。serviceApiPath =“api”;/ /路由发生在“/ api”,除非你改变DocumentService路由/ /初始化两个按钮的onclick事件. getelementbyid (“机”)。onclick = selectAndLoadFile;. getelementbyid (“getData”)。onclick = getData;. getelementbyid (“出口”)。onclick = exportPDF;/ /加载一个PDF文档常量url =“https://demo.leadtools.com/images/pdf/leadtools.pdf”;常量loadDocumentOptions =lt.Document.LoadDocumentOptions ();loadDocumentOptions。loadFormFieldsMode = 1;/ /调用“LoadFromUri”和通过成功和失败回调的承诺试一试{=等待lt.Document.DocumentFactory让文档。loadDocumentOptions loadFromUri (url)documentViewer.setDocument(文档);如果(document.formFields.hasFormFields) {console.log (“这个文档的PDF表单字段。点击“显示字段的数据在控制台”看到更多信息”)}其他的{console.log (“这个文档没有PDF表单字段”)}}(错误){console.error(错误);/ /从LEADTOOLS获得更多信息让serviceError = lt.Document.ServiceError。parseError (jqXHR statusText errorThrown);lt.LTHelper.log (serviceError);/ /显示一个警告的问题是什么让线= [];lines.push (文档查看器错误:“);lines.push (serviceError.message);lines.push (serviceError.detail);lines.push (有关详细信息,请参阅控制台”。)警报(lines.join (“\ n”));}}

接下来添加两个新函数命名selectAndLoadFileexportPDF用来加载指定的PDF文件和出口的时候完成。添加下面的代码:

函数selectAndLoadFile () {/ /创建一个输入元素导入文档按钮来上传文件/ /文档编辑器常量输入= document.createElement (“输入”);输入。类型=“文件”;input.style。显示=“没有”;输入。接受=”。医生,docx . pdf, . rtf, . txt。;输入。onchange =异步函数(e) {让文件= input.files;如果(!文件| | ! files.length)返回;让文件[0]=文件;/ /设置光标懒懒的动画document.body.style。光标=“等待”;/ /创建文档加载选项常量loadDocumentOptions =lt.Document.LoadDocumentOptions ();loadDocumentOptions。loadFormFieldsMode = 1;试一试{/ /从文件加载文档使用的选项让医生=等待lt.Document.DocumentFactory。loadDocumentOptions loadFromFile(文件)/ /设置新文档在文档查看器documentViewer.setDocument (doc);/ /检查文件表单字段如果(doc.formFields.hasFormFields) {console.log (“这个文档的PDF表单字段。点击“显示字段的数据在控制台”看到更多信息”)}其他的{console.log (“这个文档没有PDF表单字段”)}}(错误){console.error(错误);}/ /光标返回到默认值document.body.style。光标=“默认”;};input.click ();}异步函数exportPDF () {试一试{/ /获取文档的文档查看器让医生=等待documentViewer.document;/ /准备文档保存documentViewer.prepareToSave ();/ /保存文件缓存lt.Document.DocumentFactory.saveToCache (doc)/ /一旦文档保存到缓存中,创建打印选项常量选择=lt.Document.Viewer.PrintDocumentOptions ();/ /打印选项被设置选项。showAnnotations =真正的;选项。usePdfPrinting =真正的;/ /获取iframe HTMLElement“房子”打印的pdf选项。父母= . getelementbyid (“父”);/ /保存/打印并打开浏览器打印机打印documentViewer.print(选项);}(错误){console.error(错误);}}

最后,添加另一个函数的命名getData这将显示所有表单字段数据到控制台(这可以在你的浏览器DevTools)。

函数getData () {让医生= documentViewer.document;documentViewer.prepareToSave ();如果(doc.formFields.hasFormFields) {让容器= doc.formFields.getFormFields ();(让我= 0;我< containers.length;我+ +){(让j = 0;j <容器[我].children.count;j + +) {console.log (`-------------------------- Page ${i + 1} - Field ${j + 1} ---------------------------------`)console.log (`ID: ${containers[i].children.item(j).id}`);console.log (`Name: ${containers[i].children.item(j).name}`);console.log (`Bounds: ${containers[i].children.item(j).bounds}`);console.log (`Background color: ${containers[i].children.item(j).backgroundColor}`);console.log (`Printable: ${containers[i].children.item(j).printable}`);console.log (`Viewable: ${containers[i].children.item(j).viewable}`);console.log (`Locked: ${containers[i].children.item(j).locked}`);console.log (`Required: ${containers[i].children.item(j).required}`);console.log (`Read Only: ${containers[i].children.item(j).readOnly}`);console.log (`Border Style: ${containers[i].children.item(j).borderStyle.style}`);console.log (`Border Color: ${containers[i].children.item(j).borderStyle.color}`);console.log (`Border Width: ${containers[i].children.item(j).borderStyle.width}`);console.log (`Font Name: ${containers[i].children.item(j).textStyle.fontName}`);console.log (`Font Size: ${containers[i].children.item(j).textStyle.fontSize}`);console.log (`Font Color: ${containers[i].children.item(j).textStyle.color}`);console.log (`Font Alignment: ${containers[i].children.item(j).textStyle.textAlignment}`);console.log (`Type: ${containers[i].children.item(j).type}`);如果(容器[我].children.item (j)。类型= =“DocumentTextFormField”){console.log (`Value: ${containers[i].children.item(j).value}`)console.log (`Content Type: ${containers[i].children.item(j).contentType}`);console.log (`Max Length: ${containers[i].children.item(j).maxLength}`)console.log (`Multi-Line: ${containers[i].children.item(j).multiline}`)console.log (`Password: ${containers[i].children.item(j).isPassword}`)console.log (`Comb: ${containers[i].children.item(j).isComb}`)}如果(容器[我].children.item (j)。类型= =“DocumentChoiceFormField”){console.log (`Options Display Value: ${containers[i].children.item(j).optionsDisplayValue}`)console.log (`Options Exported Value: ${containers[i].children.item(j).optionsExportedValue}`);console.log (`Selected Indices: ${containers[i].children.item(j).selectedIndices}`)console.log (`Multi-Select: ${containers[i].children.item(j).multiSelect}`)console.log (`Choice Type: ${containers[i].children.item(j).choiceType}`)}如果(容器[我].children.item (j)。类型= =“DocumentButtonFormField”){console.log (`Checked: ${containers[i].children.item(j).isChecked}`)如果(容器[我].children.item (j)。buttonType = = 0) {console.log (`Button Type: Check Box`);}其他的{console.log (`Button Type: Radio Button`);}}}}}其他的{console.log (“这个文档没有PDF表单字段”)}}

运行该项目

前端HTML5 / JS文件查看器运行之前,您必须运行文档服务。LEADTOOLS SDK安装提供了以下三个例子文档的服务平台:

  • net框架
  • net核心
  • Java

说明如何设置和配置文档服务,在前面列出的三个平台中的步骤开始使用文档查看器演示,HTML5的JavaScript教程。

对于本教程,. net Framework文档服务使用,它可以在这里找到:< INSTALL_DIR > \ LEADTOOLS22 \ \例子文档\ JS \ DocumentServiceDotNet \外汇

一旦你有了后端文档服务运行,打开命令行和cd到项目文件夹。使用以下命令运行一个静态文件服务器:http服务器

服务器应该启动和运行http://localhost: 8080。消息应该出现在控制台显示所有服务器上可用的端口。

Http服务器运行的截图。

测试,遵循以下步骤:

  • 打开一个浏览器并输入URL。一旦你输入URL, HTML5 / JS文件查看器会出现与样本PDF预装。
  • 选择加载文件和打开一个PDF表单字段兼容的PDF文件。
  • 选择在控制台显示字段数据在浏览器控制台查看表单字段值。
  • 编辑PDF文档查看器中你想,然后重复上面的步骤比较值。
  • 选择导出PDF保存编辑PDF文件。

总结

本教程展示了如何装载、编辑和保存一个PDF表单字段兼容的文件在一个HTML5 / JS文件查看器应用程序。

另请参阅

iOS