importAnnotCommand 返回未定义

importAnnotCommand returning undefined

我正在尝试通过 importAnnotCommand() 方法使用 xfdf 格式导入手绘注释,但有时我得到 return 未定义的值,我正在使用 getAnnotCommand() 方法生成这些字符串。

xfdf 字符串由 getAnnotCommand() 方法生成,我将该字符串记录到控制台,以确保它看起来正确。注释仅可靠地加载在文档的前两页上。

导入代码,backlog包含加载失败的注解:

        const  backlog = docModel.prototype.annots[webviewer.id].backlog
        const  backup = docModel.prototype.annots[webviewer.id].backup
        let annots = annotationManager.importAnnotations(backup)
        annotationManager.drawAnnotationsFromList(annots)
        let tmp = [];
        if (Array.isArray(backlog)) {
          while(backlog.length > 0) {
            const backlogObj = backlog.pop()
            if (backlogObj.xfdf) {
              const xfdf = backlogObj.xfdf
              const annot = annotationManager.importAnnotCommand(xfdf)[0]
              if (annot === undefined) {
                tmp.push(backlogObj)
              } else {
              }
              console.log('loading annot:',annot,'\nxfdf:',xfdf)
            } else if(backlogObj.hide) {
              annotationManager.hideAnnotation(annotationManager.getAnnotationById(backlogObj.id))
            }
          }
        docModel.prototype.annots[webviewer.id].backlog = tmp

失败的 xfdf 示例:

<?xml version="1.0" encoding="UTF-8" ?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields />
<add><ink page="1" rect="302.917853,630.040373,348.65741,715.919953" color="#D5252E" flags="print" name="00d08e8b-01b7-e6d2-2f88-35890f19f16c" title="Guest" subject="Free hand" date="D:20190522154716-04'00'" width="4.2" creationdate="D:20190522154714-04'00'"><inklist><gesture>317.39,711.72;317.39,711.72;317.39,711.72;317.39,711.72;318.32,711.72;320.19,711.72;322.05,711.72;323.92,711.72;325.79,711.72;327.65,710.79;328.59,710.79;329.52,710.79;330.46,709.85;331.39,708.92;332.32,707.05;333.26,704.25;334.19,701.45;334.19,698.65;334.19,694.92;334.19,692.12;333.26,688.38;331.39,685.58;329.52,682.78;328.59,679.98;325.79,675.31;322.05,671.58;318.32,666.91;314.59,661.31;311.79,656.64;309.92,653.84;308.05,650.11;308.05,647.31;307.12,644.51;307.12,642.64;308.05,640.77;308.05,639.84;308.98,638.91;309.92,637.97;310.85,637.04;312.72,636.11;315.52,635.17;318.32,635.17;322.99,634.24;327.65,634.24;331.39,634.24;335.12,634.24;337.92,634.24;341.66,634.24;343.52,634.24;344.46,634.24;344.46,634.24</gesture></inklist></ink></add>
<modify />
<delete />
</xfdf>

有效的 xfdf 示例:

?xml version="1.0" encoding="UTF-8" ?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields />
<add><ink page="0" rect="325.321587,653.377596,335.588565,746.725088" color="#D5252E" flags="print" name="849c13ac-c0e6-e639-8d50-0c6b6639a3e2" title="Guest" subject="Free hand" date="D:20190522154712-04'00'" width="4.2" creationdate="D:20190522154710-04'00'"><inklist><gesture>329.52,742.53;330.46,742.53;330.46,741.59;330.46,739.72;330.46,737.86;330.46,733.19;330.46,726.66;331.39,717.32;331.39,707.99;331.39,698.65;331.39,692.12;331.39,674.38;331.39,667.85;331.39,663.18;331.39,661.31;331.39,659.44;331.39,658.51;331.39,657.58;331.39,657.58</gesture></inklist></ink></add>
<modify />
<delete />
</xfdf>

以下是我收到的错误消息,但每次返回 undefined 时它都不会消失

CoreControls.js:989 exportAnnotations was called before all annotations were loaded.
Gq  @   CoreControls.js:989
(anonymous) @   CoreControls.js:1479
Promise.then (async)        
(anonymous) @   CoreControls.js:1479
dispatch    @   jquery-3.2.1.min.js:3
q.handle    @   jquery-3.2.1.min.js:3
trigger @   jquery-3.2.1.min.js:4
triggerHandler  @   jquery-3.2.1.min.js:4
applyWithArgs   @   CoreControls.js:92
trigger @   CoreControls.js:92
Tg  @   CoreControls.js:978
HE  @   CoreControls.js:1014
Yw  @   CoreControls.js:1012
uo  @   CoreControls.js:1011
uo  @   CoreControls.js:990
updateAnnotList @   my_file.js:224

注意,我的代码中的第 224 行是上面的 const annot = annotationManager.importAnnotCommand(xfdf)[0]

我希望 importAnnotCommand(xfdf) 会 return 导入注释列表,但有时会失败,只是 return 未定义。

CoreControls.js:989 exportAnnotations was called before all annotations were loaded.

您似乎正在尝试导出到很快。初始加载文档后加载注释,如果有很多可能需要一段时间。

您想等待这个活动。

https://www.pdftron.com/documentation/web/guides/annotation-events#annotationsloaded

不过,在对文档执行任何操作之前,您需要等待 documentLoaded 事件。

https://www.pdftron.com/documentation/web/guides/webviewer-events/#documentviewerdocumentloaded