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
相关产品推荐
相关产品推荐

