List indents in cell problem

Hello! I have a table cell with list in it, but indents are very big after exporting to Word. How to make them smaller? Like green line on the picture.

I`m doing like this:

List list = paragraph.getListFormat().getList();
list.getListLevels().forEach(level -> {
    level.setTextPosition(50);
    level.setNumberPosition(20);
    level.setTabPosition(10);
});

But there is no effect at all

@cagecrew Could you please attach your input and expected output documents here for our reference? We will check your documents and provide you more information.

Of course
Table:

<table style="border-collapse: collapse; width: 100%;" border="1">
	<colgroup>
		<col style="width: 17.0311%;">
			<col style="width: 53.1599%;">
				<col style="width: 11.7423%;">
					<col style="width: 18.0667%;">
					</colgroup>\n
					<tbody>\n
						<tr>\n
							<td>Text</td>\n
							<td>Text</td>\n
							<td>Text</td>\n
							<td>Text</td>\n
						</tr>\n
						<tr>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
							<td>\n
								<ol>\n
									<li>
										<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>\n
										<ol>\n
											<li>
												<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>\n
												<ol>\n
													<li>
														<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>\n
														<ol>\n
															<li>
																<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>\n
																<ol>\n
																	<li>
																		<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>\n
																		<ol>\n
																			<li>
																				<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque</span>\n
																				<ol>\n
																					<li>
																						<span>43Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l&nbsp;</span>
																					</li>\n
																				</ol>\n
																			</li>\n
																		</ol>\n
																	</li>\n
																</ol>\n
															</li>\n
														</ol>\n
													</li>\n
												</ol>\n
											</li>\n
										</ol>\n
									</li>\n
								</ol>\n
							</td>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
						</tr>\n
						<tr>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
							<td>
								<span>33Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque l</span>
							</td>\n
						</tr>\n
					</tbody>\n
				</table>

Exported document with big list indents:
Exported.docx (16,2 КБ)
It is also not possible to edit indents in this exported document.

Expected small indents:

@cagecrew Please try using the following code:

Document doc = new Document("C:\\Temp\\in.docx");
for (Paragraph p : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (p.isListItem())
    {
        p.getParagraphFormat().setLeftIndent(p.getParagraphFormat().getLeftIndent() / 2);
        p.getParagraphFormat().setFirstLineIndent(p.getParagraphFormat().getFirstLineIndent() / 2);
    }
}
doc.save("C:\\Temp\\out.docx");
1 Like

It is works, thank you!
And now how to make this space smaller?

And for some reason it is different at each level

@cagecrew The simplest way is to use whitespace as the list trailing character:

p.getListFormat().getListLevel().setTrailingCharacter(ListTrailingCharacter.SPACE);
1 Like

Thank you! And probably the last question on this topic

How to remove this empty space?

@cagecrew Actually I d not see this space in the document you have attached earlier. But from what I see on the screenshot, it an be caused by explicitly set row height and setting cell content vertical position to center. Please try using the following code:

Document doc = new Document("C:\\Temp\\in.docx");
for (Row r : (Iterable<Row>)doc.getChildNodes(NodeType.ROW, true))
{
    r.getRowFormat().setHeight(20);
    r.getRowFormat().setHeightRule(HeightRule.AT_LEAST);
    for (Cell c : r.getCells())
        c.getCellFormat().setVerticalAlignment(CellVerticalAlignment.TOP);
}
doc.save("C:\\Temp\\out.docx");
1 Like

Hello, found another problem.
Some items have an indentation on the first line, and some have the opposite. How do I make everything look like where I marked in green?

For some reason, in every third paragraph, the first line has a smaller indentation.

And when I make this (put 4 instead of 2 as you advised me above):

p.getParagraphFormat().setFirstLineIndent(p.getParagraphFormat().getFirstLineIndent() / 4);
p.getParagraphFormat().setLeftIndent(p.getParagraphFormat().getLeftIndent() / 4);

It starts looking like this:

@cagecrew The problem here is tab stops. Since there are multiple indented levels levels tab for some levels the same tab stop is used:


As you can see the same tab stop is used for 3 levels if the list. Partially this can be resolved by using ListTrailingCharacter.SPACE as mentioned above. In this case space character is used instead of tab and tab stop position does not affect text position. but in this case it is impossible to lineup list item content with the first line of list item content.
The following code gives a bit better rresult:

Document doc = new Document("C:\\Temp\\in.docx");
for (Paragraph p : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (p.isListItem())
    {
        p.getListFormat().getListLevel().setAlignment(ListLevelAlignment.LEFT);
        p.getListFormat().getListLevel().setTrailingCharacter(ListTrailingCharacter.SPACE);
        p.getParagraphFormat().setLeftIndent(p.getParagraphFormat().getLeftIndent() / 4);
        p.getParagraphFormat().setFirstLineIndent(-12);
    }
}
doc.save("C:\\Temp\\out.docx");
1 Like

Yes, much better!
Thank you very much!

1 Like