Table with high number of columns

Hi all,

I produce a word document which is consisted of a table with high number of columns.

In case I will set the autofit width as true (table.setAllowAutoFit(true), the table will extent the document width and the right part of the table will be invisible.

In case I will set the width of the table as the width of the document table.setPreferredWidth(PreferredWidth.fromPercent(100)), the cells are getting small and the text in each cell is illustrated almost vertical.

I cannot say that the two previous cases are acceptable by me because let’s say that the table looks ugly :slight_smile: am thinking that a possible solution is to split the table vertically. For instance, if I have a table with 10 columns, the first page could have 7 columns and the second page the rest 3 columns but I am not so sure about that.

Is there any technique I could follow in my case? Does anyone have any ideas?

@giokar,

Thanks for your inquiry. We suggest you please increase the width of page. You can use PageSetup.PageWidth property to set the width of page. You may change the orientation of page to Landscape using PageSetup.Orientation property.

In your case, we suggest you please create the two tables. One table with seven columns and other is with three columns. Insert page break after first table. Moreover, you can also work with table’s columns. Please refer to the following article. Hope this helps you.
Working with Columns and Rows

Thank you Tahir for your response!

Actually my table is really big, it may has more than 10 of columns and hundreds of rows.
In my case the paper size should be A4.

How could I handle something like this? How could I split a table like this?

@giokar,

Thanks for your inquiry. Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information on this along with code.

As you can see running the test, there are four cases.

  1. The first case tests a table of 8 columns and the export file is docx
  2. The second case tests a table of 8 columns and the export file is pdf
  3. The third case tests a table of 24 columns and the export file is docx
  4. And finally the fourth case tests a table of 24 columns and the export file is pdf

I am expecting the export format of the files from the first and the second tests to be identically but there are not.
I am also expecting the same for the third and the fourth tests but the format of the table the docx and the pdf files is not the same.

You can check the exported files and the code.

exports.zip (100.3 KB)

private HashMap<String, String> headerMap = new HashMap<>();
private List<Map<String, String>> results = new LinkedList<>();

@Test
public void mainTest(){
    try {
        test1();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        test2();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        test3();
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        test4();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

@Test
public void test1() throws Exception {

    File file = new File ("D:\\folder\\test1.docx");

    // Create header row
    for (int i = 1; i<9; i++){
        headerMap.put("key_" + i, "Header "+i);
    }

    // create results
    for (int i = 1; i<9; i++) {
        Map<String, String> map = new HashMap<String, String>();
        for (int j = 1; j < 9; j++) {
            map.put("key_" + j, "result " + i + "_" + j);
        }
        results.add(map);
    }

    try {
        createWithoutTemplate(file,results );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void test2() throws Exception {

    File file = new File ("D:\\folder\\test1.pdf");

    // Create header row
    for (int i = 1; i<9; i++){
        headerMap.put("key_" + i, "Header "+i);
    }

    // create results
    for (int i = 1; i<9; i++) {
        Map<String, String> map = new HashMap<String, String>();
        for (int j = 1; j < 9; j++) {
            map.put("key_" + j, "result " + i + "_" + j);
        }
        results.add(map);
    }

    try {
        createWithoutTemplate(file,results );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void test3() throws Exception {

    File file = new File ("D:\\folder\\test2.docx");

    // Create header row
    for (int i = 1; i<25; i++){
        headerMap.put("key_" + i, "Header "+i);
    }

    // create results
    for (int i = 1; i<25; i++) {
        Map<String, String> map = new HashMap<String, String>();
        for (int j = 1; j < 25; j++) {
            map.put("key_" + j, " Result" + i + "_" + j);
        }
        results.add(map);
    }

    try {
        createWithoutTemplate(file,results );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void test4() throws Exception {


    // Create header row
    for (int i = 1; i<25; i++){
        headerMap.put("key_" + i, "Header "+i);
    }

    // create results
    for (int i = 1; i<25; i++) {
        Map<String, String> map = new HashMap<String, String>();
        for (int j = 1; j < 25; j++) {
            map.put("key_" + j, "result " + i + "_" + j);
        }
        results.add(map);
    }

    try {
        createWithoutTemplate(file,results );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void createWithoutTemplate(File file, List<Map<String, String>> results) throws Exception {
    Document mainDoc = new Document();
    DocumentBuilder docBuilder = new DocumentBuilder(mainDoc);

    Table table = docBuilder.startTable();

    int i=0;
    for (Map<String, String> row : results) {
        if (i==0) {
            docBuilder.getRowFormat().setHeadingFormat(true);
            Font font = docBuilder.getFont();
            font.setBold(true);
            for (String key : row.keySet()) {
                docBuilder.insertCell();
                docBuilder.write(headerMap.get(key));
            }
            font.clearFormatting();
            docBuilder.endRow();
            docBuilder.getParagraphFormat().clearFormatting();
        }
        List<String> headers = new LinkedList<>();
        headers.addAll(headerMap.keySet());
        docBuilder.getRowFormat().setHeadingFormat(false);

        for (Map.Entry<String, String> entry : row.entrySet()) {

                docBuilder.insertCell();
                docBuilder.write(entry.getValue());

        }

        docBuilder.endRow();
        i++;
    }
    for (Row row : table) {
        row.getRowFormat().setAllowBreakAcrossPages(false);
    }

    table.setAllowAutoFit(false);
    table.setPreferredWidth(PreferredWidth.fromPercent(100));

    docBuilder.endTable();

    mainDoc.save(file.getAbsolutePath());
}

@giokar,

Thanks for your inquiry. You are using older version of Aspose.Words. We suggest you please use latest version of Aspose.Words for Java 17.9.

Please apply the license and use Aspose.Words. You can get the temporary license from here:
Get a Temporary License