You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PDFkit动态设置复选框为选中且禁用状态?

解决方案

要实现复选框默认选中且禁用(不可点击编辑),你只需要在doc.formCheckbox的配置选项中添加两个关键参数:

  • checked: true:设置复选框初始为选中状态
  • readOnly: true:将复选框设为只读,禁止用户修改勾选状态

修改后的完整代码如下:

// create a document and pipe to a blob
var doc = new PDFDocument();
var stream = doc.pipe(blobStream());

doc.initForm();

doc.formCheckbox('checked', 45,50,16,16, {
    backgroundColor: '#fff',
    checked: true,  // 设置默认选中
    readOnly: true  // 设置为只读,禁止编辑
})

// end and display the document in the iframe to the right
doc.end();
stream.on('finish', function() {
  iframe.src = stream.toBlobURL('application/pdf');
});

说明

  • checked 属性直接控制复选框的初始勾选状态,设为true即可让它默认选中。
  • readOnly 属性会锁定表单字段,用户无法通过点击来切换复选框的状态,达到禁用交互的效果。

内容的提问来源于stack exchange,提问作者The Dead Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 12:21:49