2017-06-05 5 views
-1

このチュートリアルのコードhttp://ourcodeworld.com/articles/read/405/how-to-convert-pdf-to-text-extract-text-from-pdf-with-javascriptを使用して、pdfからテキストへの変換を設定しました。pdf.jsを使用してpdfからテキストへの変換で ` n`を表示する

https://mozilla.github.io/pdf.js/コンバージョンのフォーマット方法に関するヒントはありますが、何も見つかりませんでした。 pdf.jsを使用してテキストを解析するときに誰かが改行を表示する方法を知りたいのであれば、\nと思っています。

ありがとうございます。

+0

あなたはstring.replace 'のようなもので、' \\ N 'に '\のN'と' \\ r'と同じで任意の '\のr'を交換しようとしたことがあり( '\ '\ n '、' \\ n'); '?、note:' \ r'(キャリッジリターン)を知らない人は、一般に改行とペアになりますいくつかの環境(窓など)で文字を –

+0

ええ、試しました。 '\ n'は存在しません。私はpdf.jsが改行文字を見落とすだけであることを心配しています。 –

答えて

0

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>

関連する問題