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

Free Support Forum - aspose.com

Remove Gaps between the table rows

Hi,
The attached document is generated by Aspose, but it has gaps between table rows. I used the following code to remove section breaks, but that doesn’t help me.

for (int i = doc.getSections().getCount() - 2; i >= 0; i–)
{
    System.out.println("doc.getSections().get(i): " + doc.getSections().get(i).getText());
    // Copy the content of the current section to the beginning of the last section.
    doc.getLastSection().prependContent(doc.getSections().get(i));
    // Remove the copied section.
    doc.getSections().get(i).remove();
}

Can you please suggest me the better way of removing these gaps between rows…?

Hi Koteswara,

Thanks for your inquiry. Please use the following code example to achieve your requirements. Following code example does the followings:

  1. Remove section break from the document. See removeSectionBreaks method
  2. After removing the section breaks, remove the empty paragraphs between table.
Document doc = new Document(MyDir + "Invoice+-+Title+V+Emissions+Fee.docx");
removeSectionBreaks(doc);
doc.updatePageLayout();
Node[] tables = doc.getChildNodes(NodeType.TABLE, true).toArray();
ArrayList emptyParagaphs = new ArrayList();
for (int i = 0; i < tables.length - 1; i++)
{
    Node currentNode = tables[i];
    Node nextTable = tables[i + 1];
    while (currentNode != null && currentNode != nextTable)
    {
        Node node = currentNode.getNextSibling();
        System.out.println(node.toString(SaveFormat.TEXT));
        if (node.getNodeType() == NodeType.PARAGRAPH && node.toString(SaveFormat.TEXT).trim().equals(""))
        {
            emptyParagaphs.add(node);
        }
        currentNode = node;
    }
}
for (Node node : (Iterable)emptyParagaphs)
    node.remove();
doc.save(MyDir + "Out.docx");
protected static void removeSectionBreaks(Document doc) throws Exception {
    ArrayList list = new ArrayList();
    Section firstSection = doc.getFirstSection();
    for (Section section : doc.getSections())
    {
        if (section.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS && section != firstSection)
        {
            list.add(section);
            firstSection.appendContent(section);
        }
        else if (section.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE && section != firstSection)
        {
            list.add(section);
            firstSection.appendContent(section);
        }
        else
        {
            firstSection = section;
        }
    }
    for (Section section : (Iterable)list)
    {
        doc.removeChild(section);
    }
}