PDF Document Conversion via JavaScript via C++

Export PDF to Microsoft Office® Word, Excel, PowerPoint Presentations, Images, EPUB and fixed-layout formats

Overview

Are you searching for a method to transform PDF files into other formats using JavaScript and C++? Aspose.PDF for JavaScript via C++ is the perfect solution for converting PDF documents. This article will demonstrate how to convert PDF to text using JavaScript via C++. PDF files can include various content, such as text, images, clickable buttons, hyperlinks, embedded fonts, signatures, and stamps. When converting a PDF file to another format, users often want to be able to edit the PDF content. With Aspose.PDF for JavaScript via C++, you can easily and quickly convert your PDF documents to and from the most popular formats. Our library ensures that your PDF files are converted accurately and successfully.

PDF to DOC Conversion

Example: JavaScript Code for PDF to DOC Conversion

// Select a PDF file for converting.
var ffileToDoc = function (e) {

// Create a 'FileReader'.
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to Doc and save the "ResultPDFtoDoc.doc";

// Call the AsposePdfToDoc function.
// The name of the resulting file is set, in this example 'ResultPDFtoDoc.doc'.
const json = AsposePdfToDoc(event.target.result, e.target.files[0].name, "ResultPDFtoDoc.doc");

// Message Handling in the Web Worker
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;

// Download File link
DownloadFile(json.fileNameResult, "application/msword");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}

Aspose.PDF for JavaScript supports PDF to DOC conversion directly on the Web. First, we create ‘FileReader’. Then, we call the AsposePdfToDoc function. The web worker is set up to listen for messages using the onmessage event handler. When it receives a message from the web page, it processes the request and sends a response back to the main thread. The resultant file we can become by Download File Function. The DownloadFile function is responsible for generating a link that allows the user to download a file. Next, if the ‘json.errorCode’ is 0, then json.fileNameResult will contain the name you specified earlier. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ property. The name of the resulting file is ‘ResultPDFtoDoc.doc’.

Save PDF as XLSX

Save PDF as XLSX

// Select a PDF file for converting.
var ffileToXlsX = function (e) {

// Create a 'FileReader'.
const file_reader = new FileReader();
file_reader.onload = (event) => {
// convert a PDF-file to XlsX and save the "ResultPDFtoXlsX.xlsx";

// Call the AsposePdfToXlsX function.
// The name of the resulting file is set, in this example 'ResultPDFtoXlsX.xlsx'.
const json = AsposePdfToXlsX(event.target.result, e.target.files[0].name, "ResultPDFtoXlsX.xlsx");

// Message Handling in the Web Worker
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;

// Download File link
DownloadFile(json.fileNameResult, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}

This code is a JavaScript function that converts a PDF file to an Excel file using the Aspose.PDF. It takes an event object as a parameter, which contains the selected PDF file from a file input element. It creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer. Next, call the AsposePdfToXlsX function. The function takes the file content, the original file name, and the output file name as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. If the errorCode is 0, it means that the conversion was successful and the JSON object also has a fileNameResult property, which contains the name of the output Excel file. If the errorCode is not 0, it means that there was an error. The name of the resulting file is ‘ResultPDFtoXlsX.xlsx’.

Convert PDF to PowerPoint Presentations

Example: JavaScript Code PDF to PowerPoint Conversion

// Select a PDF file for converting.
var ffileToPptX = function (e) {

// Create a 'FileReader'.
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to PptX and save the "ResultPDFtoPptX.pptx";

// Call the AsposePdfToPptX function.
// The name of the resulting file is set, in this example 'ResultPDFtoPptX.pptx'.
const json = AsposePdfToPptX(event.target.result, e.target.files[0].name, "ResultPDFtoPptX.pptx");

// Message Handling in the Web Worker
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;

// Download File link
DownloadFile(json.fileNameResult, "application/vnd.openxmlformats-officedocument.presentationml.presentation");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}

This code is a JavaScript function that converts a PDF file to a PowerPoint file using the Aspose.PDF. It takes an event object as a parameter, which contains the selected PDF file from a file input element. Next, creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer.Call the AsposePdfToPptX function. The function takes the file content, the original file name, and the output file name as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. If the errorCode is 0, it means that the conversion was successful and the JSON object also has a fileNameResult property, which contains the name of the output PowerPoint file. If the errorCode is not 0, it means that there was an error and the errorText property contains the error message. As a result, the DownloadFile function creates a download link for the output PowerPoint file. The function takes the output file name ‘ResultPDFtoPptX.pptx’.

Portable Document Format PDF to EPUB Conversion

Example: JavaScript Code for PDF to EPUB Conversion

// Select a PDF file for converting.
var ffileToEPUB = function (e) {

// Create a 'FileReader'.
const file_reader = new FileReader();
file_reader.onload = (event) => {
// Convert a PDF-file to EPUB and save the "ResultPDFtoEPUB.epub";

// Call the AsposePdfToEPUB function.
// The name of the resulting file is set, in this example 'ResultPDFtoEPUB.epub'.
const json = AsposePdfToEPUB(event.target.result, e.target.files[0].name, "ResultPDFtoEPUB.epub");

// Message Handling in the Web Worker
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;

// Download File link
DownloadFile(json.fileNameResult, "application/epub+zip");
}
file_reader.readAsArrayBuffer(e.target.files[0]);
}

This code is a JavaScript function that converts a PDF file to an EPUB file using the Aspose.PDF. It creates a FileReader object and assigns a callback function to its onload event. Next, call the AsposePdfToEPUB function. The function takes the file content, the original file name, and the output file name as arguments and returns a JSON object with the result.The JSON object has an errorCode property and an errorText property. If the errorCode is 0, it means that the conversion was successful and the JSON object also has a fileNameResult property, which contains the name of the output EPUB file. If the errorCode is not 0, it means that there was an error and the errorText property contains the error message. As a result, the DownloadFile function creates a download link for the output EPUB file. The function takes the output file name ‘ResultPDFtoEPUB.epub’.

Convert PDF to PNG

Example: JavaScript Code for PDF to PNG conversion

// Select a PDF file for converting.
var ffileToPng = function (e) {

// Create a 'FileReader'.
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;

// Call the AsposePdfPagesToPng  function.
// The name of the resulting file is set, in this example 'ResultPdfToPng{0:D2}.png'.
const json = AsposePdfPagesToPng(event.target.result, e.target.files[0].name, "ResultPdfToPng{0:D2}.png", 150);

// Message Handling in the Web Worker
// Download File link
      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]);
  };

This code is a JavaScript function that converts a PDF file to PNG images using the Aspose.PDF. It creates a FileReader object and assigns a callback function to its onload event. The callback function is executed when the file is read as an array buffer. Calls the AsposePdfPagesToPng function, which is a wrapper for the Aspose.PDF. The function takes the file content, the original file name, the output file name template, and the resolution as arguments and returns a JSON object with the result. The JSON object has an errorCode property and an errorText property. As a result, the DownloadFile function for each output file creates a download link for the PNG image. The function takes the output file name ‘ResultPdfToPng{0:D2}.png’.