Hello Aspose Support Team,
I am currently working with Aspose.Words for Java, and I’m facing an issue when using the setRestartAfterLevel()
method on a list level. The problem is that the numbering of the list is continuing instead of restarting as expected.
Here is a summary of my use case:
- I have a multi-level list, and I want the numbering to restart at a specific level.
- I am using the
setRestartAfterLevel()
method to specify that numbering should restart after a certain level. - However, the numbers continue from the previous level, even though I have correctly applied the
setRestartAfterLevel()
method.
private void setRestartAfterLevelProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getLevelRestartAt())
.ifPresent(documentListLevel::setRestartAfterLevel);
}
I am expecting the list to restart numbering at the specified level, but instead, the numbering continues.
Things I have already tried:
- Ensuring that the list levels are correctly defined.
- Double-checking that the
stylingInstructionsLevel.getLevelRestartAt()
is returning a valid level. - Calling
setRestartAfterLevel()
after setting other list properties.
Could you please help me understand what might be causing this issue? Is there anything specific I need to do to make the restart behavior work correctly?
THIS WAS THE FULL CODE SNIPPET:
package com.trgr.ras.cd.firmstylewordgenerator.service.processors;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import com.aspose.words.*;
import com.aspose.words.Font;
import com.aspose.words.List;
import com.trgr.ras.cd.firmstylewordgenerator.entity.Lists;
import com.trgr.ras.cd.firmstylewordgenerator.entity.StylingInstructions;
import com.trgr.ras.cd.firmstylewordgenerator.service.AsposeDocumentHelper;
import java.awt.*;
import java.util.Optional;
import lombok.CustomLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
@Service
@CustomLog
public class ListProcessor extends StyleProcessor {
@Override
public void process(Document document, StylingInstructions stylingInstructions) {
if (stylingInstructions == null || isEmpty(stylingInstructions.getLists())) {
return;
}
try {
stylingInstructions
.getLists()
.forEach(list -> applyStylingInstructionsForLevels(document, list));
} catch (Exception e) {
log.error("Error processing lists styling info", e);
}
}
private void applyStylingInstructionsForLevels(Document document, Lists stylingInstructionsList) {
var newList = document.getLists().add(ListTemplate.NUMBER_DEFAULT);
for (int i = 0; i < stylingInstructionsList.getLevels().size(); i++) {
Lists.Level stylingInstructionsLevel = stylingInstructionsList.getLevels().get(i);
var documentListLevel = newList.getListLevels().get(i);
setStartAtProperty(stylingInstructionsLevel, documentListLevel);
setRestartAfterLevelProperty(stylingInstructionsLevel, documentListLevel);
setNumberStyleProperty(stylingInstructionsLevel, documentListLevel);
setNumberFormatProperty(stylingInstructionsLevel, documentListLevel);
setAlignmentProperty(stylingInstructionsLevel, documentListLevel);
setTrailingCharacterProperty(stylingInstructionsLevel, documentListLevel);
setParagraphProperty(stylingInstructionsLevel, documentListLevel);
setFontProperty(stylingInstructionsLevel, documentListLevel, document);
setLinkedStyle(stylingInstructionsLevel, newList, documentListLevel, i, document);
}
}
private void setLinkedStyle(
Lists.Level stylingInstructionsLevel,
List newList,
ListLevel documentListLevel,
int listLevelNumber,
Document document) {
Optional.ofNullable(stylingInstructionsLevel.getStyle())
.filter(StringUtils::isNotBlank)
.ifPresent(
(styleName) -> {
var linkedStyle = document.getStyles().get(styleName);
linkedStyle.getListFormat().removeNumbers();
documentListLevel.setLinkedStyle(linkedStyle);
// When a style is added to a list in ASPOSE, the list levels indent level is added to
// the style left indent. this isn't consistent with our styling instructions, so we
// need to save and restore the original left indent, provided there is one set (>0.0)
var originalLeftIndent = linkedStyle.getParagraphFormat().getLeftIndent();
linkedStyle.getListFormat().setList(newList);
if (originalLeftIndent != 0.0) {
linkedStyle.getParagraphFormat().setLeftIndent(originalLeftIndent);
}
linkedStyle.getListFormat().setListLevelNumber(listLevelNumber);
});
}
private void setTrailingCharacterProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getTrailingCharacter())
.filter(StringUtils::isNotBlank)
.ifPresentOrElse(
character ->
documentListLevel.setTrailingCharacter(ListTrailingCharacter.fromName(character)),
() -> documentListLevel.setTrailingCharacter(ListTrailingCharacter.TAB));
}
private void setAlignmentProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getAlignment())
.filter(StringUtils::isNotBlank)
.ifPresent(
alignment -> documentListLevel.setAlignment(ListLevelAlignment.fromName(alignment)));
}
private void setNumberFormatProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getNumberFormat())
.ifPresent(
(formatString) -> {
documentListLevel.setNumberFormat(processNumberFormat(formatString));
});
}
private String processNumberFormat(String input) {
var percentLoc = input.indexOf("%");
while (percentLoc >= 0) {
var listLevel = Integer.parseInt(input.substring(percentLoc + 1, percentLoc + 2));
var placeholderChar = String.valueOf((char) (listLevel - 1));
input = input.substring(0, percentLoc) + placeholderChar + input.substring(percentLoc + 2);
percentLoc = input.indexOf("%");
}
return input;
}
private void setNumberStyleProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getNumberStyle())
.filter(StringUtils::isNotBlank)
.ifPresent(
style -> {
documentListLevel.setNumberStyle(NumberStyle.fromName(style));
});
}
private void setRestartAfterLevelProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getLevelRestartAt())
.ifPresent(documentListLevel::setRestartAfterLevel);
}
private void setStartAtProperty(
Lists.Level stylingInstructionsLevel, ListLevel documentListLevel) {
Optional.ofNullable(stylingInstructionsLevel.getStartAt())
.ifPresent(documentListLevel::setStartAt);
}
private void setParagraphProperty(Lists.Level level, ListLevel listLevel) {
Optional.ofNullable(level.getParagraph())
.ifPresent(
paragraph -> {
Optional.ofNullable(paragraph.getAlignment())
.ifPresent(
alignment ->
listLevel.setAlignment(ListLevelAlignment.fromName(alignment.name())));
Optional.ofNullable(paragraph.getTabPosition())
.ifPresentOrElse(
tabPosition -> listLevel.setTabPosition(ConvertUtil.inchToPoint(tabPosition)),
() -> listLevel.setTabPosition(0));
Optional.ofNullable(paragraph.getLeftIndent())
.ifPresent(
leftIndent -> listLevel.setTextPosition(ConvertUtil.inchToPoint(leftIndent)));
// compute number position
double numberPositionInches = 0;
if (paragraph.getLeftIndent() != null) {
numberPositionInches += paragraph.getLeftIndent();
}
if (paragraph.getFirstLineIndent() != null) {
numberPositionInches += paragraph.getFirstLineIndent();
} else if (paragraph.getHangingIndent() != null) {
numberPositionInches += paragraph.getHangingIndent();
}
listLevel.setNumberPosition(ConvertUtil.inchToPoint(numberPositionInches));
});
}
private void setFontProperty(Lists.Level level, ListLevel listLevel, Document document) {
if (level.getFont() == null) {
Optional.ofNullable(level.getStyle())
.map(styleName -> document.getStyles().get(styleName))
.map(style -> style.getFont())
.ifPresent(
srcFont -> {
Font destinationFont = listLevel.getFont();
AsposeDocumentHelper.copyFontData(srcFont, destinationFont);
});
} else {
Optional.ofNullable(level.getFont()).ifPresent(font -> updateFont(listLevel.getFont(), font));
}
}
}