我使用了本教程中的代码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
。
提前致谢。
答案 0 :(得分:3)
在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>
&#13;