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

Free Support Forum - aspose.com

I want to adjust the width of the dynamic columns to a fixed size

Hi I have a table which is growing dynamically depending upon the data that is coming from the objects.

Here i have a requirement that i want to have a fixed size of the model columns that are getting added dynamically to the table . Till 14 model columns i can see the data is properly visible and the text is not getting wrapped but as soon as the model column size increases it starts impacting the width and alignment of other columns.

I want my model column to look like as in the model attachment.

my code goes like .

    public void generateTestReport() throws Exception {

        String blankValue = "";
        String type = blankValue;
        String EUAD = blankValue;
        String meansOfCompliance = blankValue;
        String remark = blankValue;
        String subject = blankValue;
        String effectiveDate = blankValue;
        String effectiveFrom = blankValue;
        String effectiveTo = blankValue;
        String todayDate = null;
        byte extractDoc[] = null;
        String[] adCols = null;
        String isPAA=blankValue;
        int count = 0;
        String tokenVal = blankValue;
        ByteArrayOutputStream bAOS = new ByteArrayOutputStream();
        Document doc = new Document("input.rtf");
        DocumentBuilder builder=new DocumentBuilder(doc);
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        table.setTextWrapping(TextWrapping.NONE);
        table.getFirstRow().getRowFormat().setHeadingFormat(true);
        CellFormat headerCell = null;
        CellFormat modelFormat=null;
        com.aspose.words.Font columnTypeFont = null;
        com.aspose.words.Font dymanicColFont = null;
        //com.aspose.words.Font dymanicColFont1 = null;
        com.aspose.words.Font columnSubjectFont = null;
        com.aspose.words.Font columnPAAFont = null;
        com.aspose.words.Font effectiveDateFont = null;
        com.aspose.words.Font mocFont = null;
        com.aspose.words.Font remarks = null;
        com.aspose.words.Font model = null;
        Column column = Column.fromIndex(table, 1);
        for (Row row1 : table.getRows()) {
            int rowIndex = table.getRows().indexOf(row1);
            if (rowIndex == 1)
                break;
            headerCell = getFormat(table.getRows().get(0).getCells().get(1));
            modelFormat = getFormat(table.getRows().get(0).getCells().get(7));
            columnTypeFont = getFontandFormat(row1.getCells().get(0), headerCell);
            dymanicColFont = getFontandFormat(row1.getCells().get(1), headerCell);
            //dymanicColFont1 = getFontandFormat(table.getRows().get(1).getCells().get(1), headerCell);
            columnSubjectFont = getFontandFormat(row1.getCells().get(2), headerCell);
            columnPAAFont=getFontandFormat(row1.getCells().get(3), headerCell);
            effectiveDateFont = getFontandFormat(row1.getCells().get(4), headerCell);
            mocFont = getFontandFormat(row1.getCells().get(5), headerCell);
            remarks = getFontandFormat(row1.getCells().get(6), headerCell);
            model = getFontandFormat(row1.getCells().get(7), headerCell);
            row1.getCells().get(1).remove();
            row1.getCells().get(6).remove();
            row1.getRowFormat().setHeadingFormat(true);
            for (int b = 0; b < 3; b++) {
                Column newColumn = column.insertColumnBefore();
                for (Cell cell : newColumn.getCells()) {
                    Run run = new Run(doc, " "+"u");
                    run.getFont().setBold(true);
                    cell.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
                    cell.getCellFormat().setWrapText(false);
                    run.getFont().setStyle(dymanicColFont.getStyle());
                    run.getFont().setSize(dymanicColFont.getSize());
                    run.getFont().setName(columnTypeFont.getName());
                    cell.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(0.1));
                    cell.getCellFormat().setTopPadding(10.0);
                    cell.getFirstParagraph().appendChild(run);
                    break;
                }
            }
            for (int j = 0; j < 24; j++) {
                Cell c = new Cell(doc);
                c.getCellFormat().setPreferredWidth(modelFormat.getPreferredWidth());
                row1.appendChild©;
                c.appendChild(new com.aspose.words.Paragraph(doc));
                Run run = new Run(doc, "models-a");
                run.getFont().setBold(true);
                c.getCellFormat().setWrapText(true);
                run.getFont().setStyle(model.getStyle());
                run.getFont().setSize(model.getSize());
                run.getFont().setName(model.getName());
                c.getFirstParagraph().appendChild(run);
            }
        }
        for (int i = 0; i < 600; i++) {
            Row newRow = new Row(doc);
            newRow.getRowFormat().setAllowBreakAcrossPages(true);
            table.appendChild(newRow);
            // static
            Cell c1 = new Cell(doc);
            c1.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
            newRow.appendChild(c1);
            c1.appendChild(new com.aspose.words.Paragraph(doc));
            Run run = new Run(doc,"b");
            run.getFont().setStyle(columnTypeFont.getStyle());
            run.getFont().setSize(columnTypeFont.getSize());
            run.getFont().setName(columnTypeFont.getName());
            c1.getFirstParagraph().appendChild(run);
            for (int b = 0; b <3; b++) {
                Cell c = new Cell(doc);
                c.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
                newRow.appendChild©;
                c.appendChild(new com.aspose.words.Paragraph(doc));
                c.getCellFormat().setWrapText(false);
                Run run2 = new Run(doc, "c");
                //run.getFont().setBold(true);
                //c.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
                run2.getFont().setStyle(dymanicColFont.getStyle());
                run2.getFont().setSize(dymanicColFont.getSize());
                run2.getFont().setName(dymanicColFont.getName());
                c.getFirstParagraph().appendChild(run2);
            }
            // static
            //cellCreate(doc, newRow, columnSubjectFont, subject, headerCell);
            Cell c0 = new Cell(doc);
            c0.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
            c0.getCellFormat().setWidth(headerCell.getWidth());
            newRow.appendChild(c0);
            c0.appendChild(new com.aspose.words.Paragraph(doc));
            Run run9 = new Run(doc,"d");
            run9.getFont().setStyle(columnSubjectFont.getStyle());
            run9.getFont().setSize(columnSubjectFont.getSize());
            run9.getFont().setName(columnSubjectFont.getName());
            c0.getFirstParagraph().appendChild(run9);
            // static
            //cellCreate(doc, newRow, columnPAAFont, isPAA, headerCell);
            Cell c3 = new Cell(doc);
            c3.getCellFormat().setPreferredWidth(modelFormat.getPreferredWidth());
            newRow.appendChild(c3);
            c3.appendChild(new com.aspose.words.Paragraph(doc));
            Run run3 = new Run(doc,"e");
            run3.getFont().setStyle(columnPAAFont.getStyle());
            run3.getFont().setSize(columnPAAFont.getSize());
            run3.getFont().setName(columnPAAFont.getName());
            c3.getFirstParagraph().appendChild(run3);
            //cellCreate(doc, newRow, effectiveDateFont, effectiveDate, headerCell);
            Cell c4 = new Cell(doc);
            c4.getCellFormat().setPreferredWidth(headerCell.getPreferredWidth());
            newRow.appendChild(c4);
            c4.getCellFormat().setWrapText(false);
            c4.appendChild(new com.aspose.words.Paragraph(doc));
            Run run4 = new Run(doc,"f");
            run4.getFont().setStyle(effectiveDateFont.getStyle());
            run4.getFont().setSize(effectiveDateFont.getSize());
            run4.getFont().setName(effectiveDateFont.getName());
            c4.getFirstParagraph().appendChild(run4);
            // static
            cellCreate(doc, newRow, mocFont, meansOfCompliance, headerCell);
            // static
            cellCreate(doc, newRow, remarks, remark, headerCell);
            // dynamic on models
            for (int j = 0; j < 24; j++) {
                //cellCreate(doc, newRow, dymanicColFont, "x", headerCell);
                Cell c = new Cell(doc);
                //c.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
                c.getCellFormat().setPreferredWidth(modelFormat.getPreferredWidth());
                c.getCellFormat().setWidth(modelFormat.getWidth());
                newRow.appendChild©;
                c.appendChild(new com.aspose.words.Paragraph(doc));
                Run run7 = new Run(doc, "x");
                //run.getFont().setBold(true);
                c.getCellFormat().setWrapText(true);
                run7.getFont().setStyle(model.getStyle());
                run7.getFont().setSize(model.getSize());
                run7.getFont().setName(model.getName());
                c.getFirstParagraph().appendChild(run7);
            }
            table.setBorders(LineStyle.HAIRLINE, 0.05, Color.LIGHT_GRAY);
            table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
        }
        builder.moveToDocumentEnd();
        ParagraphFormat paragraphFormat = builder.getParagraphFormat();
        paragraphFormat.setAlignment(ParagraphAlignment.CENTER);
        builder.write("End of report");
        doc.save("C://Users//akhil_samnotra//Desktop//Reports//out.docx");
    }

    public void cellCreate(Document doc, Row newRow, com.aspose.words.Font font, String data, CellFormat format) {
        Cell c = new Cell(doc);
        newRow.appendChild©;
        //c.getCellFormat().setWidth(1.00);
        c.getCellFormat().setTopPadding(5.0);
        c.getCellFormat().setWrapText(true);
        c.appendChild(new com.aspose.words.Paragraph(doc));
        Run r = new Run(doc, "\n" + data);
        try {
            r.getFont().setSize(font.getSize());
            r.getFont().setName(font.getName());
            r.getFont().setColor(font.getColor());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        c.getCellFormat().setPreferredWidth(PreferredWidth.fromPercent(0.1));
        c.getFirstParagraph().appendChild®;
    }

import java.util.ArrayList;
import com.aspose.words.Cell;
import com.aspose.words.Row;
import com.aspose.words.SaveFormat;
import com.aspose.words.Table;

    public class Column {
        private int mColumnIndex;
        private Table mTable;
        private Column(Table table, int columnIndex) {
            if (table == null)
                throw new IllegalArgumentException("table");
            mTable = table;
            mColumnIndex = columnIndex;
        }
        /* Returns a new column facade from the table and supplied zero-based index.
                */
        public static Column fromIndex(Table table, int columnIndex) {
            return new Column(table, columnIndex);
        }

        /* Returns the cells which make up the column.
                */
        public Cell[] getCells() {
            ArrayList columnCells = getColumnCells();
            return (Cell[]) columnCells.toArray(new Cell[columnCells.size()]);
        }

        /* Returns the index of the given cell in the column.
                */
        public int indexOf(Cell cell) {
            return getColumnCells().indexOf(cell);
        }

        /* Inserts a brand new column before this column into the table.
                * @throws Exception 
        */
        public Column insertColumnBefore() throws Exception {
            Cell[] columnCells = getCells();
            if (columnCells.length == 0)
                throw new IllegalArgumentException("Column must not be empty");
            // Create a clone of this column.
            for (Cell cell : columnCells)
                cell.getParentRow().insertBefore(cell.deepClone(false), cell);
            // This is the new column.
            Column column = new Column(columnCells[0].getParentRow().getParentTable(), mColumnIndex);
            // We want to make sure that the cells are all valid to work with (have at least one paragraph).
            for (Cell cell : column.getCells())
                cell.ensureMinimum();
            // Increase the index which this column represents since there is now one extra column infront.
            mColumnIndex++;
            return column;
        }

        /* Removes the column from the table.
                */
        public void remove() {
            for (Cell cell : getCells())
                cell.remove();
        }

        /* Returns the text of the column.
                */
        public String toTxt() throws Exception {
            StringBuilder builder = new StringBuilder();
            for (Cell cell : getCells())
                builder.append(cell.toString(SaveFormat.TEXT));
            return builder.toString();
        }

        /**
         * Provides an up-to-date collection of cells which make up the column
         * represented by this facade.
         */
        private ArrayList getColumnCells() {
            ArrayList columnCells = new ArrayList();
            for (Row row : mTable.getRows()) {
                Cell cell = row.getCells().get(mColumnIndex);
                if (cell != null)
                    columnCells.add(cell);
            }
            return columnCells;
        }
    }
}

Also, i would like to mention that i’m using the auto-fit property of the table so that it fits the page. With out autofit the table is not in a readable format.

Kindly help me to set the size of my model columns to a fixed size i.e 0.07" inch

Thanks

Akhil Samnotra

Hi Akhil,

Thanks for your inquiry. I am afraid I am unable to test your code due to missing references; getFormat() and getFontandFormat() . Please share the complete working code here, we will look into it and will guide you accordingly.

Best Regards,

Thanks Tilal for prompt reply .

Please find below the missing Methods.

    public CellFormat getFormat(Cell cell) {
        CellFormat format = cell.getCellFormat();
        return format;
    }

    public com.aspose.words.Font getFontandFormat(Cell cell, CellFormat format) {
        Iterator i = cell.getChildNodes(NodeType.RUN, true).iterator();
        format = cell.getCellFormat();
        com.aspose.words.Font f = null;
        while (i.hasNext()) {
            Run r = (Run) i.next();
            f = r.getFont();
            return f;
        }
        return f;
    }