Example usage for org.apache.poi.hwpf.usermodel TableRow numParagraphs

List of usage examples for org.apache.poi.hwpf.usermodel TableRow numParagraphs

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.usermodel TableRow numParagraphs.

Prototype


public int numParagraphs() 

Source Link

Document

Used to get the number of paragraphs in a range.

Usage

From source file:com.example.minireader.WordViewActivity.java

License:Apache License

public void readAndWrite() {

    try {// ww  w. jav  a2s .c om
        myFile = new File(htmlPath);
        output = new FileOutputStream(myFile);
        String head = "<html><body>";
        String tagBegin = "<p>";
        String tagEnd = "</p>";

        output.write(head.getBytes());

        //
        int numParagraphs = range.numParagraphs();

        //
        for (int i = 0; i < numParagraphs; i++) {
            Paragraph p = range.getParagraph(i);

            //
            if (p.isInTable()) {
                int temp = i;
                if (tableIterator.hasNext()) {
                    String tableBegin = "<table style=\"border-collapse:collapse\" border=1 bordercolor=\"black\">";
                    String tableEnd = "</table>";
                    String rowBegin = "<tr>";
                    String rowEnd = "</tr>";
                    String colBegin = "<td>";
                    String colEnd = "</td>";

                    Table table = tableIterator.next();
                    //
                    output.write(tableBegin.getBytes());

                    int rows = table.numRows();
                    //
                    for (int r = 0; r < rows; r++) {
                        output.write(rowBegin.getBytes());
                        TableRow row = table.getRow(r);
                        int cols = row.numCells();
                        int rowNumParagraphs = row.numParagraphs();
                        int colsNumParagraphs = 0;
                        //
                        for (int c = 0; c < cols; c++) {
                            output.write(colBegin.getBytes());
                            TableCell cell = row.getCell(c);
                            int max = temp + cell.numParagraphs();
                            colsNumParagraphs = colsNumParagraphs + cell.numParagraphs();
                            for (int cp = temp; cp < max; cp++) {
                                Paragraph p1 = range.getParagraph(cp);
                                output.write(tagBegin.getBytes());
                                writeParagraphContent(p1);
                                output.write(tagEnd.getBytes());
                                temp++;
                            }
                            output.write(colEnd.getBytes());
                        }
                        int max1 = temp + rowNumParagraphs;
                        for (int m = temp + colsNumParagraphs; m < max1; m++) {
                            Paragraph p2 = range.getParagraph(m);
                            temp++;
                        }
                        output.write(rowEnd.getBytes());
                    }
                    output.write(tableEnd.getBytes());
                }
                i = temp;
            } else {
                output.write(tagBegin.getBytes());
                writeParagraphContent(p);
                output.write(tagEnd.getBytes());
            }
        }

        String end = "</body></html>";
        output.write(end.getBytes());
        output.close();
    } catch (Exception e) {
        System.out.println("readAndWrite Exception");
        e.printStackTrace();
    }
}