I need to add TabStops(a bit beyond the right margin of the document) in document and position of TabStops should be consistent throughout the document.
In case of paragraphs, I add tabstops at position: (section.pageSetup.pageWidth - section.pageSetup.rightMargin - section.pageSetup.leftMargin) + 15
In case of tables also, tabstops should be added at this same position as paragraphs. For that, width of the tables or the width of the last column of tables should be increased.
For that, I am changing preferred width of all tables by:
However, tabstops get added beyond, making inconsistency compared with paragraphs.
So the solution could be to make width of all tables to 100% and then increase it by 15 pts to match up with paragraphs.
Could you please help out with what is wrong with the applied preferred width in tables and how it can be achieved to match up with paragraphs.
Please note: Here tables includes nested tables as well.
I have attached input and output document for reference. Output document includes the tabstops.
@nobilex Thank you for additional information. The problem here is how MS Word counts the position of tab in regular paragraph and in the table cell. For example see the following simple code:
Document doc = new Document("C:\\Temp\\in.docx");
// Create run with tab for testing.
Run tab = new Run(doc, "\t");
for (Section section : doc.getSections())
PageSetup ps = section.getPageSetup();
Double rightMarginPos = ps.getPageWidth() - ps.getRightMargin() - ps.getLeftMargin();
Iterable<Table> tables = section.getBody().getChildNodes(NodeType.TABLE, true);
for (Table table : tables)
Iterable<Paragraph> formattedParagraphs = section.getBody().getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph paragraph : formattedParagraphs)
paragraph.getParagraphFormat().getTabStops().add(rightMarginPos / 2, TabAlignment.RIGHT, TabLeader.LINE);
Node parentCell = paragraph.getAncestor(NodeType.CELL);
// Add tab for testing.
if (parentCell == null || parentCell.getParentNode().getLastChild() == parentCell)
The code is supposed to add a tabstop at the meddle of the page and it does for regular paragraph. See the output document: out.docx (15.3 KB)
But for cell the start position for tabstop is at the beginning of the cell. So to make the tabstops on the same position you should know the position of the cell (distance from the left side of page). So to achieve what you need, you should explicitly set width of cells before the last cell in row, to be able to calculate the start position.