翻譯|行業(yè)資訊|編輯:胡濤|2024-03-06 09:47:53.590|閱讀 90 次
概述:在這篇博文中,我們將探討如何使用 JavaScript 將 PDF 文件轉(zhuǎn)換為 Word DOC/DOCX 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在 Web 應(yīng)用程序中處理文檔時,將 PDF 文件無縫轉(zhuǎn)換為 Word 文檔的能力是一項寶貴的資產(chǎn)。此任務(wù)不僅常見,而且對于文檔轉(zhuǎn)換器和編輯器、從編輯和協(xié)作到內(nèi)容提取的各種應(yīng)用程序來說也是必不可少的。在這篇博文中,我們將探討如何使用 JavaScript 將 PDF 文件轉(zhuǎn)換為 Word DOC/DOCX 文檔。
Aspose.PDF 是一款高級PDF處理API,可以在跨平臺應(yīng)用程序中輕松生成,修改,轉(zhuǎn)換,呈現(xiàn),保護和打印文檔。無需使用Adobe Acrobat。此外,API提供壓縮選項,表創(chuàng)建和處理,圖形和圖像功能,廣泛的超鏈接功能,圖章和水印任務(wù),擴展的安全控件和自定義字體處理。本文將為你介紹如何在 C++ 中將PDF轉(zhuǎn)換為Doc 、Docx 。
Aspose API支持流行文件格式處理,并允許將各類文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
對于 JavaScript 中的 PDF 到 Word 文檔轉(zhuǎn)換,我們將使用Aspose.PDF for JavaScript。它是一個綜合性庫,使開發(fā)人員能夠以編程方式進行 PDF 生成、操作、編輯和轉(zhuǎn)換。該庫的設(shè)計易于使用,并無縫集成到 JavaScript 應(yīng)用程序中,使其成為 PDF 相關(guān)任務(wù)的理想選擇。
下載該庫并按照此處提供的安裝說明進行操作:安裝 Aspose.PDF for JavaScript。
使用 Aspose.PDF,您不必經(jīng)歷復(fù)雜的 PDF 到 Word 轉(zhuǎn)換過程。只需加載 PDF 文件并將其保存為 Word 格式即可。但是,我們會將資源密集型 PDF 到 DOC 轉(zhuǎn)換任務(wù)卸載給 Web Worker,以防止阻塞主 UI 線程。這確保了在 Web 應(yīng)用程序中以用戶友好的方式下載轉(zhuǎn)換后的 Word 文檔。
以下是在 JavaScript 中將 PDF 轉(zhuǎn)換為 DOC 所需執(zhí)行的步驟。
/*Create Web Worker*/ const AsposePDFWebWorker = new Worker("AsposePDFforJS.js"); AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`); AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent = (evt.data == 'ready') ? 'loaded!' : (evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "application/msword", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`; /*Event handler*/ const ffileToDoc = e => { const file_reader = new FileReader(); file_reader.onload = event => { /*Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc" - Ask Web Worker*/ AsposePDFWebWorker.postMessage({ "operation": 'AsposePdfToDoc', "params": [event.target.result, e.target.files[0].name, "ResultPDFtoDoc.doc"] }, [event.target.result]); }; file_reader.readAsArrayBuffer(e.target.files[0]); }; /*Make a link to download the result file*/ const DownloadFile = (filename, mime, content) => { mime = mime || "application/octet-stream"; var link = document.createElement("a"); link.href = URL.createObjectURL(new Blob([content], {type: mime})); link.download = filename; link.innerHTML = "Click here to download the file " + filename; document.body.appendChild(link); document.body.appendChild(document.createElement("br")); return filename; }
下面是使用 JavaScript 將 PDF 轉(zhuǎn)換為 Word DOC 格式的代碼片段。
var ffileToDoc = function (e) { const file_reader = new FileReader(); file_reader.onload = (event) => { /*Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc"*/ const json = AsposePdfToDoc(event.target.result, e.target.files[0].name, "ResultPDFtoDoc.doc"); if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult; else document.getElementById('output').textContent = json.errorText; /*Make a link to download the result file*/ DownloadFile(json.fileNameResult, "application/msword"); } file_reader.readAsArrayBuffer(e.target.files[0]); }
如果您需要將 PDF 轉(zhuǎn)換為 DOCX 格式,您可以按照相同的過程進行少量更改,以獲得 DOCX 格式的 Word 文檔。讓我們用 JavaScript 將 PDF 轉(zhuǎn)換為 DOCX 文檔。
使用下面的代碼片段創(chuàng)建 Web Worker。
/*Create Web Worker*/ const AsposePDFWebWorker = new Worker("AsposePDFforJS.js"); AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`); AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent = (evt.data == 'ready') ? 'loaded!' : (evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`; /*Event handler*/ const ffileToDocX = e => { const file_reader = new FileReader(); file_reader.onload = event => { /*convert a PDF-file to DocX and save the "ResultPDFtoDocX.docx" - Ask Web Worker*/ AsposePDFWebWorker.postMessage({ "operation": 'AsposePdfToDocX', "params": [event.target.result, e.target.files[0].name, "ResultPDFtoDocX.docx"] }, [event.target.result]); }; file_reader.readAsArrayBuffer(e.target.files[0]); }; /// [Code snippet] /*make a link to download the result file*/ const DownloadFile = (filename, mime, content) => { mime = mime || "application/octet-stream"; var link = document.createElement("a"); link.href = URL.createObjectURL(new Blob([content], {type: mime})); link.download = filename; link.innerHTML = "Click here to download the file " + filename; document.body.appendChild(link); document.body.appendChild(document.createElement("br")); return filename; }
var ffileToDocX = function (e) { const file_reader = new FileReader(); file_reader.onload = (event) => { /*convert a PDF-file to DocX and save the "ResultPDFtoDocX.docx"*/ const json = AsposePdfToDocX(event.target.result, e.target.files[0].name, "ResultPDFtoDocX.docx"); if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult; else document.getElementById('output').textContent = json.errorText; /*make a link to download the result file*/ DownloadFile(json.fileNameResult, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); } file_reader.readAsArrayBuffer(e.target.files[0]); }
在這篇博文中,我們探索了使用 JavaScript 將 PDF 文件轉(zhuǎn)換為 Word 文檔的過程。本博文中提供的步驟和代碼片段簡化了 JavaScript 應(yīng)用程序中 PDF 到 DOC 和 PDF 到 DOCX 的轉(zhuǎn)換。憑借其簡單的集成和強大的功能,Aspose.PDF 簡化了文檔操作任務(wù),使開發(fā)人員能夠通過高效的 PDF 到 Word 轉(zhuǎn)換來增強其應(yīng)用程序。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn