Converting the layout of a Word document is a frequent need when generating reports, contracts, or printable brochures. GroupDocs.Merger Cloud SDK for Node.js enables developers to programmatically convert Word page Orientation in Node.JS with just a few lines of code. This guide walks you through the required steps, shows a complete working example, demonstrates equivalent cURL calls, and explains installation and configuration so you can integrate page orientation changes into your Node.js applications.
Steps to Convert Word Page Orientation in Node.JS
Install the SDK and configure credentials: Use npm to add the library and set up your client ID and secret.
npm install groupdocs-merger-cloudCreate the API instance: Initialize
ChangePageOrientationApiwith the configuration object.const GroupDocsMergerCloud = require('groupdocs-merger-cloud'); const clientId = 'YOUR_CLIENT_ID'; const clientSecret = 'YOUR_CLIENT_SECRET'; const config = new GroupDocsMergerCloud.Configuration(clientId, clientSecret); const orientationApi = new GroupDocsMergerCloud.ChangePageOrientationApi(config);Build the request options: Define the source file, page range, desired orientation, and output path.
const options = new GroupDocsMergerCloud.ChangePageOrientationOptions(); options.fileInfo = new GroupDocsMergerCloud.FileInfo(); options.fileInfo.filePath = 'input.docx'; options.pages = ['1-']; // all pages options.orientation = GroupDocsMergerCloud.Orientation.Landscape; // or Portrait options.outputPath = 'output.docx';Execute the orientation change: Send the request and handle the response.
const request = new GroupDocsMergerCloud.ChangePageOrientationRequest(options); orientationApi.changePageOrientation(request) .then(response => { console.log('Page orientation conversion completed.'); console.log('Output stored at:', response.path); }) .catch(error => { console.error('Error during orientation conversion:', error); });(Optional) Adjust additional settings: You can modify the
pagesarray to target specific pages or switch theorientationenum toPortraitfor a different layout.
Word Page Orientation Change - Complete Code Example
The following example demonstrates the full workflow for changing the orientation of a DOCX file using the GroupDocs.Merger Cloud SDK for Node.js.
const GroupDocsMergerCloud = require('groupdocs-merger-cloud');
// ==== Configuration ====
// Replace with your actual credentials
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const config = new GroupDocsMergerCloud.Configuration(clientId, clientSecret);
// ==== API Instance ====
const orientationApi = new GroupDocsMergerCloud.ChangePageOrientationApi(config);
// ==== Request Construction ====
const options = new GroupDocsMergerCloud.ChangePageOrientationOptions();
options.fileInfo = new GroupDocsMergerCloud.FileInfo();
options.fileInfo.filePath = 'input.docx'; // source Word document
options.pages = ['1-']; // apply to all pages (1 to end)
options.orientation = GroupDocsMergerCloud.Orientation.Landscape; // Portrait or Landscape
options.outputPath = 'output.docx'; // destination file
const request = new GroupDocsMergerCloud.ChangePageOrientationRequest(options);
// ==== Execution ====
orientationApi.changePageOrientation(request)
.then(response => {
console.log('Page orientation conversion completed.');
console.log('Output stored at:', response.path);
})
.catch(error => {
console.error('Error during orientation conversion:', error);
});
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.docx,output.docx) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Changing Page Orientation via REST API with cURL
If you prefer a pure HTTP approach, the same operation can be performed with cURL commands against the GroupDocs.Merger Cloud REST endpoints.
Obtain an access token
curl -X POST "https://api.groupdocs.cloud/v1.0/oauth2/token" \ -H "Content-Type: application/json" \ -d '{"client_id":"YOUR_CLIENT_ID","client_secret":"YOUR_CLIENT_SECRET"}'The response contains
access_token.Upload the source DOCX file
curl -X POST "https://api.groupdocs.cloud/v1.0/merger/storage/upload" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -F "file=@input.docx" \ -F "path=/input.docx"Request orientation change
curl -X POST "https://api.groupdocs.cloud/v1.0/merger/words/changePageOrientation" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -H "Content-Type: application/json" \ -d '{ "fileInfo": { "filePath": "/input.docx" }, "pages": ["1-"], "orientation": "Landscape", "outputPath": "/output.docx" }'Download the resulting file
curl -X GET "https://api.groupdocs.cloud/v1.0/merger/storage/download?path=/output.docx" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o output.docx
For more details on request payloads and supported parameters, see the official API documentation.
Installing and Configuring GroupDocs.Merger Cloud SDK for Node.js
To get started, install the package from npm and configure your credentials.
npm install groupdocs-merger-cloud
const GroupDocsMergerCloud = require('groupdocs-merger-cloud');
const clientId = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const config = new GroupDocsMergerCloud.Configuration(clientId, clientSecret);
Download the latest library from the release page if you need a specific version. Ensure your environment runs Node.js 12 or higher and that you have a valid GroupDocs Cloud account.
Configuring Page Orientation Options
The SDK exposes several properties you can tweak:
- pages - Define which pages to affect (e.g.,
['1-5']for the first five pages).options.pages = ['1-5']; - orientation - Choose
GroupDocsMergerCloud.Orientation.PortraitorLandscape.options.orientation = GroupDocsMergerCloud.Orientation.Portrait; - outputPath - Specify the destination path in your cloud storage.
options.outputPath = 'converted/output.docx';
These options are part of the ChangePageOrientationOptions class documented in the API reference.
Conclusion
Changing the layout of Word files programmatically is straightforward with the GroupDocs.Merger Cloud SDK for Node.js. By following the steps above, you can convert Word page Orientation in Node.JS, automate bulk processing, and integrate orientation changes into any document workflow. Remember to secure a proper license for production use; pricing details are available on the product page, and you can obtain a temporary license from the temporary license page. With the SDK installed and configured, you’re ready to enhance your applications with flexible document manipulation capabilities.
FAQs
How do I change page orientation in Word documents using Node.js?
Use theChangePageOrientationApifrom the GroupDocs.Merger Cloud SDK for Node.js, set the desiredorientationin the request options, and execute the API call.Is there a way to export Word page Orientation in Node.JS without writing code?
Yes, the same operation can be performed via the REST API using cURL commands, which is useful for quick scripts or integration with other systems.What orientations are supported for Word documents?
The SDK supports bothPortraitandLandscapemodes through theOrientationenum. You can also specify page ranges to apply the change selectively.Do I need a license to run this in production?
A valid license is required for production deployments. You can start with a temporary license from the temporary license page and upgrade to a full license as your usage grows.
