I am using List to automatic number the picture with different images.
my code is :
private com.aspose.words.List addImageList(Document document) {
com.aspose.words.List list = document.getLists().add(ListTemplate.OUTLINE_NUMBERS);
ListLevel level1 = list.getListLevels().get(1);
level1.setNumberFormat("图\u0000.\u0001");
level1.setNumberStyle(NumberStyle.ARABIC);
level1.setTabPosition(0);
level1.setTextPosition(0);
level1.setNumberPosition(0);
level1.setTrailingCharacter(ListTrailingCharacter.NOTHING);
level1.setAlignment(ListLevelAlignment.CENTER);
level1.setLinkedStyle(document.getStyles().getByStyleIdentifier(8));
list.isRestartAtEachSection(true);
return list;
}
and i applied it in code
builder.insertParagraph();
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_8);
builder.getListFormat().setList(autoNumberImageList);
builder.getListFormat().setListLevelNumber(1);
builder.write(" ");
however, it restart the second number, but the first number didn’t changed through chapters
so i wish the second number restart from 1, and the first number to increase through section (chapter) , how can i adjust my code ?
also in the above code , i used
level1.setLinkedStyle(document.getStyles().getByStyleIdentifier(8));
and
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_8);
however, in another java class, i used
paragraph.appendField("\\o \"8-8\" \\h \\z \\u");
I thought it could show all the images TOC , but it didn’t. how can i fix this ?
@Madecho As I can see you are changing only the second level formatting. If possible, could you please create a simplified code example that will allow us to reproduce the problem and attach your current and expected output documents.
testClass.zip (2.4 KB)
here is the test code.
test.docx (4.5 MB)
here is the test document.
and the reuslt is
output.docx (4.0 MB)
the toc is so weird
so are those picture titles
@Madecho Could you please also explain what is the expected output?
i wish the first number to increase as the chapter increase , and the second number restart from 1 if a new chapter begins. like in chapter3, the fifth image, should be 图3.5 . rather than 图1.5
@Madecho To achieve this the list items must belong to the same list as used for numbering chapters. But in your code you are creating a new list for it. Here is a simple code that allows to achieve what you need:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Crete list which is used for numbering chapters and titles.
com.aspose.words.List lst = doc.getLists().add(ListTemplate.OUTLINE_NUMBERS);
// For chapter numbering we will use the first level.
lst.getListLevels().get(0).setNumberFormat("Chapter: \u0000");
// For title numbering lets use the 9th level (the last one).
lst.getListLevels().get(8).setNumberFormat("Image: \u0000.\u0008");
lst.getListLevels().get(8).setNumberStyle(NumberStyle.ARABIC);
// numbering on the 9th level will be restarted after the 1st level.
lst.getListLevels().get(8).setRestartAfterLevel(0);
// Now add some content.
for (int i = 0; i < 5; i++)
{
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.getListFormat().setList(lst);
builder.getListFormat().setListLevelNumber(0);
builder.writeln("Some chapter title: " + i);
builder.getParagraphFormat().clearFormatting();
// Add some titles
for (int j = 0; j < 10; j++)
{
builder.getListFormat().setList(lst);
builder.getListFormat().setListLevelNumber(8);
builder.writeln("Title: " + j);
}
builder.getListFormat().removeNumbers();
}
doc.save("C:\\Temp\\out.docx");
out.docx (8.2 KB)
thank you very very much @alexey.noskov , it works really well !!!
another question pops up, it’s how to change the paragraphformat and font of a certain field.
for example :
Paragraph paragraph = new Paragraph(doc);
paragraphCollection.add(paragraph);
paragraph.appendField("TOC \\o \"8-8\" \\h \\z \\u");
since "TOC \\o \"8-8\" \\h \\z \\u"
will show lots of paragraph with font.
how can i change it to certain style that i want. totally custom style
i tried with
StyleUtils.merge(paragraph.getParagraphFormat(), styleConfigDto.getText());
public static void merge(ParagraphFormat paragraphFormat, StyleTemplateDto styleTemplateDto)
{
StyleTemplateDto.ParagraphFormat newParagraphFormat = styleTemplateDto.getParagraphFormat();
paragraphFormat.setAlignment(newParagraphFormat.getAlignment());
Indent indent = newParagraphFormat.getIndent();
Indent.Config indentLeft = indent.getLeft();
Indent.Config indentFirstLine = indent.getFirstLine();
if (indentFirstLine.getUnit() == IndentUnitEnum.CHARACTER)
{
paragraphFormat.setFirstLineIndent(0);
paragraphFormat.setCharacterUnitFirstLineIndent(indentFirstLine.getValue());
}
else
{
paragraphFormat.setFirstLineIndent(
indentFirstLine.getUnit().converter.apply(indentFirstLine.getValue())
);
paragraphFormat.setCharacterUnitFirstLineIndent(0);
}
// 这两会同时生效,所以要先清空
if (indentLeft.getUnit() == IndentUnitEnum.CHARACTER)
{
paragraphFormat.setLeftIndent(0);
paragraphFormat.setCharacterUnitLeftIndent(indentLeft.getValue());
}
else
{
paragraphFormat.setLeftIndent(
indentLeft.getUnit().converter.apply(indentLeft.getValue())
);
paragraphFormat.setCharacterUnitLeftIndent(0);
}
Indent.Config indentRight = indent.getRight();
if (indentRight.getUnit() == IndentUnitEnum.CHARACTER)
{
paragraphFormat.setRightIndent(0);
paragraphFormat.setCharacterUnitRightIndent(indentRight.getValue());
}
else
{
paragraphFormat.setRightIndent(
indentRight.getUnit().converter.apply(indentRight.getValue())
);
paragraphFormat.setCharacterUnitRightIndent(0);
}
Spacing spacing = newParagraphFormat.getSpacing();
Spacing.Config spacingBefore = spacing.getBefore();
if (spacingBefore.getUnit() == SpacingUnitEnum.AUTO)
{
paragraphFormat.setSpaceBeforeAuto(true);
}
else
{
paragraphFormat.setSpaceBeforeAuto(false);
paragraphFormat.setSpaceBefore(
spacingBefore.getUnit().converter.apply(spacingBefore.getValue())
);
}
Spacing.Config spacingAfter = spacing.getAfter();
if (spacingAfter.getUnit() == SpacingUnitEnum.AUTO)
{
paragraphFormat.setSpaceAfterAuto(true);
}
else
{
paragraphFormat.setSpaceAfterAuto(false);
paragraphFormat.setSpaceAfter(
spacingAfter.getUnit().converter.apply(spacingAfter.getValue())
);
}
Spacing.Config line = spacing.getLine();
paragraphFormat.setLineSpacingRule(line.getType().getValue());
paragraphFormat.setLineSpacing(
line.getUnit().converter.apply(line.getValue())
);
}
but it didn’t work. how should i fix this ?
@Madecho Actually, to modify the table of contents font, you only need to modify the TOC1-TOC9 styles. Please try using the following code:
int[] tocStyles = new int[]
{
StyleIdentifier.TOC_1,
StyleIdentifier.TOC_2,
StyleIdentifier.TOC_3,
StyleIdentifier.TOC_4,
StyleIdentifier.TOC_5,
StyleIdentifier.TOC_6,
StyleIdentifier.TOC_7,
StyleIdentifier.TOC_8,
StyleIdentifier.TOC_9
};
Document doc = new Document("C:\\Temp\\in.docx");
for (int toc : tocStyles)
{
Style s = doc.getStyles().getByStyleIdentifier(toc);
s.getFont().setNameAscii("Arial");
s.getFont().setSize(10.5);
s.getFont().setBold(false);
s.getFont().setItalic(false);
}
doc.save("C:\\Temp\\out.docx");
1 Like