使用坐标数组将 PDF 文本解析为 CSV

发布于 2025-01-09 09:17:55 字数 2731 浏览 0 评论 0原文

我有一个如下输入文件,其中包含数字化 OCR 文本

https://drive.google.com/drive/folders/1mAzjcHKX1tsKhNvTtF8InhkXFZbmdbKz?usp=sharing

此 PDF 包含从第 3 页开始的人员详细信息

预期输出基于特定位置 我正在尝试使用 PDF 位置区域数组来完成,因为所有页面在相同区域都有文本

预期的输出数组是 “1”,“1YO6963656”,“ववनितासुनीलभोईर',”तीचेनाव。 सुनील भोईर","घर क्रमांक", "32 लिंग:महिला" ... ...

我尝试过

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.min.js" integrity="sha512-qa1o08MA0596eSNsnkRv5vuGloSKUhY09O31MY2OJpODjUVlaL0GOJJcyt7J7Z61FiEgHMgBkH04ZJ+vcuLs/w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js" integrity="sha512-S9Dwzi4TCjPQkxlaXsqQLj2gXUjPZk4HBzE7zWU6Itc1r2RNmlBrVLH4EsYQrdnzLgvkN8P7l9SCru+2I4rZwg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<label for="myfile">Select a file:</label>
<input type="file" id="file" name="file">



<script>
/*Offical release of the pdfjs worker*/
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.worker.js';

document.getElementById('file').onchange = function(event) {
  separator = ' '
  var file = event.target.files[0];
  var fileReader = new FileReader();
  fileReader.onload = function() {
    var typedarray = new Uint8Array(this.result);
    console.log(typedarray);

    let pdf = pdfjsLib.getDocument(typedarray);
    return pdf.promise.then(function(pdf) { // get all pages text
        let maxPages = pdf._pdfInfo.numPages;
        let countPromises = []; // collecting all page promises
        for (let i = 1; i <= maxPages; i++) {
            let page = pdf.getPage(i);
            countPromises.push(page.then(function(page) { // add page promise
                let textContent = page.getTextContent();
                return textContent.then(function(text) { // return content promise
                    return text.items.map(function(obj) {
                        return obj.str;
                    }).join(separator); // value page text
                });
            }));
        };

        return Promise.all(countPromises).then(function(texts) {
            for(let i = 0; i < texts.length; i++){
                texts[i] = texts[i].replace(/\s+/g, ' ').trim();
            };
            console.log(texts)
            alert(texts)
            return texts;
        });
    });
  }
  fileReader.readAsArrayBuffer(file);
}

</script>

I have a input file as below which has digitized OCR text

https://drive.google.com/drive/folders/1mAzjcHKX1tsKhNvTtF8InhkXFZbmdbKz?usp=sharing

This PDF has person details starting from page3

The expected output is to based on the certain locations
I am trying to accomplish using the array of zones of PDF locations since all of pages have text at same zones

The expected output array is
"1","1YO6963656","व वनिता सुनील भोईर',"तीचे नाव. सुनील भोईर","घर क्रमांक", "32 लिंग:महिला"
...
...

I have tried with

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.min.js" integrity="sha512-qa1o08MA0596eSNsnkRv5vuGloSKUhY09O31MY2OJpODjUVlaL0GOJJcyt7J7Z61FiEgHMgBkH04ZJ+vcuLs/w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js" integrity="sha512-S9Dwzi4TCjPQkxlaXsqQLj2gXUjPZk4HBzE7zWU6Itc1r2RNmlBrVLH4EsYQrdnzLgvkN8P7l9SCru+2I4rZwg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<label for="myfile">Select a file:</label>
<input type="file" id="file" name="file">



<script>
/*Offical release of the pdfjs worker*/
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.worker.js';

document.getElementById('file').onchange = function(event) {
  separator = ' '
  var file = event.target.files[0];
  var fileReader = new FileReader();
  fileReader.onload = function() {
    var typedarray = new Uint8Array(this.result);
    console.log(typedarray);

    let pdf = pdfjsLib.getDocument(typedarray);
    return pdf.promise.then(function(pdf) { // get all pages text
        let maxPages = pdf._pdfInfo.numPages;
        let countPromises = []; // collecting all page promises
        for (let i = 1; i <= maxPages; i++) {
            let page = pdf.getPage(i);
            countPromises.push(page.then(function(page) { // add page promise
                let textContent = page.getTextContent();
                return textContent.then(function(text) { // return content promise
                    return text.items.map(function(obj) {
                        return obj.str;
                    }).join(separator); // value page text
                });
            }));
        };

        return Promise.all(countPromises).then(function(texts) {
            for(let i = 0; i < texts.length; i++){
                texts[i] = texts[i].replace(/\s+/g, ' ').trim();
            };
            console.log(texts)
            alert(texts)
            return texts;
        });
    });
  }
  fileReader.readAsArrayBuffer(file);
}

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文