Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active July 27, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/68b20564599e1c1a70006c664afade61 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/68b20564599e1c1a70006c664afade61 to your computer and use it in GitHub Desktop.
Edit Word Documents using a REST API in Node.js
Edit Word Documents using a REST API in Node.js
1. Edit Word Document using REST API in Node.js
2. Add Table in Word documents using Node.js
3. Insert Image in Word documents using Node.js
// api initialization
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// input file
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// define load options
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// create load request
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// download html document
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// add table
htmlString = htmlString.replace("left-aligned.", "left-aligned. <br/><table style=\"width: 100%;background-color: #dddddd;\">"
+ "<caption style=\"font-weight:bold;\"> Persons List</caption>"
+ "<tr><th>First Name</th><th>Last Name</th><th>Age</th></tr>"
+ "<tr><td>Jill</td><td>Smith</td><td>50</td></tr>"
+ "<tr><td>Eve</td><td>Jackson</td><td>94</td></tr>"
+ "</table>");
// upload html back to storage
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// save html back to docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// create save request
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const configuration = new groupdocs_editor_cloud.Configuration(clientId, clientSecret);
configuration.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
var fileApi = groupdocs_editor_cloud.FileApi.fromConfig(configuration);
// create file download request
let request = new groupdocs_editor_cloud.DownloadFileRequest("output/edited.docx", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save image file in working directory
fs.writeFile("C:\\Files\\edited.docx", response, "binary", function (err) { });
// api initialization
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// input file
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// define load options
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// create load request
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// download html document
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// edit something...
htmlString = htmlString.replace("Title of the document", "Welcome");
htmlString = htmlString.replace("Subtitle #1", "Hello world");
// upload html back to storage
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// save html back to docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// create save request
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
// api initialization
let editApi = groupdocs_editor_cloud.EditApi.fromKeys(clientId, clientSecret);
let fileApi = groupdocs_editor_cloud.FileApi.fromKeys(clientId, clientSecret);
// input file
let fileInfo = new groupdocs_editor_cloud.FileInfo();
fileInfo.filePath = "Sample.docx";
// define load options
let loadOptions = new groupdocs_editor_cloud.WordProcessingLoadOptions();
loadOptions.fileInfo = fileInfo;
loadOptions.outputPath = "output";
// create load request
let loadRequest = new groupdocs_editor_cloud.LoadRequest(loadOptions);
let loadResult = await editApi.load(loadRequest);
// download html document
let downloadRequest = new groupdocs_editor_cloud.DownloadFileRequest(loadResult.htmlPath);
let buf = await fileApi.downloadFile(downloadRequest);
let htmlString = buf.toString("utf-8");
// insert an image
htmlString = htmlString.replace("left-aligned.", "left-aligned. <br/> <img src=\"sample.png\" alt=\"signatures\" style=\"width: 128px; height: 128px;\">");
// upload html back to storage
let uploadRequest = new groupdocs_editor_cloud.UploadFileRequest(loadResult.htmlPath, new Buffer.from(htmlString, "utf-8"));
await fileApi.uploadFile(uploadRequest);
// save html back to docx
let saveOptions = new groupdocs_editor_cloud.WordProcessingSaveOptions();
saveOptions.fileInfo = fileInfo;
saveOptions.outputPath = "output/edited.docx";
saveOptions.htmlPath = loadResult.htmlPath;
saveOptions.resourcesPath = loadResult.resourcesPath;
// create save request
let saveRequest = new groupdocs_editor_cloud.SaveRequest(saveOptions);
let saveResult = await editApi.save(saveRequest);
console.log("Document edited: " + saveResult.path);
// construct FileApi
let fileApi = groupdocs_editor_cloud.FileApi.fromConfig(configuration);
let resourcesFolder = 'C:\\Files\\sample.docx';
fs.readFile(resourcesFolder, (err, fileStream) => {
let request = new groupdocs_editor_cloud.UploadFileRequest("sample.docx", fileStream, myStorage);
fileApi.uploadFile(request);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment