Some test text!

Search
Hamburger Icon

Convert PDF to Office in JavaScript

More languages

More languages
C++
C#
C# (.NET Core)
Go
Java
Obj-C
JS (Node.js)
PHP
Python
Ruby
VB

Sample JavaScript code for using PDFTron SDK to programmatically convert generic PDF documents to Word, Excel, PowerPoint. Learn more about our JavaScript PDF to Office

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
// The following sample illustrates how to use the PDF::Convert utility class to convert 
// documents and files to Word, Excel and PowerPoint.
//
// The Structured Output module is an optional PDFNet Add-on that can be used to convert PDF
// and other documents into Word, Excel, PowerPoint and HTML format.
//
// The Apryse SDK Structured Output module can be downloaded from
// https://docs.apryse.com/documentation/core/info/modules/
//
// Please contact us if you have any questions.	
//---------------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
	'use strict';

	exports.runPDF2OfficeTest = () => {

		const main = async () => {

			const inputPath = '../TestFiles/';
			const outputPath = '../TestFiles/Output/';

			//////////////////////////////////////////////////////////////////////////

			await PDFNet.addResourceSearchPath('../../lib/');

			if (!await PDFNet.StructuredOutputModule.isModuleAvailable()) {
				console.log('\nUnable to run the sample: Apryse SDK Structured Output module not available.');
				console.log('---------------------------------------------------------------');
				console.log('The Structured Output module is an optional add-on, available for download');
				console.log('at https://docs.apryse.com/documentation/core/info/modules/. If you have already');
				console.log('downloaded this module, ensure that the SDK is able to find the required files');
				console.log('using the PDFNet::AddResourceSearchPath() function.\n');

				return;
			}

			//////////////////////////////////////////////////////////////////////////

			try {
				// Convert PDF document to Word
				console.log('Converting PDF to Word');

				const outputFile = outputPath + 'paragraphs_and_tables.docx';

				await PDFNet.Convert.fileToWord(inputPath + 'paragraphs_and_tables.pdf', outputFile);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			try {
				// Convert PDF document to Word with options
				console.log('Converting PDF to Word with options');

				const outputFile = outputPath + 'paragraphs_and_tables_first_page.docx';

				const wordOutputOptions = new PDFNet.Convert.WordOutputOptions();

				// Convert only the first page
				wordOutputOptions.setPages(1, 1);

				await PDFNet.Convert.fileToWord(inputPath + 'paragraphs_and_tables.pdf', outputFile, wordOutputOptions);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			//////////////////////////////////////////////////////////////////////////

			try {
				// Convert PDF document to Excel
				console.log('Converting PDF to Excel');

				const outputFile = outputPath + 'paragraphs_and_tables.xlsx';

				await PDFNet.Convert.fileToExcel(inputPath + 'paragraphs_and_tables.pdf', outputFile);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			try {
				// Convert PDF document to Excel with options
				console.log('Converting PDF to Excel with options');

				const outputFile = outputPath + 'paragraphs_and_tables_second_page.xlsx';

				const excelOutputOptions = new PDFNet.Convert.ExcelOutputOptions();

				// Convert only the second page
				excelOutputOptions.setPages(2, 2);

				await PDFNet.Convert.fileToExcel(inputPath + 'paragraphs_and_tables.pdf', outputFile, excelOutputOptions);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			//////////////////////////////////////////////////////////////////////////

			try {
				// Convert PDF document to PowerPoint
				console.log('Converting PDF to PowerPoint');

				const outputFile = outputPath + 'paragraphs_and_tables.pptx';

				await PDFNet.Convert.fileToPowerPoint(inputPath + 'paragraphs_and_tables.pdf', outputFile);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			try {
				// Convert PDF document to PowerPoint with options
				console.log('Converting PDF to PowerPoint with options');

				const outputFile = outputPath + 'paragraphs_and_tables_first_page.pptx';

				const powerPointOutputOptions = new PDFNet.Convert.PowerPointOutputOptions();

				// Convert only the first page
				powerPointOutputOptions.setPages(1, 1);

				await PDFNet.Convert.fileToPowerPoint(inputPath + 'paragraphs_and_tables.pdf', outputFile, powerPointOutputOptions);

				console.log('Result saved in ' + outputFile);
			} catch (err) {
				console.log(err);
			}

			//////////////////////////////////////////////////////////////////////////

			console.log('Done.');
		};

		PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
			console.log('Error: ' + JSON.stringify(error));
		}).then(function () { return PDFNet.shutdown(); });
	};
	exports.runPDF2OfficeTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=PDF2OfficeTest.js