How to adjust paragraph first line indentation in ONLYOFFICE presentations
When preparing presentations, consistency and readability are key. One of the essential formatting features is adjusting the first-line indentation of paragraphs. In this blog post, we’ll guide you through creating an ONLYOFFICE macro that adjusts the first-line indentation for all paragraphs across your presentation.
Building the macro
Set indentation value
The macro starts by defining the amount of indentation for the first line of each paragraph:
const indentationValue = 720;
/* Please enter the paragraph's first line indentation value. A value of 0 means no indentation,
and any value greater than 0 will indent the text. The value is specified in twentieths of a point (1/1440 of an inch).*/
indentationValue: This constant variable defines the first-line indentation in twentieths of a point (1/1440 of an inch), and is set to 720 by default, which corresponds to a half-inch indentation. A value of 0 means no indentation, while any value greater than 0 will indent the text. Users can adjust the indentation value according to their preference.
Retrieving the presentation and number of slides
Before we retrieve current presentation, we ensure the macro executes only when the user’s indentation value is valid non-negative number:
if (!isNaN(indentationValue) && indentationValue >= 0) {
We retrieve current presentation using the GetPresentation() method and total number of slides is obtained with GetSlidesCount() method:
let presentation = Api.GetPresentation();
let nSlides = presentation.GetSlidesCount(); // Get the number of slides in the presentation
Loop through each slide, text box in the slide and paragraph
// Loop through each slide
for (let slideIndex = 0; slideIndex < nSlides; slideIndex++) {
let slide = presentation.GetSlideByIndex(slideIndex); // Get the slide
let aShapes = slide.GetAllShapes(); // Get all shapes in the slide
// Loop through each shape in the slide
for (let shapeIndex = 0; shapeIndex < aShapes.length; shapeIndex++) {
let content = aShapes[shapeIndex].GetDocContent(); // Get the document content of the shape
if (content) {
let count = content.GetElementsCount(); // Get the number of elements (paragraphs) in the shape
// Loop through each paragraph in the shape
for (let elementIndex = 0; elementIndex < count; elementIndex++) {
let paragraph = content.GetElement(elementIndex); // Get the paragraph element
- GetSlideByIndex(slideIndex) accesses the slide object at the specified index (position) in the presentation.
- GetAllShapes() retrieves all shape objects within the current slide. This method returns a collection of all shapes (such as text boxes, images, and other elements) present on the slide.
- GetDocContent() retrieves the document content of a specific shape. This method returns the textual content associated with a shape.
- GetElementsCount() retrieves the total number of elements (paragraphs) within the specified shape.
- GetElement(elementIndex) accesses a specific element (paragraph) within the shape, based on the provided index (elementIndex).
In this section we do the following:
- Loop through all the slides in the presentation.
- Loop through all shapes (text boxes) in the slide.
- Get the text content of the shape.
- Check if the shape has text content.
- Loop through each shape content element (paragraph) in the shape.
- Get the paragraph element.
Adjust paragraph first-line indentation
We set the desired first-line indentation for paragraphs within text boxes by using GetParaPr() to retrieve the paragraph properties and SetIndFirstLine(indentationValue) to adjust the indentation:
let paraPr = paragraph.GetParaPr();
paraPr.SetIndFirstLine(indentationValue);
}
}
}
}
}
Full macro code
The entire macro is the following:
(function () {
const indentationValue = 720;
/* Please enter the paragraph's first line indentation value. A value of 0 means no indentation,
and any value greater than 0 will indent the text. The value is specified in twentieths of a point (1/1440 of an inch).*/
if (!isNaN(indentationValue) && indentationValue >= 0) {
let presentation = Api.GetPresentation();
let nSlides = presentation.GetSlidesCount(); // Get the number of slides in the presentation
// Loop through each slide
for (let slideIndex = 0; slideIndex < nSlides; slideIndex++) {
let slide = presentation.GetSlideByIndex(slideIndex); // Get the slide
let aShapes = slide.GetAllShapes(); // Get all shapes in the slide
// Loop through each shape in the slide
for (let shapeIndex = 0; shapeIndex < aShapes.length; shapeIndex++) {
let content = aShapes[shapeIndex].GetDocContent(); // Get the document content of the shape
if (content) {
let count = content.GetElementsCount(); // Get the number of elements (paragraphs) in the shape
// Loop through each paragraph in the shape
for (let elementIndex = 0; elementIndex < count; elementIndex++) {
let paragraph = content.GetElement(elementIndex); // Get the paragraph element
let paraPr = paragraph.GetParaPr();
paraPr.SetIndFirstLine(indentationValue);
}
}
}
}
}
})();
Let’s run our macro and see how it works!
This macro offers a simple yet effective solution for adjusting the first-line indentation across all paragraphs in your presentation, saving you time and ensuring consistency in your slides. By automating this process, you can focus more on content creation while maintaining a polished and professional layout.
We encourage you to explore our ONLYOFFICE API methods and create your own macros to further enhance your workflow. If you have any ideas or suggestions, feel free to reach out to us—we’d love to hear your feedback!
About the author
Create your free ONLYOFFICE account
View, edit and collaborate on docs, sheets, slides, forms, and PDF files online.