Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getStyleID

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getStyleID

Introduction

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

Prototype

public String getStyleID() 

Source Link

Document

Return styleID of the paragraph if style exist for this paragraph if not, null will be returned

Usage

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphPropertiesOnly(List<XWPFParagraph> lp) {
    int i1 = 1;/* w ww .j av a 2 s  .c o  m*/
    for (XWPFParagraph p : lp) {
        //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText());
        System.out.println("____________________________________");
        if (p.getParagraphText().trim().length() > 0) {
            System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText());
            System.out.println("ALIGNMENT: " + p.getAlignment().toString());
            //Uncomment to display other properties

            System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString());
            System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString());
            System.out.println("BORDER LEFT: " + p.getBorderLeft().toString());
            System.out.println("BORDER RIGHT: " + p.getBorderRight().toString());
            System.out.println("BORDER TOP: " + p.getBorderTop().toString());
            System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString());
            System.out.println("FOOTNOTE: " + p.getFootnoteText());
            System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine());
            System.out.println("INDENTATION HANGING: " + p.getIndentationHanging());
            System.out.println("INDENTATION LEFT: " + p.getIndentationLeft());
            System.out.println("INDENTATION RIGHT: " + p.getIndentationRight());
            System.out.println("NUMBERING FORMAT: " + p.getNumFmt());
            System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl());
            System.out.println("STYLE: " + p.getBody().getXWPFDocument().getStyles().getStyle(p.getStyleID()));

            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());

            System.out.println("SPACING VALUE: " + pc.getCTSpacing(false).getLine().floatValue() / 240);
            System.out.println("SPACING AFTER: " + p.getSpacingAfter());
            System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines());
            System.out.println("SPACING BEFORE: " + p.getSpacingBefore());
            System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines());
            System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule());
            System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment());

        } // can also use .searchText to look for a string
        else {
            // Uncomment to display lines
            //System.out.println("\n#" + i1++ + " LINE: <SPACE>");
        }
    }
}

From source file:com.project3.utils.poiold.DocumentPropertyEnumerator.java

public static void showAllParagraphProperties(List<XWPFParagraph> lp) {
    int i1 = 1;/*  w  w w.  j  a va 2  s .co  m*/
    for (XWPFParagraph p : lp) {
        //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText());
        System.out.println("____________________________________");
        if (p.getParagraphText().trim().length() > 0) {
            System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText());
            System.out.println("ALIGNMENT: " + p.getAlignment().toString());

            System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString());
            System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString());
            System.out.println("BORDER LEFT: " + p.getBorderLeft().toString());
            System.out.println("BORDER RIGHT: " + p.getBorderRight().toString());
            System.out.println("BORDER TOP: " + p.getBorderTop().toString());
            System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString());
            System.out.println("FOOTNOTE: " + p.getFootnoteText());
            System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine());
            System.out.println("INDENTATION HANGING: " + p.getIndentationHanging());
            System.out.println("INDENTATION LEFT: " + p.getIndentationLeft());
            System.out.println("INDENTATION RIGHT: " + p.getIndentationRight());
            System.out.println("NUMBERING FORMAT: " + p.getNumFmt());
            System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl());
            System.out.println("STYLE: " + p.getBody().getXWPFDocument().getStyles().getStyle(p.getStyleID()));

            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());

            System.out.println("SPACING VALUE: " + pc.getCTSpacing(false).getLine().floatValue() / 240);
            System.out.println("SPACING AFTER: " + p.getSpacingAfter());
            System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines());
            System.out.println("SPACING BEFORE: " + p.getSpacingBefore());
            System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines());
            System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule());
            System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment());

        } // can also use .searchText to look for a string
        else {
            // Uncomment to display lines
            //System.out.println("\n#" + i1++ + " LINE: <SPACE>");
        }
    }
}

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

License:Open Source License

private void testParagraph(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {

    String styleId = paragraph.getStyleID();
    if ("Style1".equals(styleId)) {
        testParagraphWithStyle1(paragraph, stylesDocument);
    } else if ("Style2".equals(styleId)) {
        testParagraphWithStyle2(paragraph, stylesDocument);
    } else if ("Style3".equals(styleId)) {
        testParagraphWithStyle3(paragraph, stylesDocument);
    } else if ("Style4".equals(styleId)) {
        testParagraphWithStyle4(paragraph, stylesDocument);
    }// w ww  . jav  a  2s.  com
}

From source file:fr.opensagres.poi.xwpf.converter.core.utils.StylesHelper.java

License:Open Source License

public static List<String> getStyleIDs(XWPFParagraph paragraph) {
    List<String> styleIDs = null;
    String paragraphStyleId = paragraph.getStyleID();
    boolean hasParagraphStyleId = StringUtils.isNotEmpty(paragraphStyleId);
    if (hasParagraphStyleId) {
        if (styleIDs == null) {
            styleIDs = new ArrayList<String>();
        }/*from   w  ww. j  a  v  a 2 s. c  o m*/
        styleIDs.add(paragraphStyleId);
    }
    // if paragraph is include in a table which defines a style, use this tabe style
    //        IBody body = paragraph.getBody();
    //        if ( body != null && body.getPartType() == BodyType.TABLECELL )
    //        {
    //            XWPFTableCell cell = (XWPFTableCell) body;
    //            XWPFTable table = cell.getTableRow().getTable();
    //            String tableStyleID = table.getStyleID();
    //            if ( StringUtils.isNotEmpty( tableStyleID ) )
    //            {
    //                if ( styleIDs == null )
    //                {
    //                    styleIDs = new ArrayList<String>();
    //                }
    //                styleIDs.add( tableStyleID );
    //            }
    //        }
    return styleIDs;
}

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

License:Open Source License

protected void visitBodyElements(List<IBodyElement> bodyElements, T container) throws Exception {
    if (!masterPageManager.isInitialized()) {
        // master page manager which hosts each <:w;sectPr declared in the word/document.xml
        // must be initialized. The initialization loop for each
        // <w:p paragraph to compute a list of <w:sectPr which contains information
        // about header/footer declared in the <w:headerReference/<w:footerReference
        masterPageManager.initialize();//w ww .  j a v  a  2s  .  co  m
    }

    String previousParagraphStyleName = null;
    for (int i = 0; i < bodyElements.size(); i++) {
        IBodyElement bodyElement = bodyElements.get(i);
        switch (bodyElement.getElementType()) {
        case PARAGRAPH:
            XWPFParagraph paragraph = (XWPFParagraph) bodyElement;
            String paragraphStyleName = paragraph.getStyleID();
            boolean sameStyleBelow = (paragraphStyleName != null
                    && paragraphStyleName.equals(previousParagraphStyleName));

            visitParagraph(paragraph, i, container);
            break;
        case TABLE:
            previousParagraphStyleName = null;
            visitTable((XWPFTable) bodyElement, i, container);
            break;
        case CONTENTCONTROL:
            visitSDT((XWPFSDT) bodyElement, i, container);
            break;
        }
    }

}

From source file:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected Object startVisitParagraph(XWPFParagraph paragraph, ListItemContext itemContext,
        Object parentContainer) throws Exception {
    // 1) create attributes

    // 1.1) Create "class" attributes.
    AttributesImpl attributes = createClassAttribute(paragraph.getStyleID());

    // 1.2) Create "style" attributes.
    CTPPr pPr = paragraph.getCTP().getPPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(pPr);
    if (cssStyle != null) {
        cssStyle.addProperty(CSSStylePropertyConstants.WHITE_SPACE, "pre-wrap");
    }//from ww w.j  a  v  a  2s .c  o  m
    attributes = createStyleAttribute(cssStyle, attributes);

    // 2) create element
    startElement(P_ELEMENT, attributes);

    //To handle list items in paragraph
    if (itemContext != null) {
        startElement(SPAN_ELEMENT, attributes);
        String text = itemContext.getText();
        if (StringUtils.isNotEmpty(text)) {
            text = StringUtils.replaceNonUnicodeChars(text);
            text = text + "\u0020";
            SAXHelper.characters(contentHandler, StringEscapeUtils.escapeHtml(text));
        }
        endElement(SPAN_ELEMENT);
    }
    return null;
}

From source file:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected void visitRun(XWPFRun run, boolean pageNumber, String url, Object paragraphContainer)
        throws Exception {
    if (run.getParent() instanceof XWPFParagraph) {
        this.currentParagraph = (XWPFParagraph) run.getParent();
    }// w w  w . jav  a 2 s .c  om

    XWPFParagraph paragraph = run.getParagraph();
    // 1) create attributes

    // 1.1) Create "class" attributes.
    this.currentRunAttributes = createClassAttribute(paragraph.getStyleID());

    // 1.2) Create "style" attributes.
    CTRPr rPr = run.getCTR().getRPr();
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(rPr);
    if (cssStyle != null) {
        cssStyle.addProperty(CSSStylePropertyConstants.WHITE_SPACE, "pre-wrap");
    }
    this.currentRunAttributes = createStyleAttribute(cssStyle, currentRunAttributes);

    if (url != null) {
        // url is not null, generate a HTML a.
        AttributesImpl hyperlinkAttributes = new AttributesImpl();
        SAXHelper.addAttrValue(hyperlinkAttributes, HREF_ATTR, url);
        startElement(A_ELEMENT, hyperlinkAttributes);
    }

    super.visitRun(run, pageNumber, url, paragraphContainer);

    if (url != null) {
        // url is not null, close the HTML a.
        // TODO : for the moment generate space to be ensure that a has some content.
        characters(" ");
        endElement(A_ELEMENT);
    }
    this.currentRunAttributes = null;
    this.currentParagraph = null;
}

From source file:kz.service.DocumentReader.java

public static String readDocxFile(String fileName) {

    try {//from  w  ww  . j a v a  2  s.c  o m
        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:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.XWPFWordExtractorDecorator.java

License:Apache License

private void extractParagraph(XWPFParagraph paragraph, XHTMLContentHandler xhtml)
        throws SAXException, XmlException, IOException {
    // If this paragraph is actually a whole new section, then
    // it could have its own headers and footers
    // Check and handle if so
    XWPFHeaderFooterPolicy headerFooterPolicy = null;
    if (paragraph.getCTP().getPPr() != null) {
        CTSectPr ctSectPr = paragraph.getCTP().getPPr().getSectPr();
        if (ctSectPr != null) {
            headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
            extractHeaders(xhtml, headerFooterPolicy);
        }/* w ww . ja  v a 2 s .  c  o m*/
    }

    // Is this a paragraph, or a heading?
    String tag = "p";
    String styleClass = null;
    if (paragraph.getStyleID() != null) {
        XWPFStyle style = styles.getStyle(paragraph.getStyleID());

        if (style != null && style.getName() != null) {
            TagAndStyle tas = WordExtractor.buildParagraphTagAndStyle(style.getName(),
                    paragraph.getPartType() == BodyType.TABLECELL);
            tag = tas.getTag();
            styleClass = tas.getStyleClass();
        }
    }

    if (styleClass == null) {
        xhtml.startElement(tag);
    } else {
        xhtml.startElement(tag, "class", styleClass);
    }

    // Output placeholder for any embedded docs:

    // TODO: replace w/ XPath/XQuery:
    for (XWPFRun run : paragraph.getRuns()) {
        XmlCursor c = run.getCTR().newCursor();
        c.selectPath("./*");
        while (c.toNextSelection()) {
            XmlObject o = c.getObject();
            if (o instanceof CTObject) {
                XmlCursor c2 = o.newCursor();
                c2.selectPath("./*");
                while (c2.toNextSelection()) {
                    XmlObject o2 = c2.getObject();

                    XmlObject embedAtt = o2.selectAttribute(new QName("Type"));
                    if (embedAtt != null && embedAtt.getDomNode().getNodeValue().equals("Embed")) {
                        // Type is "Embed"
                        XmlObject relIDAtt = o2.selectAttribute(new QName(
                                "http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id"));
                        if (relIDAtt != null) {
                            String relID = relIDAtt.getDomNode().getNodeValue();
                            AttributesImpl attributes = new AttributesImpl();
                            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
                            attributes.addAttribute("", "id", "id", "CDATA", relID);
                            xhtml.startElement("div", attributes);
                            xhtml.endElement("div");
                        }
                    }
                }
                c2.dispose();
            }
        }

        c.dispose();
    }

    // Attach bookmarks for the paragraph
    // (In future, we might put them in the right place, for now
    // we just put them in the correct paragraph)
    for (CTBookmark bookmark : paragraph.getCTP().getBookmarkStartList()) {
        xhtml.startElement("a", "name", bookmark.getName());
        xhtml.endElement("a");
    }

    TmpFormatting fmtg = new TmpFormatting(false, false);

    // Do the iruns
    for (IRunElement run : paragraph.getIRuns()) {
        if (run instanceof XWPFSDT) {
            fmtg = closeStyleTags(xhtml, fmtg);
            processSDTRun((XWPFSDT) run, xhtml);
            // for now, we're ignoring formatting in sdt
            // if you hit an sdt reset to false
            fmtg.setBold(false);
            fmtg.setItalic(false);
        } else {
            fmtg = processRun((XWPFRun) run, paragraph, xhtml, fmtg);
        }
    }
    closeStyleTags(xhtml, fmtg);

    // Now do any comments for the paragraph
    XWPFCommentsDecorator comments = new XWPFCommentsDecorator(paragraph, null);
    String commentText = comments.getCommentText();
    if (commentText != null && commentText.length() > 0) {
        xhtml.characters(commentText);
    }

    String footnameText = paragraph.getFootnoteText();
    if (footnameText != null && footnameText.length() > 0) {
        xhtml.characters(footnameText + "\n");
    }

    // Also extract any paragraphs embedded in text boxes:
    for (XmlObject embeddedParagraph : paragraph.getCTP().selectPath(
            "declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' declare namespace wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape' .//*/wps:txbx/w:txbxContent/w:p")) {
        extractParagraph(new XWPFParagraph(CTP.Factory.parse(embeddedParagraph.xmlText()), paragraph.getBody()),
                xhtml);
    }

    // Finish this paragraph
    xhtml.endElement(tag);

    if (headerFooterPolicy != null) {
        extractFooters(xhtml, headerFooterPolicy);
    }
}

From source file:offishell.task.Task.java

License:MIT License

/**
 * <p>//w  w w  .j  av  a 2  s.  c  om
 * ???
 * </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();
}