跳到内容
  • 福昕首页
  • 开发中心
  • SDK文档资料
  • 福昕首页
  • 开发中心
  • SDK文档资料
申请试用
  • 企业自动化
    • Compressor
  • 福昕CloudAPI
  • 福昕PDF SDK 软件开发工具包
    • 福昕PDF SDK(ActiveX)
    • 福昕PDF SDK(桌面/服务器)
    • 福昕PDF SDK(Plug-in)
    • 福昕 PDF SDK(安卓)
    • 福昕PDF SDK(iOS)
    • 福昕PDF SDK(Web)
  • 福昕管理控制台
    • 公有云
    • 私有云
    • 通用情况
  • 福昕阅读器
    • RMS插件
  • 福昕高级编辑器
    • AI助手
    • Mac版本
      • 常规问题
    • windows版本
      • ECM集成
      • 互联PDF
      • 企业管理指南
      • 保护
      • 内容编辑
      • 创建PDF
      • 压缩
      • 图章
      • 安装与卸载
      • 常见问题
      • 打印
      • 注释/评论
      • 福昕插件
      • 翻译助手
      • 翻译助手教程
      • 试用与激活
      • 转换
      • 页面管理
    • 教育用户
      • 论文查重
      • 论文畅
      • 操作指南
    • 网页版
      • Foxit eSign
      • 电子签章
    • 订阅
    • 资源
  • 福昕高级编辑器Linux版本
  • 福船图纸管理系统
  • 福昕PDF SDK 软件开发工具包 > 福昕PDF SDK(Web)
  • 标签:
  • WebSDK,变量,注释,监听事件,组合

WebSDK 实现注释组合与取消组合

  • 福昕知识库
  • 2025-10-29

在WebSDK中多个注释需要组合,可以参考以下示例代码

1. 通过监听事件获取当前是否有多个选中的注释

//全局变量记录选中的注释
        var annotsActive = [];

//添加监听判断选中的注释
        pdfui.eventEmitter.on(PDFViewCtrl.constants.ViewerEvents.activeAnnotation, (annotRender) => {
            console.log('activeAnnotation',annotRender)
            const isExist = annotsActive.some(item => item.id === annotRender.annot.id);
            if (isExist) {
                return;
            }
            var annotJson = {
                type: annotRender.annot.getType(),
                rect:annotRender.annot.getRect(),
                borderStyle: {
                    width: annotRender.annot.getBorderWidth(), // 边框宽度
                    style: annotRender.annot.getBorderStyle(), // 边框样式
                    color: annotRender.annot.getBorderColor() // 边框颜色 
                },
            };
            annotsActive.push({ id: annotRender.annot.id, annotJson: annotJson,pageindex: annotRender.annot.page.getIndex() ,annot:annotRender.annot});
        })

//添加监听判断取消选中的注释
        pdfui.eventEmitter.on(PDFViewCtrl.constants.ViewerEvents.unActiveAnnotation, (annotRender) => {
            console.log('unActiveAnnotation',annotRender)
            const index = annotsActive.findIndex(item => item.id === annotRender.annot.id);
            if (index!== -1) {
                annotsActive.splice(index, 1);
            }
        });

//多选监听事件
        pdfui.eventEmitter.on(PDFViewCtrl.constants.ViewerEvents.activeMultipleAnnotations, (annotRenders) => {
            console.log('activeMultipleAnnotations',annotRenders)
            annotRenders.forEach(annotRender => {
                if (!annotsActive.some(item => item.id === annotRender.annot.id)) {
                    // 若结构不同,需转换为 annotsActive 所需的类型(此处假设结构兼容)
                    
                    var annotJson = {
                        type: annotRender.annot.getType(),
                        rect:annotRender.annot.getRect(),
                        borderStyle: {
                            width: annotRender.annot.getBorderWidth(), // 边框宽度
                            style: annotRender.annot.getBorderStyle(), // 边框样式
                            color: annotRender.annot.getBorderColor() // 边框颜色 
                        },
                    };
                    annotsActive.push({ id: annotRender.annot.id, annotJson: annotJson ,pageindex: annotRender.annot.page.getIndex(),annot:annotRender.annot});
                }
            });
        })

2. 组合注释代码

async function 组合注释(){
            if(annotsActive.length === 0){
                return;
            }

            var pdfviewer = await pdfui.getPDFViewer();
            var pdfdoc = await pdfviewer.getCurrentPDFDoc();
            var page = await pdfdoc.getPageByIndex(annotsActive[0].pageindex);
            //要是page不一样,提示当前选中的组合不在一个页面,重新选择?
            if(annotsActive.some(item => item.pageindex !== annotsActive[0].pageindex)){
                alert('当前选中的组合不在一个页面,重新选择?');
                return;
            }

            const allAnnots = annotsActive.map(item => item.annotJson);
            await page.addAnnotGroup(allAnnots,1)

            const allAnnotsId = annotsActive.map(item => item.id);
            for (const id of allAnnotsId) {
                // 等待当前标注删除完成后,再处理下一个
                await page.removeAnnotByObjectNumber(id);
            }
        }

3. 取消组合注释代码

async function 取消注释(){
            if(annotsActive.length != 1 || annotsActive[0].annot.groupElements.length === 0){
                return;
            }
            
            var pdfviewer = await pdfui.getPDFViewer();
            var pdfdoc = await pdfviewer.getCurrentPDFDoc();
            var page = await pdfdoc.getPageByIndex(annotsActive[0].pageindex);

            //根据annotsActive[0].annot.groupElements删除里面具体的内容
            for(const item of annotsActive[0].annot.groupElements)
            {
                var annotJson = {
                        type: item.getType(),
                        rect: item.getRect(),
                        borderStyle: {
                            width: item.getBorderWidth(), // 边框宽度
                            style: item.getBorderStyle(), // 边框样式
                            color: item.getBorderColor() // 边框颜色 
                        },
                    };
                await page.addAnnot(annotJson);
            }

            await page.removeAnnotByObjectNumber(annotsActive[0].id)
        }

相关内容

如何将注释中的富文本格式设置为默认格式

PDF文档如何设置数字签名?

如何获取签名的日期、签名人等信息?

如何使用PDF格式文件作为图标来源?

如何将彩色PDF一键转为黑白版?

PDF转word因权限限制报错:any unknown error occurs.

PDFViewCtrl中如何隐藏文本选中菜单

【福昕管理控制台】自动停用功能常见问题解答

如何通过示例 Demo生成license-key.js?

如何去除PDF中的水印?

推荐内容

如何将注释中的富文本格式设置为默认格式

PDF文档如何设置数字签名?

WebSDK 实现注释组合与取消组合

如何获取签名的日期、签名人等信息?

如何使用PDF格式文件作为图标来源?

如何将彩色PDF一键转为黑白版?

PDF转word因权限限制报错:any unknown error occurs.

PDFViewCtrl中如何隐藏文本选中菜单

【福昕管理控制台】自动停用功能常见问题解答

如何通过示例 Demo生成license-key.js?

产品
  • 应用行业
  • 白皮书
开发支持
  • 开发中心
  • SDK文档资料

销售咨询:010-50951668

客服电话:0591-38509808

销售咨询
微信公众号

©2025 福建福昕软件开发股份有限公司 版权所有

隐私策略