We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Add TabStops in the Last Cell of Table Row using .NET

Hi,

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:

 tables.forEach { table ->
            table.preferredWidth = PreferredWidth.fromPoints(
                    (section.pageSetup.pageWidth - section.pageSetup.rightMargin - section.pageSetup.leftMargin) + 15
            )

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.

Looking forward to hear from you.Documents.zip (29.2 KB)

@nobilex

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document (not read only).
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please share the steps that you used to generate the expected output using MS Word.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello,

I have attached input and expected output files with some code base. Could you please guide on how to add tabstops at same position inside table cells(of last column), like I have done in paragraphs?

AsposeTabStops.zip (68.0 KB)

@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)
    {
        table.setAlignment(TableAlignment.LEFT);
        table.setPreferredWidth(PreferredWidth.fromPoints(rightMarginPos));
    }

    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)
            paragraph.appendChild(tab.deepClone(true));
    }
}

doc.save("C:\\Temp\\out.docx");

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.

Thanks for the quick response.
Could you share the code sample to achieve the same? (To set proper cell widths and find tabstop position accordingly)

@nobilex Please see the following 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)
    {
        // Set left table alignment.
        table.setAlignment(TableAlignment.LEFT);
        for (Row r : table.getRows())
        {
            double cellWidth = rightMarginPos / r.getCells().getCount();
            for (Cell c : r.getCells())
                c.getCellFormat().setWidth(cellWidth);
        }
    }

    Iterable<Paragraph> formattedParagraphs = section.getBody().getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph paragraph : formattedParagraphs)
    {
        double tabPosition = rightMarginPos - 15;
        paragraph.getParagraphFormat().getTabStops().add(tabPosition, TabAlignment.RIGHT, TabLeader.LINE);

        Cell parentCell = (Cell)paragraph.getAncestor(NodeType.CELL);
        if (parentCell != null && parentCell.getParentNode().getLastChild() == parentCell)
        {
            double widthOfPrevCells = 0;
            for (Cell c : parentCell.getParentRow().getCells())
            {
                if (c != parentCell)
                    widthOfPrevCells += c.getCellFormat().getWidth();
            }

            paragraph.getParagraphFormat().getTabStops().clear();
            double cellTabPosition = tabPosition - widthOfPrevCells - parentCell.getCellFormat().getLeftPadding();
            paragraph.getParagraphFormat().getTabStops().add(cellTabPosition, TabAlignment.RIGHT, TabLeader.LINE);
        }

        // Add tab for testing.
        if (parentCell == null || parentCell.getParentNode().getLastChild() == parentCell)
        {
            paragraph.appendChild(tab.deepClone(true));
        }
    }
}

doc.save("C:\\Temp\\out.docx");

As you can see tab position in the cell is calculated as

double cellTabPosition = tabPosition - widthOfPrevCells - parentCell.getCellFormat().getLeftPadding();