Example usage for org.apache.poi.xwpf.usermodel BodyElementType TABLE

List of usage examples for org.apache.poi.xwpf.usermodel BodyElementType TABLE

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel BodyElementType TABLE.

Prototype

BodyElementType TABLE

To view the source code for org.apache.poi.xwpf.usermodel BodyElementType TABLE.

Click Source Link

Usage

From source file:com.pdf.GetPdf.java

public static void docConvert(Document document, String url, String type)
        throws IOException, DocumentException {
    WordExtractor we;/*from ww  w. jav  a  2s. c o m*/

    if (type.equals("doc")) {
        HWPFDocument wordDoc = new HWPFDocument(new URL(url).openStream());
        we = new WordExtractor(wordDoc);
        String[] paragraphs = we.getParagraphText();
        for (int i = 0; i < paragraphs.length; i++) {
            paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
            document.add(new Paragraph(paragraphs[i]));
        }
    } else {
        XWPFDocument wordDoc = new XWPFDocument(new URL(url).openStream());
        List<IBodyElement> contents = wordDoc.getBodyElements();

        for (IBodyElement content : contents) {
            if (content.getElementType() == BodyElementType.PARAGRAPH) {
                List<XWPFParagraph> paras = content.getBody().getParagraphs();
                for (XWPFParagraph para : paras) {
                    document.add(new Paragraph(para.getParagraphText()));
                }

            } else if (content.getElementType() == BodyElementType.TABLE) {
                List<XWPFTable> tables = content.getBody().getTables();
                for (XWPFTable table : tables) {
                    List<XWPFTableRow> rows = table.getRows();
                    for (XWPFTableRow row : rows) {
                        List<XWPFTableCell> tablecells = row.getTableCells();
                    }
                }
            }

        }
    }

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.TableCellVerticalAlignmentTestCase.java

License:Open Source License

@Test
public void testParagraphStyles() throws Exception {
    // 1) Load docx with Apache POI
    XWPFDocument document = new XWPFDocument(Data.class.getResourceAsStream("TableCellVerticalAlignment.docx"));

    // Create styles engine
    XWPFStylesDocument stylesDocument = new XWPFStylesDocument(document);

    // Loop for each paragraph
    List<IBodyElement> elements = document.getBodyElements();
    for (IBodyElement element : elements) {
        if (element.getElementType() == BodyElementType.TABLE) {
            testTable((XWPFTable) element, stylesDocument);
        }/* w  w  w .  java2 s.co m*/
    }
}

From source file:org.obeonetwork.m2doc.parser.TokenIterator.java

License:Open Source License

/**
 * Put the iterator in a state where it is finished or there's a token to
 * consumme in the currentIterator.//from  ww  w .j  a va2s  .c  o  m
 */
private void moveToNextToken() {
    if (tokenIterator == null || !tokenIterator.hasNext()) {
        while (elementIterator.hasNext() && (tokenIterator == null || !tokenIterator.hasNext())) {
            final IBodyElement element = elementIterator.next();
            if (element.getElementType().equals(BodyElementType.PARAGRAPH)) {
                // create an empty run if there's no run in the paragraph.
                // this eases the processing of documents. The processing is based on runs and a paragraph that has no run in it won't
                // be seen by the generator and, as a consequence, won't be inserted as a static part in the result.
                XWPFParagraph paragraph = (XWPFParagraph) element;
                if (paragraph.getRuns().size() == 0) {
                    paragraph.createRun().setText("");
                }
                tokenIterator = new RunIterator(((XWPFParagraph) element).getRuns());
            } else if (element.getElementType().equals(BodyElementType.TABLE)) {
                tokenIterator = new TableIterator((XWPFTable) element);
            } else {
                throw new UnsupportedOperationException(
                        "Unsupported type of body element : " + element.getElementType());
            }
        }
    }
}