我花了一些时间才弄明白,但我已经成功地向用pdfkit生成的PDF添加了一个签名域.我使用以下函数添加该字段:

const formSig = ((pdfDoc, name, x, y, w, h, options = {}) => {
    const annotOptions = {
        ...options,
        FT: 'Sig',
    }
    pdfDoc.formAnnotation(name, null, x, y, w, h, annotOptions)
});

但是,我现在希望控制在有人对文档进行签名时锁定哪些字段,以便我可以请求多个签名.我知道我需要采取什么步骤,但我不确定该怎么做.

  1. 添加一个签名字段锁字典,将Type设置为SigFieldLock,将Action设置为Include,并将Fields设置为字段名字符串array.
  2. 在签名字段中添加一个引用锁词典的Lock条目.

我正在努力找出哪种方法可以让我添加字典,找到它的引用,并在添加签名字段时包括引用.

Update

我相信我已经知道如何创建词典并将其引用添加到字段选项中,但是词典没有写入到流中.

有没有人对pdfkit了如指掌,能给我指个方向?

推荐答案

我设法让它工作起来了.在pdfkit中,基本上可以使用ref()方法创建任何字典对象.我学到的另一件事是,要为锁词典创建字段数组,我需要将每个字符串转换为String以便pdfkit正确编码.

我使用了以下代码:

const formSig = ((pdfDoc, name, x, y, w, h, fieldsToLock, options = {}) => {
    // Converts the array of strings to an array of Strings
    const lockArray = fieldsToLock.map((fieldName) => {
        return new String(fieldName);
    })
    // Call the function to create the lock dictionary and capture its reference
    const lockRef = lockDict(pdfDoc, lockArray)

    // Build the annotation options including the field type, make the field require and a reference
    // to the lock dictionary in the Lock field
    const annotOptions = {
        ...options,
        FT: 'Sig',
        Ff: 2,
        Lock: lockRef,
    }
    // Add the field to the dictionary
    pdfDoc.formAnnotation(name, null, x, y, w, h, annotOptions)
});

const lockDict = ((pdfDoc, locks) => {
    // Build the Lock dictionary object to be added to the PDF
    const dict = {
        Type: 'SigFieldLock',
        Action: 'Include',
        Fields: locks
    }
    // Create a dictionary object reference and capture the reference
    let dictRef = pdfDoc.ref(dict);
    // Write the reference to the PDF stream
    dictRef.end();
    // Return the reference
    return fieldRef;
});

我需要添加一些错误处理和逻辑(以防没有要锁定的字段),但它可以工作.我用锁定不同字段的多个签名和锁定相同字段的多个签名对其进行了测试.

Javascript相关问答推荐

拖放仅通过 Select 上传

无法在nextjs应用程序中通过id从mongoDB删除'

v—自动完成不显示 Select 列表中的所有项目

在执行异步导入之前判断模块是否已导入()

使用Java脚本导入gltf场景并创建边界框

material UI按钮组样式props 不反射

在运行时使用Next JS App Router在服务器组件中运行自定义函数

用JS从平面文件构建树形 struct 的JSON

JavaScript不重定向配置的PATH

如何用javascript更改元素的宽度和高度?

Cherrio JS返回父div的所有图像SRC

自动滚动功能在当前图像左侧显示上一张图像的一部分

无法使用npm install安装react-dom、react和next

Promise.race()返回已解析的promise ,而不是第一个被拒绝的promise

需要刷新以查看Mern堆栈应用程序中的更改

使用Java脚本筛选数组中最接近值最小的所有项

打字脚本中的函数包装键入

webpack 5和mini-css-extract-plugin在将scss保存到css文件后不加载css

查找函数句柄的模块/文件

如何循环通过一个参数的键,该参数可以是TypeScrip中三个不同接口之一?