How to integrate DocIO functionality in Node.js using ASP.NET Core Web API?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can integrate DocIO functionality in Node.js using ASP.NET Core Web API using C#.
Step 1: Steps to launch a Core Web API
- Create a new ASP.NET Core Web API project.
2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
- Create a file named DocIOController.cs inside the Controllers folder and include the following namespace.
C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
- Use the following code example to perform Open and save Word document.
C#
public IActionResult OpenAndResave(IFormFile file)
{
if (file == null || file.Length == 0)
return BadRequest("Invalid file uploaded");
try
{
using (Stream inputStream = file.OpenReadStream())
{
// Open the Word document
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
{
using (MemoryStream memoryStream = new MemoryStream())
{
// Save the document to the memory stream
document.Save(memoryStream, FormatType.Docx);
// Reset the stream position to the beginning
memoryStream.Position = 0;
// Convert MemoryStream to Byte Array
byte[] fileBytes = memoryStream.ToArray();
// Return the file as a downloadable response
return File(memoryStream.ToArray(),
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"ResavedDocument.docx");
}
}
}
}
catch (Exception ex)
{
return StatusCode(500, $"Error processing document: {ex.Message}");
}
}
- Build and Run the Web API in Visual Studio
- Rebuild and run the application in Visual Studio.
- The API will start on localhost, e.g., http://localhost:5083.
- Keep the Web API Running
- Do not close the terminal; this API should run when using Node.js.
Step 2: Create Node.js Sample
- Create a folder named “CSharpToNodeJS” and create a new JavaScript file in the folder named Sample.js.
- Add the following code in Sample.js, in which input Word documents are sent to the Web API as form data, and the Web API saves the resultant Word document in the mentioned location.
Add the Web API service URL along with the complete route of the API mentioned in Node.js.
const fs = require('fs');
const FormData = require('form-data');
const axios = require('axios');
const https = require('https');
const filePath1 = "Input.docx"; // Replace with the actual file path
const fileData1 = fs.readFileSync(filePath1);
const formData = new FormData();
formData.append('file', fileData1, {
filename: 'Input.docx',
contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
});
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
axios.post('http://localhost:5083/api/docio/OpenAndResave', formData, {
headers: formData.getHeaders(),
responseType: 'arraybuffer', // Ensure response is treated as a binary file
httpsAgent,
})
.then(response => {
console.log('File successfully processed');
fs.writeFileSync('ResavedDocument.docx', response.data);
})
.catch(error => {
console.error('Error:', error.message);
});
- Add a package.json file and input the document inside the CSharpToNodeJS folder.
- Run the following command in the command prompt to install the required Node.js packages.
npm install
Now the CSharpToNodeJS folder will show the downloaded packages.
- Use the following command to run the Node.js script and also ensure the Web API is running before executing the script.
node Sample.js
The output Word document will be saved inside the Node.js folder.
You can download a complete working sample to integrate DocIO functionality in Node.js using ASP.NET Core Web API from GitHub.
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to integrate DocIO functionality in Node.js using ASP.NET Core Web API in a .NET Core Word document.
You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!