我需要使用pdf.js以正确的文本顺序从pdf获取纯文本。 我需要帮助Mozilla的pdf.js 2.x(我使用2.0.550)。 我发送了几天寻找解决方案但没有任何成功。
目标:使用Mozilla的pdf.js从pdf文件中提取正确的纯文本而不渲染pdf图像(canvas或svg),如果可能的话,更好的没有文本图层。
我知道这里是类似的问题,但没有答案 - 理由以及如何解决它。链接是PDF.js getTextContent returning text in wrong order 我还会在列表中查看其他问题并提供可能的答案。
可以从每个页面获取。我使用getTextContent()函数,它返回一个带有' items'数组的对象。有一个元素' .str'需要页面中的文字。 但顺序是错误的。是否有可能像在原始页面中那样获得正确的文本顺序?
结果你可以看到订单错了。 但是我很惊讶,因为相同或相似的代码渲染层显示正确的图像。当我用文本图层检查pdfjs应用程序时,我可以看到很多带有不同文本部分的标签。有时一行数字。我看起来每个标签都包含font \ style文本组。
附加: 我看到obj {}。items []。transform []数组,元素[4]像X一样,元素[5]像Y坐标。这是真的吗? 从顶部到底部以及从左到右手动排序并获得正确的文本顺序是否有用#str'元素? 也许在Pdf.js中有一些功能可以做得更好吗?
提前致谢。
技术规格: 链接到PDF文件进行测试: https://www.pdfill.com/example/pdf_commenting_new.pdf 与其他pdf相同的问题。
代码:
let pdfurl = "https://www.pdfill.com/example/pdf_commenting_new.pdf";
pdfjsLib.getDocument( pdfurl )
.then(function(pdf) {
pdf.getPage( i )
.then(function(page){
return page.getTextContent( render_options ) })
.then(function(textContent){
console.log( i, "# textContent :", textContent );
textContent.items.forEach( (el, ind) => console.log( (++ind) + ". " + el.str ) );
});
})
控制台中的结果:
1. How to align these objects
2. You can open a PDF or create a blank PDF by PDFill.
3. Here are the seven types of PDF Commenting created by PDFill
4. Goto Page 4: Text Box Tool
5. Goto Page 6: Sticky Note Tool
6. Goto Page 7: Popup Tool
7. Goto Page 8: File Attachment Tool
8. Goto Page 9: Play Video Tool
9. Goto Page 11: Line or Arrow Tool
10. Goto Page 12: Rectangle or Oval Tool
11. Next Page
12. Next Page
13. First Page
14. Previous Page
15. Next Page
16. Last Page
17. Please save into a new PDF to see the effect!
18. Online Help
19. PDFill: PDF Commenting or Annotation
20. Goto Page 13: Polyline, Cloud and Pencil Tool
21. Goto Page 2: Select Original Texts
22. Goto Page 5: Highlight Tool
23. Goto Page 10: Link Tool
24. Goto Page 3: Stamp Tool
配置: - Web浏览器及其版本:chrome版本66.0.3359.181(官方版本)(64位) - 操作系统及其版本:Linux 4.15.0-22-generic#24-Ubuntu SMP Wed May 16 12:15:17 UTC 2018 x86_64 x86_64 x86_64 GNU / Linux - NG模块 - pdfjs-dist - PDF.js版本:2.0.550(也是2.0.489) - 角度6 /节点8x / npm 5x / TS
答案 0 :(得分:0)
我明白了。 很快 - 将所有文本放入数组和/或集合中,并按相反的Y排序,然后从变换数组中选择X坐标以获得正确的顺序。
描述: 很高兴理解PDF文件中的Y轴是相反的方向。我得到了它,并在调试一段时间后找到有关它的附加信息。
因此,这是简单的工作,以获得正确的文本顺序。 1.按原样获取所有元素。 2.然后在Items数组转换数组中找到。 3.进入数组转换此元素的值X(第4个)和第Y个(第5个)位置。 4.我使用Lodash.js按最后得到的2个X和Y数组进行分组。关注你需要首先按Y从大到小排序,因为Y在PDF中是反向的。然后按X排序;
_.orderBy(pagetext, ['y','x'], ['desc', 'asc'] )
在下面的代码中,我想你有PDF页面对象。 代码:
// got text elements from getTextContent() to new array with page, x, y, and text
pagetext.push(
{ 'p': i, 'x': el.transform[4], 'y': el.transform[5], 't': el.str } )
// follow what we got
console.log( (++ind) + ". "
+" t: "+ el.transform
+" x= "+ el.transform[4]
+" y= "+ el.transform[5]
+" | "+ el.str );
})
return pagetext; // return all text as result from page
})
.then(function(ptext){
// be careful :)
// resort order by opposite Y and then X coordinates
let pagetext = ptext;
console.log( "# it is wrong # pagetext = ", pagetext );
let p2 = _.orderBy(pagetext, ['y','x'], ['desc', 'asc'] )
console.log( "# it is correct # pagetext = ", p2 );
});
这是一个关于页面上大约1个文本元素的数组示例。正如我在上面的问题中所示,你将以自定义顺序获得许多带有getTextContent的元素。
//From console:
Object
items : Array(24)
str : "How to align these objects"
transform : (6) [18, 0, 0, 18, 349.76, 335.25]
width : 190.78199999999998
__proto__
祝你好运:)
答案 1 :(得分:0)
我就是这样做的
//first sort the fragments by Y desc , X asc to order the text
const sorted = data.items.sort(
(a, b) => b.transform[5] - a.transform[5] || a.transform[4] - b.transform[4]
);
// generate the string chain
const text = sorted.map((e) => e.str).join();