你在寻找一种使用 JavaScript 和 C++ 将 PDF 文件转换为其他格式的方法吗?通过 C++ for JavaScript 的 Aspose.PDF 是转换 PDF 文档的完美解决方案。本文将演示如何通过 C++ 使用 JavaScript 将 PDF 转换为文本。PDF 文件可以包含各种内容,例如文本、图像、可点击的按钮、超链接、嵌入式字体、签名和图章。将 PDF 文件转换为另一种格式时,用户通常希望能够编辑 PDF 内容。通过 C++ 使用 JavaScript 的 Aspose.PDF,你可以轻松快速地将 PDF 文档与最流行的格式相互转换。我们的库可确保您的 PDF 文件准确、成功地转换。
PDF 到 DOC 的转换
// 选择要转换的 PDF 文件。
var ffileToDoc = function (e) {
// 创建一个 “文件阅读器”。
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc";
// 调用 asposePDFToDoc 函数。
// 结果文件的名称已设置,在本示例中为 “resultPDFTODoc.doc”。
const json = AsposePdfToDoc(event.target.result, e.target.files[0].name, "ResultPDFtoDoc.doc");
// Web Worker 中的消息处理
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
// 下载文件链接
DownloadFile(json.fileNameResult, "application/msword");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}
Aspose.PDF for JavaScript 支持直接在 Web 上将 PDF 转换为 DOC。首先,我们创建 “FileReader”。然后,我们调用 asposePDFToDoc 函数。网络工作者被设置为使用 onmessage 事件处理程序监听消息。当它收到来自网页的消息时,它会处理请求并将响应发送回主线程。我们可以通过下载文件功能变为结果文件。DownloadFile 函数负责生成一个允许用户下载文件的链接。接下来,如果 “json.errorCode” 为 0,那么 json.FileNameResult 将包含你之前指定的名称。如果 “json.errorCode” 参数不等于 0,因此您的文件中会出现错误,则有关此类错误的信息将包含在 “json.errorText” 属性中。生成的文件名是 ‘resultPDFTODoc.doc ‘。
将 PDF 另存为 XLSX
// 选择要转换的 PDF 文件。
var ffileToXlsX = function (e) {
// 创建一个 “文件阅读器”。
const file_reader = new FileReader();
file_reader.onload = (event) => {
// convert a PDF-file to XlsX and save the "ResultPDFtoXlsX.xlsx";
// 调用 asposePDFToxLSX 函数。
// 在此示例中,结果文件的名称已设置为 “resultpdftoxlsx.xlsx”。
const json = AsposePdfToXlsX(event.target.result, e.target.files[0].name, "ResultPDFtoXlsX.xlsx");
// Web Worker 中的消息处理
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
// 下载文件链接
DownloadFile(json.fileNameResult, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}
此代码是一个 JavaScript 函数,它使用 Aspose.PDF 将 PDF 文件转换为 Excel 文件。它使用事件对象作为参数,其中包含从文件输入元素中选择的 PDF 文件。它创建一个 FileReader 对象并为其 onload 事件分配一个回调函数。当文件作为数组缓冲区读取时,将执行回调函数。接下来,调用 asposepdftoxLSX 函数。该函数将文件内容、原始文件名和输出文件名作为参数,并返回一个包含结果的 JSON 对象。JSON 对象有一个 errorCode 属性和一个 errorText 属性。如果错误代码为 0,则表示转换成功,并且 JSON 对象还具有 FileNameResult 属性,其中包含输出 Excel 文件的名称。如果错误代码不是 0,则表示发生了错误。结果文件的名称是 ‘resultPDFtoxlsx.xlsx’。
将 PDF 转换为幻灯片演示文稿
// 选择要转换的 PDF 文件。
var ffileToPptX = function (e) {
// 创建一个 “文件阅读器”。
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to PptX and save the "ResultPDFtoPptX.pptx";
// 调用 asposePDFTOptX 函数。
// 在此示例中,结果文件的名称已设置为 “resultpdftopptx.pptx”。
const json = AsposePdfToPptX(event.target.result, e.target.files[0].name, "ResultPDFtoPptX.pptx");
// Web Worker 中的消息处理
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
// 下载文件链接
DownloadFile(json.fileNameResult, "application/vnd.openxmlformats-officedocument.presentationml.presentation");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}
此代码是一个 JavaScript 函数,它使用 Aspose.PDF 将 PDF 文件转换为 PowerPoint 文件。它使用事件对象作为参数,其中包含从文件输入元素中选择的 PDF 文件。接下来,创建一个 FileReader 对象并为其 onload 事件分配一个回调函数。当文件作为数组缓冲区读取时,会执行回调函数。调用 asposePDFTopptX 函数。该函数将文件内容、原始文件名和输出文件名作为参数,并返回一个包含结果的 JSON 对象。JSON 对象有一个 errorCode 属性和一个 errorText 属性。如果错误代码为 0,则表示转换成功,并且 JSON 对象还具有 FileNameResult 属性,其中包含输出 PowerPoint 文件的名称。如果 errorCode 不是 0,则表示发生了错误,并且 errorText 属性包含错误消息。因此,下载文件函数为输出 PowerPoint 文件创建下载链接。该函数的输出文件名为 “resultPDFtopptx.pptx”。
便携式文档格式 PDF 到 EPUB 转换
// 选择要转换的 PDF 文件。
var ffileToEPUB = function (e) {
// 创建一个 “文件阅读器”。
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to EPUB and save the "ResultPDFtoEPUB.epub";
// 调用 asposePDFToepub 函数。
// 在此示例中,结果文件的名称已设置为 “resultPDFtoepub.epub”。
const json = AsposePdfToEPUB(event.target.result, e.target.files[0].name, "ResultPDFtoEPUB.epub");
// Web Worker 中的消息处理
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
// 下载文件链接
DownloadFile(json.fileNameResult, "application/epub+zip");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}
此代码是一个 JavaScript 函数,它使用 Aspose.PDF 将 PDF 文件转换为 EPUB 文件。它创建一个 FileReader 对象并为其 onload 事件分配一个回调函数。接下来,调用 asposePDFToePub 函数。该函数将文件内容、原始文件名和输出文件名作为参数,并返回一个包含结果的 JSON 对象。JSON 对象有一个 errorCode 属性和一个 errorText 属性。如果错误代码为 0,则表示转换成功,并且 JSON 对象还具有 FileNameResult 属性,其中包含输出 EPUB 文件的名称。如果 errorCode 不是 0,则表示发生了错误,并且 errorText 属性包含错误消息。因此,DownloadFile 函数会为输出 EPUB 文件创建下载链接。该函数的输出文件名为 “resultPDFtoepub.epub”。
将 PDF 转换为 PNG
// 选择要转换的 PDF 文件。
var ffileToPng = function (e) {
// 创建一个 “文件阅读器”。
const file_reader = new FileReader();
file_reader.onload = (event) => {
// convert a PDF file to png-files with template "ResultPdfToPng{0:D2}.png" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save;
// 调用 asposePDFPageStopng 函数。
// 结果文件的名称已设置,在本示例中为 “resultPDFtoPng {0: D2} .png”。
const json = AsposePdfPagesToPng(event.target.result, e.target.files[0].name, "ResultPdfToPng{0:D2}.png", 150);
// Web Worker 中的消息处理
// 下载文件链接
if (json.errorCode == 0) {
document.getElementById('output').textContent = "Files(pages) count: " + json.filesCount.toString();
for (let fileIndex = 0; fileIndex < json.filesCount; fileIndex++) DownloadFile(json.filesNameResult[fileIndex], "image/png");
}
else document.getElementById('output').textContent = json.errorText;
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
此代码是一个 JavaScript 函数,它使用 Aspose.PDF 将 PDF 文件转换为 PNG 图像。它创建一个 FileReader 对象并为其 onload 事件分配一个回调函数。当文件作为数组缓冲区读取时,将执行回调函数。调用 asposePDFPageStopng 函数,该函数是 Aspose.PDF 的包装器。该函数将文件内容、原始文件名、输出文件名模板和分辨率作为参数,并返回一个包含结果的 JSON 对象。JSON 对象有一个 errorCode 属性和一个 errorText 属性。因此,每个输出文件的 downloadFile 函数会为 PNG 图像创建下载链接。该函数的输出文件名为 “resultPDFtoPng {0: D2} .png”。