Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse Word Documents using a REST API in Node.js
Parse Word Documents using a REST API in Node.js
1. Parse Word Documents and Extract Images
2. Parse Word Documents and Extract Text
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const configuration = new groupdocs_parser_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
var fileApi = groupdocs_parser_cloud.FileApi.fromConfig(configuration);
// create file download request
let request = new groupdocs_parser_cloud.DownloadFileRequest("parser/images/sample_docx/image_0.png", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save image file in working directory
fs.writeFile("C:\\Files\\images\\image_0.png", response, "binary", function (err) { });
console.log(response);
// api initialization
let parseApi = groupdocs_parser_cloud.ParseApi.fromConfig(configuration);
// sample file info
let fileInfo = new groupdocs_parser_cloud.FileInfo();
fileInfo.filePath = "sample.docx";
// define image options
let options = new groupdocs_parser_cloud.ImagesOptions();
options.fileInfo = fileInfo;
// create image request
let request = new groupdocs_parser_cloud.ImagesRequest(options);
// get images
let result = await parseApi.images(request);
// show images download path
let images = result.images;
images.forEach(image => {
console.log("Image path in storage: " + image.path);
console.log("Download url: " + image.downloadUrl);
console.log("Image format: " + image.fileFormat + ". Page index: " + image.pageIndex);
});
// api initialization
let parseApi = groupdocs_parser_cloud.ParseApi.fromConfig(configuration);
// sample file info
let fileInfo = new groupdocs_parser_cloud.FileInfo();
fileInfo.filePath = "sample.docx";
// define text options
let options = new groupdocs_parser_cloud.TextOptions();
options.fileInfo = fileInfo;
options.startPageNumber = 0;
options.countPagesToExtract = 1;
options.formattedTextOptions = new groupdocs_parser_cloud.FormattedTextOptions({
mode: "Html" // extract text in HTML
});
// create text request
let request = new groupdocs_parser_cloud.TextRequest(options);
// get text
let result = await parseApi.text(request);
// show extracted text
let pages = result.pages;
pages.forEach(page => console.log(page.text));
// initialize API
let fileApi = groupdocs_parser_cloud.FileApi.fromConfig(configuration);
let resourcesFolder = 'C:\\Files\\sample.docx';
// read file
fs.readFile(resourcesFolder, (err, fileStream) => {
// create file upload request
let request = new groupdocs_parser_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
// upload file
fileApi.uploadFile(request);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment