企业必须经常将其硬拷贝文档和扫描文件数字化。 考虑一下
场景
. 一家律师事务所有数千份法律合同,他们扫描这些合同是为了创建数字文件。 他们想确定这些法律合同中是否有任何合同有特定的条款或补充条款,是否必须加以修订。 出于合规性目的,准确性是必需的。 解决方案是清点数字文档,使文本可搜索,并创建索引以查找此信息。
对于大多数公司来说,创建数字归档文件以检索信息以进行编辑或下游操作是一个噩梦。
您可以学到的内容
此实际操作教程将探讨如何 Adobe Acrobat Services API的功能,可以轻松用于存档和数字化文档。 您可以通过构建Express NodeJS应用程序,然后集成 Acrobat Services 用于归档、数字化和文档转换的API。
若要关注,您需要
Node.js
已安装并基本了解Node.js和
ES6语法
.
相关的API和资源
"repository": {
"type": "git",
"url": "git+https://github.com/agavitalis/AdobeDocumentServicesAPIs.git"
"author": "Ogbonna Vitalis",
"license": "ISC",
"bugs": {
"url": "https://github.com/agavitalis/AdobeDocumentServicesAPIs/issues"
"homepage": "https://github.com/agavitalis/AdobeDocumentServicesAPIs#readme"
###bash
npm install express mongoose config body-parser morgan multer hbs path pdf-parse
Ensure that the content of your package.json file is similar to this code snippet:
###package.json
完成注册后,代码示例将下载到包含您的API凭据的电脑。 提取此代码示例,并将private.key和pdftools-api-credentials.json文件放在应用程序的根目录中。
立即安装 PDF服务Node.js SDK 通过运行 npm install --save @adobe/documentservices-pdftools-node-sdk
命令使用应用程序根目录中的终端。
创建PDF
Acrobat Services 支持从Microsoft OfficePDF(Word、Excel和PowerPoint)及其他文档创建文档 支持的文件格式 例如,.txt、.rtf、.bmp、.jpg、.gif、.tiff和.png。
要从支持的文件格式创建PDF文档,请使用此表单上传文档。 您可以在上访问表单的HTML和CSS文件 GitHub.
const PDFToolsSdk = require('@adobe/documentservices-pdftools-node-sdk');
* GET / route to show the createPDF form.
function createPDF(req, res) {
//catch any response on the url
let response = req.query.response
res.render('index', { response })
* POST /createPDF to create a new PDF File.
function createPDFPost(req, res) {
let filePath = req.file.path;
let fileName = req.file.filename;
try {
// Initial setup, create credentials instance.
const credentials = PDFToolsSdk.Credentials
.serviceAccountCredentialsBuilder()
.fromFile("pdftools-api-credentials.json")
.build();
// Create an ExecutionContext using credentials and create a new operation
instance.
const executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
createPdfOperation = PDFToolsSdk.CreatePDF.Operation.createNew();
// Set operation input from a source file.
const input = PDFToolsSdk.FileRef.createFromLocalFile(filePath);
createPdfOperation.setInput(input);
// Execute the operation and Save the result to the specified location.
createPdfOperation.execute(executionContext)
.then((result) => {
result.saveAsFile('output/createPDFFromDOCX.pdf')
//download the file
res.redirect('/?response=PDF Successfully created')
.catch(err => {
if (err instanceof PDFToolsSdk.Error.ServiceApiError
|| err instanceof PDFToolsSdk.Error.ServiceUsageError) {
console.log('Exception encountered while executing operation',
err);
} else {
console.log('Exception encountered while executing operation',
err);
} catch (err) {
console.log('Exception encountered while executing operation', err);
//export all the functions
module.exports = { createPDF, createPDFPost };
转换后的PDF文档保存在输出目录中,而原始文件保存在上载目录中。
使用文本识别
光学字符识别(OCR)将图像和扫描的文档转换为可搜索的文件。 您可以转换 Acrobat Services 将API、图像和扫描的文档添加到可搜索的PDF中。 在执行OCR操作后,文件变为可编辑并可搜索。 您可以将文件的内容存储在数据存储中,以便索引和其他用途。
回想一下,对于许多文件管理和信息处理至关重要的公司来说,搜索和索引扫描文档非常重要。 OCR功能消除了这些难题。
要实施此功能,必须设计类似于上述上载表单的上传表单。 这一次,您可将表单限制为PDF文件,因为您只能对PDF文档使用OCR功能。
以下是该示例的上传表单:
const fs = require('fs')
const pdf = require('pdf-parse');
const mongoose = require('mongoose');
const Document = require('../models/document');
const PDFToolsSdk = require('@adobe/documentservices-pdftools-node-sdk');
* GET /makeOCR route to show the makeOCR form.
function makeOCR(req, res) {
//catch any response on the url
let response = req.query.response
res.render('ocr', { response })
* POST /makeOCRPost to create a new PDF File.
function makeOCRPost(req, res) {
let filePath = req.file.path;
let fileName = req.file.filename;
try {
// Initial setup, create credentials instance.
const credentials = PDFToolsSdk.Credentials
.serviceAccountCredentialsBuilder()
.fromFile("pdftools-api-credentials.json")
.build();
// Create an ExecutionContext using credentials and create a new operation
instance.
const executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
ocrOperation = PDFToolsSdk.OCR.Operation.createNew();
// Set operation input from a source file.
const input = PDFToolsSdk.FileRef.createFromLocalFile(filePath);
ocrOperation.setInput(input);
// Execute the operation and Save the result to the specified location.
ocrOperation.execute(executionContext)
.then(async (result) => {
let newFileName = `createPDFFromDOCX-${Math.random() * 171}.pdf`;
await result.saveAsFile(`output/${newFileName}`);
let documentContent = fs.readFileSync(
require("path").resolve("./") + `\\output\\${newFileName}`
pdf(documentContent)
.then(function (data) {
//Creates a new document
var newDocument = new Document({
documentName: fileName,
documentDescription: description,
documentContent: data.text,
url: require("path").resolve("./") + `\\output\\${newFileName}`
//Save it into the DB.
newDocument.save((err, docs) => {
if (err) {
res.send(err);
} else {
//If no errors, send it back to the client
res.redirect(
"/makeOCR?response=OCR Operation Successfully performed on
the PDF File"
.catch(function (error) {
// handle exceptions
console.log(error);
.catch(err => {
if (err instanceof PDFToolsSdk.Error.ServiceApiError
|| err instanceof PDFToolsSdk.Error.ServiceUsageError) {
console.log('Exception encountered while executing operation',
err);
} else {
console.log('Exception encountered while executing operation',
err);
} catch (err) {
console.log('Exception encountered while executing operation', err);
//export all the functions
module.exports = { makeOCR, makeOCRPost };
您需要 Acrobat Services 节点SDK、mongoose、pdf-parse和fs模块以及您的文档模型架构。 要将转换文件的内容保存到MongoDB数据库,这些模块是必需的。
现在创建两个功能:makeOCR以显示上传的表单,然后创建makeOCRPost来处理上传的文档。 将原始表单保存到数据库,然后将转换后的表单保存到应用程序的输出文件夹中。
在转换文件之前,将在每种情况下加载pdftools-api-credentials.json文件中Adobe提供的凭据。
OCR功能仅支持PDF文档。
此外,将下面的代码片段添加到应用程序的Modes/Document.js文件。
在代码片段中,定义一个单选模型,然后描述要存储在数据库中的文档属性。 此外,还可以为documentContent字段编制索引,以便轻松高效地搜索文本。
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
//Document schema definition
var DocumentSchema = new Schema(
documentName: { type: String, required: false },
documentDescription: { type: String, required: false },
documentContent: { type: String, required: false },
url: { type: String, required: false },
status: {
type: String,
enum : ["active","inactive"],
default: "active"
{ timestamps: true }
//for text search
DocumentSchema.index({
documentContent: "text",
//Exports the DocumentSchema for use elsewhere.
module.exports = mongoose.model("document", DocumentSchema);
现在,您可以实现简单的搜索功能,以使用户能够执行一些简单的文本搜索。 您还可以添加下载功能,以便下载PDF文件。
此功能需要一个简单的表单和卡片来显示搜索结果。 您可以在以下位置找到表单和卡片的设计: GitHub.
下面的屏幕截图说明了搜索功能和搜索结果。 您可以下载任何搜索结果。
const fs = require('fs')
const mongoose = require('mongoose');
const Document = require('../models/document');
* GET / route to show the search form.
function search(req, res) {
//catch any response on the url
let response = req.query.response
res.render('search', { response })
* POST /searchPost to search the contents of your saved file.
function searchPost(req, res) {
let searchString = req.body.searchString;
Document.aggregate([
{ $match: { $text: { $search: searchString } } },
{ $sort: { score: { $meta: "textScore" } } },
.then(function (documents) {
res.render('search', { documents })
.catch(function (error) {
let response = error
res.render('search', { response })
//export all the functions
module.exports = { search, searchPost, downloadPDF };
async function downloadPDF(req, res) {
console.log("here")
let documentId = req.params.documentId
let document = await Document.findOne({_id:documentId});
res.download(download.link);
在本实际操作教程中,您集成了 Acrobat Services 将API转换为Node.js应用程序,并使用API实现将文件转换为PDF的文档转换。 您添加了一个OCR功能,使图片和扫描文件可搜索。 然后,您已将文件保存到文件夹,以便可以下载它们。
接下来,您添加了一个搜索功能,用于搜索通过OCR转换为文本的文档。 最后,您实现了下载功能,可以轻松下载这些文件。 借助您完成的申请,法律公司可以更轻松地查找和处理特定文本。
使用 Acrobat Services 对于文档转换,我们强烈推荐,因为与其他服务相比,它稳健且易于使用。 您可以快速创建一个帐户,以便开始享用 Acrobat Services 用于文档转换和管理的API。
现在您已深入了解如何使用 Acrobat Services 在API方面,您可以通过实践来进一步提升技能。 您可以克隆本教程中使用的存储库,并尝试您刚刚学到的一些技能。 更棒的是,您可以尝试重建此应用程序,同时探索 Acrobat Services API。
准备好在自己的应用程序中启用文档共享和审阅了吗? 注册您的 Adobe Acrobat Services
开发人员帐户。 享受6个月的免费试用,然后 即付即用 随着您的业务增长,每次文档交易只需0.05美元。
recommendation-more-help