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

Free Support Forum - aspose.com

Table heading not getting reflected on every page

Hi, I’m creating a report with table getting dynamically populated with the objects. I’m picking the font setting dynamically but i’m unable to get the table header on each and every page. i have even used the line

Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
table.getRows().get(0).getRowFormat().setHeadingFormat(true);

But it is not working. Code snippet :

    public void generateReport() throws Exception
    {
        Document doc = new Document(inputstream);
        Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
        table.getRows().get(0).getRowFormat().setHeadingFormat(true);
        CellFormat headerCell = 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 effectiveDateFont = null;
        com.aspose.words.Font mocFont = null;
        com.aspose.words.Font remarks = null;
        Column column = Column.fromIndex(table, 1);
        for (Row row1 : table.getRows()) {
            int rowIndex = table.getRows().indexOf(row1);
            if (rowIndex == 1)
                break;
            headerCell = getFormat(row1.getCells().get(0));
            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);
            effectiveDateFont = getFontandFormat(row1.getCells().get(3), headerCell);
            mocFont = getFontandFormat(row1.getCells().get(4), headerCell);
            remarks = getFontandFormat(row1.getCells().get(5), headerCell);
            row1.getCells().get(1).remove();
            row1.getRowFormat().setHeadingFormat(true);
            for (int s = 0; s <= 2; s++) {
                Column newColumn = column.insertColumnBefore();
                for (Cell cell : newColumn.getCells()) {
                    Run run = new Run(doc, "p");
                    run.getFont().setStyle(columnTypeFont.getStyle());
                    run.getFont().setSize(columnTypeFont.getSize());
                    run.getFont().setName(columnTypeFont.getName());
                    cell.getFirstParagraph().appendChild(run);
                    cell.getCellFormat().setWrapText(true);
                }
            }
            // Models
            for (int j = 0; j < 3; j++) {
                Cell c = new Cell(doc);
                //c.getCellFormat().getShading().setBackgroundPatternColor(java.awt.Color.LIGHT_GRAY);
                c.getCellFormat().setWidth(0.08);
                row1.appendChild©;
                c.appendChild(new com.aspose.words.Paragraph(doc));
                Run run = new Run(doc, "x");
                run.getFont().setStyle(columnTypeFont.getStyle());
                run.getFont().setSize(columnTypeFont.getSize());
                run.getFont().setName(columnTypeFont.getName());
                //c.getCellFormat().getBorders().setLineWidth(LineStyle.SINGLE);
                //c.getCellFormat().getBorders().setColor(Color.BLACK);
                c.getFirstParagraph().appendChild(run);
            }
        }
        com.aspose.words.Font columnTypeFont1 = null;
        com.aspose.words.Font columnSubjectFont1 = null;
        com.aspose.words.Font effectiveDateFont1 = null;
        com.aspose.words.Font mocFont1 = null;
        com.aspose.words.Font remarks1 = null;
        for (int i = 0; i < 200; i++) {
            if (i == 0) {
                for (Row row1 : table.getRows()) {
                    if (table.getRows().indexOf(row1) == 1) {
                        int cellCount = row1.getCells().getCount();
                        row1 = table.getRows().get(1);
                        columnTypeFont1 = getFontandFormat(row1.getCells().get(0), headerCell);
                        columnSubjectFont1 = getFontandFormat(row1.getCells().get(cellCount - 4), headerCell);
                        effectiveDateFont1 = getFontandFormat(row1.getCells().get(cellCount - 3), headerCell);
                        mocFont1 = getFontandFormat(row1.getCells().get(cellCount - 2), headerCell);
                        remarks1 = getFontandFormat(row1.getCells().get(cellCount - 1), headerCell);
                        for (Cell cell : row1.getCells()) {
                            cell.remove();
                        }
                    }
                }
            }
            Row newRow = new Row(doc);
            newRow.getRowFormat().setAllowBreakAcrossPages(true);
            table.appendChild(newRow);
            // static
            cellCreate(doc, newRow, columnTypeFont1, "x", headerCell);
            for (int s = 0; s <= 2; s++) {
                cellCreate(doc, newRow, dymanicColFont1, "a", headerCell);
            }
            // static
            cellCreate(doc, newRow, columnSubjectFont1, "b", headerCell);
            // static
            cellCreate(doc, newRow, effectiveDateFont1, "c", headerCell);
            // static
            cellCreate(doc, newRow, mocFont1, "d", headerCell);
            // static
            cellCreate(doc, newRow, remarks1, "e", headerCell);
            // dynamic on models
            for (int j = 0; j < 3; j++) {
                cellCreate(doc, newRow, dymanicColFont, "f", headerCell);
            }
            table.setBorders(LineStyle.HAIRLINE, 0.05, Color.LIGHT_GRAY);
            table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
        }
        doc.save(outputstream);
    }

    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®;
    }

    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;
    }

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

Please let me know if your not able to run the code. Please assist for the same .

Hi there,

Thanks for your inquriy. I am afraid we are unable to run your code, due to missing references. However please check following sample code to achieve your requirements. Your table is floating table so you need to set TextWrapping property to None for repeating heading row on subsequent pages.

com.aspose.words.Document doc = new com.aspose.words.Document("COMPENDIUM.rtf");
com.aspose.words.Table table = (com.aspose.words.Table)doc.getChild(NodeType.TABLE, 0, true);
table.setTextWrapping(TextWrapping.NONE);
table.getFirstRow().getRowFormat().setHeadingFormat(true);
for (int i = 0; i < 200; i++)
{
    com.aspose.words.Row rowCLone = (com.aspose.words.Row)table.getLastRow().deepClone(true);
    rowCLone.getRowFormat().setHeadingFormat(false);
    table.appendChild(rowCLone);
}
doc.save("Clone_table_Out.docx");

Best Regards,