Tho*_*dez 2 javascript pdf pdftotext pdf.js
我使用本教程中的代码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/ 上查看了有关如何格式化转换的一些提示,但找不到任何内容。我只是想知道是否有人知道如何在\n使用 pdf.js 解析文本时 显示换行符。
提前致谢。
在 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;
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://npmcdn.com/pdfjs-dist/build/pdf.js"></script>Run Code Online (Sandbox Code Playgroud)