Cell height of table document is increate

  1. I am filling data in template documents, after filling the data in document, cell height is increasing.
  2. we need to hide first column also, currently we are setting first column font 1. but user is able to see “.” , we want to hide that for that we are trying to set color of the cell/column to back ground color. but we can not find the method to do that. please suggest.

I am attachinging template.doc and templateGen.doc(generate after filling data) and attaching code also.

TemplateGen.docx (23.1 KB)

Template.docx (29.3 KB)

package com.sirionlabs.api;

import com.aspose.words.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sirionlabs.util.JsonUtils;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main2 {
    public static void main(String[] args) throws Exception {
        License wordLicense = new License();
        wordLicense.setLicense(new FileInputStream("/home/mohitkumar/IdeaProjects/contract-authoring/auto-tagging/target/test-classes/aspose-licence"));

        Document document = new Document("/home/mohitkumar/Downloads/OldTemplate.docx");
        //Code to convert cc to bookmark

        NodeCollection childNodes = document.getChildNodes(NodeType.TABLE, true);
        for (Object childNode : childNodes) {
            Table targetTable = (Table) childNode;
            String title = targetTable.getTitle();
            if (StringUtils.isEmpty(title)) {
                continue;
            }

            String[] split = title.split("_");
            String description = targetTable.getDescription();
            if (StringUtil.isEmpty(description)) {
                continue;
            }

            if (split.length > 1 && "LineItem".equals(split[0])) {
                Map<String, String> orderToKey = new ObjectMapper().readValue(description, HashMap.class);
                String tableFilterQuery = "";
                Integer templateId = Integer.parseInt(split[1]);
                //orderToKey.put("0", "rowNum");
                targetTable.setDescription(JsonUtils.toJson(orderToKey));
                if (orderToKey.get("filterQuery") != null) {
                    tableFilterQuery = orderToKey.get("filterQuery");
                    orderToKey.remove("filterQuery");
                }

                if (orderToKey.get("tableName") != null) {
                    orderToKey.remove("tableName");
                }

                List<Map<String, Object>> data = JsonUtils.fromJson("[{\"rowNum\":1,\"Site Id\":\"TestSiteId1\",\"Site Address\":\"TestSiteAddress1\"},{\"rowNum\":2,\"Site Id\":\"TestSiteId2\",\"Site Address\":\"TestSiteAddress2\"}]", List.class);//new ArrayList<>();
                RowCollection rows = targetTable.getRows();
                for (Row row : rows) {
                    if (row != targetTable.getFirstRow()) {
                        rows.remove(row);
                    }
                }

                for (Map<String, Object> dataRow : data) {
                    Row clonedRow = (Row) targetTable.getFirstRow().deepClone(true);
                    int sequence = 0;

                    for (Cell cell : clonedRow.getCells()) {
                        Font font = null;
                        if (cell.getFirstParagraph().getRuns().getCount() > 0) {
                            font = cell.getFirstParagraph().getRuns().get(0).getFont();
                            font.getHighlightColor();
                            font.getColor();

                        }
                        if (cell.getParagraphs().getCount() > 1) {
                            ParagraphCollection paragraphs = cell.getParagraphs();
                            for (Paragraph para : paragraphs) {
                                if (!cell.getFirstParagraph().equals(para)) {
                                    para.remove();
                                }
                            }
                        }
                        cell.getFirstParagraph().getChildNodes(NodeType.ANY, false).clear();
                        cell.getFirstParagraph().getParagraphFormat().clearFormatting();



                        String columnKey = orderToKey.get(String.valueOf(sequence));
                        sequence++;
                        if (StringUtil.isEmpty(columnKey)) {
                            continue;
                        }
                        Object columnValue = dataRow.get(columnKey);
                        if (columnValue == null) {
                            continue;
                        }
                        columnValue = dataRow.get(columnKey);
                        Run run = new Run(document, String.valueOf(columnValue));
                        if (font != null) {
                            run.getFont().setName(font.getName());
                            run.getFont().setSize(font.getSize());
                        }
                        cell.getFirstParagraph().getRuns().add(run);
                    }
                    targetTable.appendChild(clonedRow);
                }
            }
        }

        //document.updateFields();
        //document.updatePageLayout();
        document.save("/home/mohitkumar/Downloads/OldTemplateGen1.docx");

    }
}












@mohitkumar1991

This occurs because paragraphs added into the cells have space after:

You can reset it using the following code:

cell.getFirstParagraph().getParagraphFormat().setSpaceAfter(0);

If you need to hide text you can set Font.Hidden property.

If you need to set background of cell, you can use CellFormat.Shading property.