PDF处理控件aspose.PDF功能演示:将 PDF 转换为 Word 文档

在 Web 应用程序中处理文档时,将 PDF 文件无缝转换为 Word 文档的能力是一项宝贵的资产。此任务不仅常见,而且对于文档转换器和编辑器、从编辑和协作到内容提取的各种应用程序来说也是必不可少的。在这篇博文中,我们将探讨如何使用 JavaScript 将 PDF 文件转换为 Word DOC/DOCX 文档。

Aspose.PDF 是一款高级PDF处理API,可以在跨平台应用程序中轻松生成,修改,转换,呈现,保护和打印文档。无需使用Adobe Acrobat。此外,API提供压缩选项,表创建和处理,图形和图像功能,广泛的超链接功能,图章和水印任务,扩展的安全控件和自定义字体处理。本文将为你介绍如何在 C++ 中将PDF转换为Doc 、Docx 。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.pdf 最新下载(qun:666790229)https://www.evget.com/product/4118/download

将 PDF 转换为 Word 的 JavaScript 库

对于 JavaScript 中的 PDF 到 Word 文档转换,我们将使用Aspose.PDF for JavaScript。它是一个综合性库,使开发人员能够以编程方式进行 PDF 生成、操作、编辑和转换。该库的设计易于使用,并无缝集成到 JavaScript 应用程序中,使其成为 PDF 相关任务的理想选择。

下载该库并按照此处提供的安装说明进行操作:安装 Aspose.PDF for JavaScript。

在 JavaScript 中将 PDF 转换为 Word DOC

使用 Aspose.PDF,您不必经历复杂的 PDF 到 Word 转换过程。只需加载 PDF 文件并将其保存为 Word 格式即可。但是,我们会将资源密集型 PDF 到 DOC 转换任务卸载给 Web Worker,以防止阻塞主 UI 线程。这确保了在 Web 应用程序中以用户友好的方式下载转换后的 Word 文档。

以下是在 JavaScript 中将 PDF 转换为 DOC 所需执行的步骤。

  1. 创建一个 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/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;
}
  1. 按照以下步骤编写将 PDF 转换为 DOC 的代码。
  • 首先,选择您要转换的 PDF 文件。
  • 然后,创建一个新的FileReader对象。
  • 调用AsposePdfToDoc函数将PDF转换为Word格式。此函数还接受转换后的 Word 文件的名称。
  • 接下来,如果json.errorCode 为 0,则生成的 Word 文件将采用您之前指定的名称。否则,您的文件中将会出现错误,并且错误消息将记录在json.errorText文件中。
  • 最后,DownloadFile函数生成一个链接来下载转换后的 Word 文件。

下面是使用 JavaScript 将 PDF 转换为 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]);
}
在 JavaScript 中将 PDF 转换为 DOCX

如果您需要将 PDF 转换为 DOCX 格式,您可以按照相同的过程进行少量更改,以获得 DOCX 格式的 Word 文档。让我们用 JavaScript 将 PDF 转换为 DOCX 文档。

使用下面的代码片段创建 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;
}
  1. 现在,编写将 PDF 转换为 DOCX 的代码。在这里,您将使用方法AsposePdfToDocX 而不是AsposePdfToDoc
复制代码
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 文件转换为 Word 文档的过程。本博文中提供的步骤和代码片段简化了 JavaScript 应用程序中 PDF 到 DOC 和 PDF 到 DOCX 的转换。凭借其简单的集成和强大的功能,Aspose.PDF 简化了文档操作任务,使开发人员能够通过高效的 PDF 到 Word 转换来增强其应用程序。

相关推荐
一颗无畏豆儿3 小时前
word出现“错误!未找到引用源”问题,以及锁定(和解除)目录更新域
word
魔法阵维护师3 小时前
从零开发游戏需要学习的c#模块,第二十章(2D 敌人与战斗触发)
学习·游戏·c#
我是唐青枫5 小时前
C#.NET YARP + OpenTelemetry:网关链路追踪实战
开发语言·c#·.net
2601_958492555 小时前
7 Best WordPress Tools to Help Your News Site Actually Make Money
前端·word
w2018005 小时前
新高考答题卡模板全套PDF可打印(语文数学英语等)
pdf·高考
奋斗的老史5 小时前
LibreOffice封装文档转 PDF 工具类
java·pdf
优化控制仿真模型6 小时前
【26年最新】新高考英语大纲词汇表3500个电子版PDF(含正序版、乱序版和默写版)
经验分享·pdf
诸葛大钢铁6 小时前
OFD如何转Word?OFD转为可编辑Word的两种方法
经验分享·word·ofd·ofd转word
Ws_6 小时前
C# 学习 Day1
开发语言·学习·c#
魔法阵维护师6 小时前
从零开发游戏需要学习的c#模块,第二十一章(精灵动画 —— 让角色走起来)
学习·游戏·c#