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

Free Support Forum - aspose.com

Removing Table Property

I am drawing a table into word document by setting column width for each column, but after generating if i open the word document i find all columns having the same width, if i uncheck the prefered width for column in table properties, column width changes to what i have set. Kindly suggest how can change this property using api. I have attached my test program for your reference.

Thanks
Gopal

Hi

Thanks for your request. There is no way to disable this. But, I found a mistake in your code. You never increase colIndex. So width of cell is always set to 120 (the first value). You should modify your code like this:

String[] currentColWidth = columnWidths[colIndex++].split("-");

Also, I think you should disable AllowAutoFit option.

builder.startTable();
builder.getRowFormat().setAllowAutoFit(false);

In this case you will get the expected result.

Hope this helps.

Best regards,

Thanks for input.

I need help for another issue i am facing. I want to merge first & second column of a table from the last row. I am sending you the code sample which i have written for you to check. It is merging 1,2 & 3 columns of the last row & even the text in column are going away, kindly suggest what is the change required.

    public static void insertDataTableAtBookMark() throws Throwable {
        ValueObject vo = VoFactory.createVo();
        vo.setSfield("HLT Name", "ABCCCCCCCCCCCCCC DDDDDDDDDD");
        vo.setSfield("PT Name", "ABCCCCCCCCCCCCCC DDDDDDDDDD");
        vo.setSfield("Fatal count", "10");
        vo.setSfield("Total Events", "10");
        vo.setSfield("COLUMNS\_TO\_BE\_MERGED", "");
        vo.setSfield("COLUMNS\_WIDTH", "1-120,2-120,3-50,4-50");

        List data = new ArrayList();
        data.add(vo);

        vo = VoFactory.createVo();
        vo.setSfield("HLT Name", "");
        vo.setSfield("PT Name", "Total");
        vo.setSfield("Fatal count", "10");
        vo.setSfield("Total Events", "10");
        vo.setSfield("COLUMNS\_TO\_BE\_MERGED", "12");
        vo.setSfield("COLUMNS\_WIDTH", "1-120,2-120,3-50,4-50");
        data.add(vo);

        Document document = new Document();
            //make a builder
        DocumentBuilder builder = new DocumentBuilder(document);
        Paragraph para = builder.insertParagraph();
        para.getParagraphFormat().clearFormatting();

        //get the headings
        ValueObject fieldsVo = data.get(0);
        String[] fieldNames = fieldsVo.getFieldNames();

        //start the table
        //Table table =
        Table table = builder.startTable();
        builder.getRowFormat().setHeight(30);
        builder.getRowFormat().setHeightRule(HeightRule.AT\_LEAST);
        builder.getRowFormat().setAllowBreakAcrossPages(true);
        builder.getRowFormat().setAlignment(RowAlignment.CENTER);

        builder.getRowFormat().getBorders().getTop().setLineStyle(LineStyle.SINGLE);
        builder.getRowFormat().getBorders().getTop().setLineWidth(1);

        builder.getRowFormat().getBorders().getLeft().setLineStyle(LineStyle.SINGLE);
        builder.getRowFormat().getBorders().getLeft().setLineWidth(1);

        builder.getRowFormat().getBorders().getRight().setLineStyle(LineStyle.SINGLE);
        builder.getRowFormat().getBorders().getRight().setLineWidth(1);

        builder.getRowFormat().getBorders().getBottom().setLineStyle(LineStyle.SINGLE);
        builder.getRowFormat().getBorders().getBottom().setLineWidth(3);

        //save font color
        //Color builderFontColor = builder.getFont().getColor();

        //go through the headings and add them
        int colIndex = 0;

        for (String field : fieldNames) {
            if (!field.equals("COLUMNS\_WIDTH") && !field.equals("COLUMNS\_TO\_BE\_MERGED")) {

                Cell cell = builder.insertCell();

                //code for top alignment of each cell…
                cell.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

                //use cellformat with write fonts
                cell.getCellFormat().getShading().setBackgroundPatternColor(Color.BLACK);
                builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);

                cell.getCellFormat().setWrapText(true);
                cell.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                cell.getCellFormat().getBorders().setLineWidth(1);
                cell.getCellFormat().getBorders().setColor(Color.WHITE);

                // Specify font formatting before adding text.
                Font font = builder.getFont();
                font.setBold(true);
                //font.setColor(Color.WHITE);

                if (!fieldsVo.getSfield("COLUMNS\_WIDTH").equals("")) {

                    String columnWidths[] = fieldsVo.getSfield("COLUMNS\_WIDTH").split(",");

                    String[] currentColWidth = columnWidths[colIndex].split("-");

                    //builder.getParagraphFormat().getStyle().getFont().setColor(Color.BLACK);
                    //builder.getCellFormat().setWidth(getTableColWidth(fieldNames.length));
                    cell.getCellFormat().setWidth(Double.parseDouble(currentColWidth[1]));
                }

                //builder.getCellFormat().setWidth(getTableColWidth(fieldNames.length));

                //write the heading in the cell
                //builder.write(field);
                //write the data in the cell
                builder.write(getNewLineChar(field));
            }
            colIndex++;
        }

        //reset the font color
        // builder.getFont().setColor(builderFontColor);

        //end the row to mark end of heading
        Row headingRow = builder.endRow();
        headingRow.getRowFormat().setHeadingFormat(true);

        //now comes the data
        for (int rowNum = 0; rowNum < data.size(); rowNum++) {

            colIndex = 0;
            ValueObject rowVo = data.get(rowNum);
            String[] fields = rowVo.getFieldNames();
            for (String field : fields) {
                if (!field.equals("COLUMNS\_WIDTH") && !field.equals("COLUMNS\_TO\_BE\_MERGED")) {

                    String value = rowVo.getSfield(field);

                    Cell cell = builder.insertCell();

                    //code for top alignment of each cell…
                    cell.getCellFormat().setVerticalAlignment(CellVerticalAlignment.TOP);

                    //use cellformat with write fonts
                    cell.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
                    builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
                    cell.getCellFormat().setWrapText(true);
                    cell.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
                    cell.getCellFormat().getBorders().setLineWidth(1);
                    cell.getCellFormat().getBorders().setColor(Color.BLACK);

                    // Specify font formatting before adding text.
                    Font font = builder.getFont();
                    font.setBold(false);
                    // font.setColor(Color.BLACK);

                    if (!rowVo.getSfield("COLUMNS\_WIDTH").equals("")) {

                        String columnWidths[] = rowVo.getSfield("COLUMNS\_WIDTH").split(",");

                        String[] currentColWidth = columnWidths[colIndex].split("-");

                        //builder.getParagraphFormat().getStyle().getFont().setColor(Color.BLACK);
                        //builder.getCellFormat().setWidth(getTableColWidth(fieldNames.length));
                        cell.getCellFormat().setWidth(Double.parseDouble(currentColWidth[1]));
                    }

                    String colMerge = rowVo.getSfield("COLUMNS\_TO\_BE\_MERGED");

                    if (!colMerge.equals("") && colMerge.contains(String.valueOf(colIndex+1))){
                        cell.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
                    }else{
                        cell.getCellFormat().setHorizontalMerge(CellMerge.NONE);
                    }

                    //write the data in the cell
                    builder.write(getNewLineChar(value));
                }
                colIndex++;
            }

            Row dataRow = builder.endRow();
            dataRow.getRowFormat().setHeadingFormat(false);

        }

        //at this point all the data has been added
        //close the table
        //table =
        builder.endTable();

        /*para = builder.insertParagraph();
                para.getParagraphFormat().clearFormatting();
        \*/
        document.save("c:/Test.doc");
    }

Thanks,
Gopal

Hi Gopal,

Thanks for your request. It seems this is a bug in Aspose.Words. If you save the output document as DOCX, you will see the expected output. But with DOC the output is incorrect. I logged this issue in our defect database. We will let you know once it is resolved.

Best regards,

A fix for the issue(s) you’ve reported (filed as 26962) will be released in the next release at the end of this month. You will be notified.

The issues you have found earlier (filed as WORDSNET-4734) have been fixed in this .NET update and in this Java update.