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

Node.js中Document AI表单解析器表格多行文本读取问题及API版本疑问

问题描述

我希望实现和Document AI拖拽演示一样的PDF表格数据提取效果,但当表格行内包含多行文本时,我的JSON响应会将其识别为单独的行,云控制台的上传测试文档功能也存在同样问题。然而Document AI拖拽演示却能正确识别这类多行文本的表格行。请问这是否是由于使用了不同的API版本导致的?我当前使用的是Document AI v1beta3版本的表单解析器,相关Node.js代码如下:

核心代码片段

主逻辑部分

var dataTable;
for (const page of document.pages) {
  //tables
  for (const table of page.tables) {
    dataTable = printTableInfo(table, text);
    if (dataTable) return { table: dataTable };
  }
}

表格处理与文本提取函数

// printTableInfo function
function printTableInfo(table, text) {
  let dataArray = [];
  
  for (let i = 0; i < table.bodyRows.length; i++) {
    let bodyRowText = "";
    for (const bodyCell of table.bodyRows[i].cells) {
      const bodyCellText = getText(bodyCell.layout.textAnchor, text);
      bodyRowText += `${JSON.stringify(bodyCellText.trim())} | `;
    
    }
    dataArray.push({
      [`Data`]: bodyRowText,
    });
  }

  return dataArray;
}

// getText function
const getText = (textAnchor, text) => {
  if (!text || !textAnchor) return;

  if (
    (!textAnchor && !textAnchor.textSegments && !textAnchor.textSegments) ||
    (textAnchor &&
      textAnchor.textSegments &&
      textAnchor.textSegments.length) === 0
  ) {
    return "";
  }
  const startIndex = textAnchor.textSegments[0].startIndex || 0;
  const endIndex = textAnchor.textSegments[0].endIndex;

  return text.substring(startIndex, endIndex);
};

内容的提问来源于stack exchange,提问作者Zaahid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 06:14:57