importAnnotCommand返回未定义

时间:2019-05-23 15:55:57

标签: javascript pdftron

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

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

导入代码,积压中包含无法加载的注释:

[{
  "name": "n1",
  "country": "EMA",
  "budget": 789,
  "goal": "Awareness",
  "category": "Technology"
}, {
  "name": "n2",
  "country": "KSA",
  "budget": 449,
  "goal": "Awareness",
  "category": "Sports"
}]

Ninja.find({
        country:req.query.lat,
        budget:req.query.lng
       }).then(function(ninjas){
        res.send(ninjas);
    }).catch(next);
});

xfdf失败的示例:

        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>

以下是我收到的错误消息,但是每次我回到未定义状态时,错误消息都不会消失

?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>

请注意,我的代码中的第224行是上方的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

我希望importAnnotCommand(xfdf)会返回导入的批注的列表,但有时会失败,只是返回未定义的内容。

1 个答案:

答案 0 :(得分:0)

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