Example usage for org.apache.poi.xwpf.usermodel XWPFStyles getStyle

List of usage examples for org.apache.poi.xwpf.usermodel XWPFStyles getStyle

Introduction

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

Prototype

public XWPFStyle getStyle(String styleID) 

Source Link

Document

Get style by a styleID

Usage

From source file:kz.service.DocumentReader.java

public static String readDocxFile(String fileName) {

    try {//from   w w w .  j a va 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:offishell.task.Task.java

License:MIT License

/**
 * <p>/*from  ww w  .j  av a2 s .c o m*/
 * ???
 * </p>
 * 
 * @return
 */
default Date month() {
    List<XWPFParagraph> paras = mainWord().paragraphs.toList();
    List<String> methods = I.signal(new Error().getStackTrace())
            .take(e -> e.getClassName().equals(getClass().getName())).map(e -> e.getMethodName()).toList();

    XWPFStyles styles = paras.get(0).getDocument().getStyles();
    String heading = "";

    for (XWPFParagraph para : paras) {
        String text = para.getText();
        String id = para.getStyleID();

        if (id != null && styles.getStyle(id).getName().toLowerCase().contains("heading")) {
            heading = text;
        }

        if (methods.stream().anyMatch(text::contains)) {
            // parse heading text
            heading = Normalizer.normalize(heading, Form.NFKC);

            int start = heading.indexOf("(");
            int end = heading.indexOf(")");

            if (start != -1 && end != -1) {
                heading = heading.substring(start + 1, end);

                Matcher matcher = Pattern.compile("((\\d+))?(\\d+).*").matcher(heading);

                if (matcher.matches()) {
                    int year = matcher.group(1) == null ? LocalDate.now().getYear()
                            : Integer.parseInt(matcher.group(2));

                    return Date.of(year, Integer.parseInt(matcher.group(3)), 1);
                }
            }
            return Date.now();
        }
    }
    return Date.now();
}