通过 C++ 通过 JavaScript 转换

将 PDF 导出为微软 Office® Word、Excel、PowerPoint 演示文稿、图像、EPUB 和固定布局

Aspose.PDF for JavaScript via C++ Logo

概述

你是否在寻找一种使用 JavaScript 和 C++ 将 PDF 文件转换为其他格式的方法?通过 C++ for JavaScript 的 Aspose.PDF 是转换 PDF 文档的完美解决方案。本文将演示如何通过 C++ 使用 JavaScript 将 PDF 转换为文本。PDF 文件可以包含各种内容,例如文本、图像、可点击按钮、超链接、嵌入字体、签名和图章。将 PDF 文件转换为其他格式时,用户通常希望能够编辑 PDF 内容。通过 C++ 使用 Aspose.PDF for JavaScript,你可以轻松快速地将 PDF 文档转换为最流行的格式。我们的资料库可确保您的PDF文件得到准确、成功的转换。

将 PDF 转换为 DOC

示例:用于 PDF 到 DOC 转换的 JavaScri

// 选择要转换的 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]);
}

适用于 JavaScript 的 Aspose.PDF 支持直接在网上将 PDF 转换为 DOC。首先,我们创建 “文件阅读器”。然后,我们调用 AsposePDFtoDoc 函数。网络工作者已设置为使用 onmessage 事件处理程序监听消息。当它收到来自网页的消息时,它会处理请求并将响应发送回主线程。我们可以通过下载文件功能生成生成的文件。DownloadFile 函数负责生成一个允许用户下载文件的链接。接下来,如果 “json.errorCode” 为 0,则 json.FileNameResult 将包含你之前指定的名称。如果 “json.ErrorCode” 参数不等于 0,因此您的文件中会出现错误,则有关此类错误的信息将包含在 “json.ErrorText” 属性中。生成的文件的名称是 “resultpdftodoc.doc”。

将 PDF 另存为 XLSX

将 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 属性和错误文本属性。如果 ErrorCode 为 0,则表示转换成功并且 JSON 对象还具有 FileNameResult 属性,该属性包含输出 Excel 文件的名称。如果 errorCode 不是 0,则表示发生了错误。生成的文件的名称是 “resultpdftoxlsx.xlsx”。

将 PDF 转换为幻灯片演示文稿

示例:JavaScript 代码 PDF 转换为 Power

// 选择要转换的 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";

// 调用 asposePDFTopptx 函数。
// 结果文件的名称已设置,在本示例中为 “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 属性和错误文本属性。如果 errorCode 为 0,则表示转换成功并且 JSON 对象还具有 FileNameResult 属性,该属性包含输出 PowerPoint 文件的名称。如果 ErrorCode 不是 0,则表示发生了错误并且 ErrorText 属性包含错误消息。因此,下载文件功能为输出的 PowerPoint 文件创建了下载链接。该函数采用输出文件名 “resultpdftopptx.pptx”。

便携式文档格式 PDF 到 EPUB 的转换

示例:将 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 属性。如果 ErrorCode 为 0,则表示转换成功并且 JSON 对象还具有 FileNameResult 属性,该属性包含输出 EPUB 文件的名称。如果 ErrorCode 不是 0,则表示发生了错误并且 ErrorText 属性包含错误消息。因此,DownloadFile 函数为输出 EPUB 文件创建了下载链接。该函数采用输出文件名 “resultpdftoepub.epub”。

将 PDF 转换为 PNG

示例:将 PDF 转换为 PNG 的 JavaScrip

// 选择要转换的 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 属性和错误文本属性。因此,每个输出文件的 downloadFile 函数都会为 PNG 图像创建一个下载链接。该函数采用输出文件名 “resultpdftoPng {0: D2} .png”。