Need to remove all spacing and padding between table rows & inner tables from word document

I am using aspose-words:21.2:jdk17. I am removing the padding from the table and all the cells. However the cells having single-line text, still have small padding applied which causes inconsistent padding in the document.

Below is the code and input-output documents.

@Test
fun `Remove table padding from source document`() {
    val inputDoc = resourceAsStream("input-doc.docx")
    val document = Document(inputDoc)
    document.getChildNodes(NodeType.TABLE, true).toMutableList().forEach { removeTablePadding(it as Table) }
    val outputDocName = "output-doc.docx"
    ByteArrayOutputStream()
            .use {
                document.save(outputDocName, it)
                val actual = File("documents", outputDocName)
                actual.parentFile.mkdirs()
                FileCopyUtils.copy(it.toByteArray(), actual)
            }
}
private fun removeTablePadding(table: Table) {
    table.allowCellSpacing = false
    table.cellSpacing = 0.0
    table.bottomPadding = 0.0
    table.topPadding = 0.0
    table.rows.forEach { row ->
        row.cells.forEach { cell ->
            cell.cellFormat.topPadding = 0.0
            cell.cellFormat.bottomPadding = 0.0
            // Recursively remove cell padding for nested tables
            cell.getChildNodes(NodeType.TABLE, true).toMutableList().forEach {
                removeTablePadding(it as Table)
            }
        }
    }
}

Attachments:
input-doc.docx (11.0 KB)
input-doc-ss.png (47.9 KB)
output-doc.docx (12.2 KB)
output-doc-ss.png (51.9 KB)

@nobilex Could you please attach your input and output documents in DOCX format here for testing? We will check the issue and provide you more information.

Added .docx files (In the original post).

@nobilex Thank you for additional information. The problem is not in paddings, the problem is in the tables structure. If you enable borders for all tables in the document, you will see that tables are different and that is why text is shifted differently:

Yes, the records are individual tables, and so need to remove the space between inner tables, However if the text is of single line in any of the table, issue persist. Otherwise the extra vertical space between the table gets removed.
You can check the other input file,

input-doc-1.docx (11.0 KB)
output-doc-1.docx (12.3 KB)


@nobilex Unfortunately, I cannot see the problem on my side after using the following code:

Document doc = new Document("C:\\Temp\\in.docx");

Iterable<Table> tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table t : tables)
{
    for (Row r : t.getRows())
    {
        r.getRowFormat().getBorders().setLineStyle(LineStyle.NONE);
        r.getRowFormat().getBorders().setLineWidth(0);
        for (Cell c : r.getCells())
        {
            c.getCellFormat().setBottomPadding(0);
            c.getCellFormat().setTopPadding(0);
        }
    }
}

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

can you share a pdf file? I need to check the file in a different PDF viewer, as all PDF viewers don’t show the spacing properly on the text selection, like Chrome/Document Viewer. i.e. In Master PDF Editor it can be verified. Also I tried with your given code but still the issue persists.

@nobilex Sure, here is the output PDF produced on my side: out.pdf (33.8 KB)

It looks like there is no problem in Aspose.Words, but there are specific of different PDF editors. I have used Acrobat Reader on my side.