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)
}
}
}
}
@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.
@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,
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.