我设法让它工作起来了.在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;
});
我需要添加一些错误处理和逻辑(以防没有要锁定的字段),但它可以工作.我用锁定不同字段的多个签名和锁定相同字段的多个签名对其进行了测试.