Example usage for org.apache.poi.xwpf.usermodel XWPFDocument getStyles

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument getStyles

Introduction

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

Prototype

public XWPFStyles getStyles() 

Source Link

Document

get Styles

Usage

From source file:kz.service.DocumentReader.java

public static String readDocxFile(String fileName) {

    try {//from   www.  java 2  s  .  c om
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        StringBuffer content = new StringBuffer();

        XWPFDocument document = new XWPFDocument(fis);
        XWPFStyles styles = document.getStyles();

        List<XWPFParagraph> paragraphs = document.getParagraphs();
        List<XWPFTable> tables = document.getTables();
        List<XWPFPictureData> pictures = document.getAllPictures();

        //int Picture_ID = 0;
        for (XWPFPictureData picture : pictures) {
            //XWPFPictureData picture = pictures.get(Picture_ID);
            System.out.println("Picture: " + picture.getFileName());
            byte[] pictureData = picture.getData();
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(pictureData));
            ImageIO.write(image, picture.getFileName(), file);
            content.append("<p>");
            content.append("Here must be image");
            content.append("</p>");
            //Picture_ID++;
        }

        Iterator<IBodyElement> bodyElementIterator = document.getBodyElementsIterator();
        int Table_ID = 0;
        int Paragraph_ID = 0;
        while (bodyElementIterator.hasNext()) {

            IBodyElement element = bodyElementIterator.next();
            System.out.println(element.getElementType().name());//prints Element type name

            if ("TABLE".equalsIgnoreCase(element.getElementType().name())) {

                content.append("<table>");
                XWPFTable table = tables.get(Table_ID);
                CTTbl cttbl = table.getCTTbl();
                CTTblPr cttblPr = cttbl.getTblPr();

                List<XWPFTableRow> tblRows = table.getRows();
                for (XWPFTableRow tblRow : tblRows) {
                    content.append("<tr>");
                    List<XWPFTableCell> tblCells = tblRow.getTableCells();
                    for (XWPFTableCell tblCell : tblCells) {
                        content.append("<td>");
                        content.append(tblCell.getText());
                        content.append("</td>");
                    }
                    content.append("</tr>");
                }
                content.append("</table>");
                Table_ID++;

            } else if ("PARAGRAPH".equalsIgnoreCase(element.getElementType().name())) {

                XWPFParagraph paragraph = paragraphs.get(Paragraph_ID);

                String styleClass = null;
                if (paragraph.getStyleID() != null) {
                    content.append("<p class=''>");
                    XWPFStyle style = styles.getStyle(paragraph.getStyleID());
                    if (style != null && style.getName() != null) {
                        //here will be code creation of tag with class style
                    }
                } else {
                    content.append("<p>");
                }
                content.append(paragraph.getText());
                content.append("</p>");
                Paragraph_ID++;

            }
        }

        fis.close();
        return content.toString();
    } catch (Exception e) {
        return e.toString();
    }

}

From source file:org.obeonetwork.m2doc.generator.UserContentRawCopy.java

License:Open Source License

/**
 * Copy Table Style./*w  ww  . j  ava 2 s .c om*/
 * 
 * @param inputTable
 *            input Table
 * @param outputDoc
 *            outputDoc where copy style
 * @throws IOException
 *             if the copy fails
 */
private static void copyTableStyle(XWPFTable inputTable, XWPFDocument outputDoc) throws IOException {
    try (XWPFDocument inputDoc = inputTable.getBody().getXWPFDocument();) {
        XWPFStyle style = inputDoc.getStyles().getStyle(inputTable.getStyleID());
        if (outputDoc == null || style == null) {
            return;
        }

        if (outputDoc.getStyles() == null) {
            outputDoc.createStyles();
        }

        List<XWPFStyle> usedStyleList = inputDoc.getStyles().getUsedStyleList(style);
        for (XWPFStyle xwpfStyle : usedStyleList) {
            outputDoc.getStyles().addStyle(xwpfStyle);
        }
    }
}