gpt4 book ai didi

javascript - 使用 pdf.js 在 pdf 到文本转换中将换行符显示为 `\n`

转载 作者:行者123 更新时间:2023-12-05 09:18:27 25 4
gpt4 key购买 nike

我使用了本教程中的代码 http://ourcodeworld.com/articles/read/405/how-to-convert-pdf-to-text-extract-text-from-pdf-with-javascript设置 pdf 到文本的转换。

在这个网站上看了一遍https://mozilla.github.io/pdf.js/有关如何格式化转换的一些提示,但找不到任何内容。我只是想知道在使用 pdf.js 解析文本时是否有人知道如何将换行符显示为 \n

提前致谢。

最佳答案

在 PDF 中,没有使用诸如“\n”之类的控制字符来控制布局这样的东西——PDF 中的字形使用精确坐标定位。使用文本 y 坐标(可以从变换矩阵中提取)来检测行变化。

var url = "https://cdn.mozilla.net/pdfjs/tracemonkey.pdf";
var pageNumber = 2;
// Load document
PDFJS.getDocument(url).then(function (doc) {
// Get a page
return doc.getPage(pageNumber);
}).then(function (pdfPage) {
// Get page text content
return pdfPage.getTextContent();
}).then(function (textContent) {
var p = null;
var lastY = -1;
textContent.items.forEach(function (i) {
// Tracking Y-coord and if changed create new p-tag
if (lastY != i.transform[5]) {
p = document.createElement("p");
document.body.appendChild(p);
lastY = i.transform[5];
}
p.textContent += i.str;
});
});
<script src="https://npmcdn.com/pdfjs-dist/build/pdf.js"></script>

关于javascript - 使用 pdf.js 在 pdf 到文本转换中将换行符显示为 `\n`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44376415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com