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

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

Introduction

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

Prototype

@Internal
    public CTP getCTP() 

Source Link

Usage

From source file:de.knowwe.include.export.ListExporter.java

License:Open Source License

public void exportItem(Section<DashTreeElement> section, BigInteger numID, DocumentBuilder builder)
        throws ExportException {
    String text = section.getText().trim();
    int depth = 0;
    while (depth < text.length() && "#*".indexOf(text.charAt(depth)) >= 0) {
        depth++;/*from   w ww.  ja v a2  s. c  om*/
    }

    XWPFParagraph paragraph = builder.getNewParagraph(Style.list);
    paragraph.setNumID(numID);
    paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(depth - 1));
    ListBuilder listBuilder = new ListBuilder(builder);
    listBuilder.export(Sections.successor(section, DashTreeElementContent.class));
    listBuilder.closeParagraph();
}

From source file:de.knowwe.include.export.TodoExporter.java

License:Open Source License

@Override
public void export(Section<DefaultMarkupType> section, DocumentBuilder manager) throws ExportException {
    // preformatted code: make each line a paragraph
    String[] lines = Strings.trim(section.getText()).split("\n\r?");
    for (String line : lines) {
        XWPFParagraph paragraph = manager.getNewParagraph(DocumentBuilder.Style.code);
        CTR ctr = paragraph.getCTP().addNewR();
        CTRPr ctrPr = ctr.addNewRPr();//from ww  w.ja  va 2  s. c o m
        CTHighlight ctHighlight = CTHighlight.Factory.newInstance();
        ctHighlight.setVal(getUserColor(section));
        ctrPr.setHighlight(ctHighlight);
        ctr.addNewT().setStringValue(line + "\n\r");
        manager.closeParagraph();
    }
}

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

License:Open Source License

public CTParaRPr getCTParaRPr(XWPFParagraph paragraph) {
    return paragraph.getCTP().getPPr().getRPr();
}

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

License:Open Source License

public CTPPr getCTPPr(XWPFParagraph paragraph) {
    return paragraph.getCTP().getPPr();
}

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

License:Open Source License

/**
 * Visit the given paragraph.//from  w w w. ja  va2 s  .c o m
 * 
 * @param paragraph
 * @param index
 * @param container
 * @throws Exception
 */
protected void visitParagraph(XWPFParagraph paragraph, int index, T container) throws Exception {
    if (isWordDocumentPartParsing()) {
        // header/footer is not parsing.
        // It's the word/document.xml which is parsing
        // test if the current paragraph define a <w:sectPr
        // to update the header/footer declared in the <w:headerReference/<w:footerReference
        masterPageManager.update(paragraph.getCTP());
    }
    if (pageBreakOnNextParagraph) {
        pageBreak();
    }
    this.pageBreakOnNextParagraph = false;

    ListItemContext itemContext = null;
    CTNumPr originalNumPr = stylesDocument.getParagraphNumPr(paragraph);
    CTNumPr numPr = getNumPr(originalNumPr);
    if (numPr != null) {
        // paragraph is a numbered/bullet list
        // see http://msdn.microsoft.com/en-us/library/office/ee922775%28v=office.14%29.aspx
        // - <w:p>
        // - <w:pPr>
        // <w:pStyle w:val="style0" />
        // - <w:numPr>
        // <w:ilvl w:val="0" />
        // <w:numId w:val="2" />
        // </w:numPr>

        // get numbering.xml/w:num
        /**
         * <w:num w:numId="2"> <w:abstractNumId w:val="1" /> </w:num>
         */
        XWPFNum num = getXWPFNum(numPr);
        if (num != null) {
            // get the abstractNum by usisng abstractNumId
            /**
             * <w:abstractNum w:abstractNumId="1"> <w:nsid w:val="3CBA6E67" /> <w:multiLevelType
             * w:val="hybridMultilevel" /> <w:tmpl w:val="7416D4FA" /> - <w:lvl w:ilvl="0" w:tplc="040C0001">
             * <w:start w:val="1" /> <w:numFmt w:val="bullet" /> <w:lvlText w:val="o" /> <w:lvlJc w:val="left" /> -
             * <w:pPr> <w:ind w:left="720" w:hanging="360" /> </w:pPr> - <w:rPr> <w:rFonts w:ascii="Symbol"
             * w:hAnsi="Symbol" w:hint="default" /> </w:rPr> </w:lvl>
             */
            XWPFAbstractNum abstractNum = getXWPFAbstractNum(num);

            // get the <w:lvl by using abstractNum and numPr level
            /**
             * <w:num w:numId="2"> <w:abstractNumId w:val="1" /> </w:num>
             */
            CTDecimalNumber ilvl = numPr.getIlvl();
            int level = ilvl != null ? ilvl.getVal().intValue() : 0;

            CTLvl lvl = abstractNum.getAbstractNum().getLvlArray(level);
            if (lvl != null) {
                ListContext listContext = getListContext(originalNumPr.getNumId().getVal().intValue());
                itemContext = listContext.addItem(lvl);
            }
        }

    }
    T paragraphContainer = startVisitParagraph(paragraph, itemContext, container);
    visitParagraphBody(paragraph, index, paragraphContainer);
    endVisitParagraph(paragraph, container, paragraphContainer);
}

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

License:Open Source License

protected void visitParagraphBody(XWPFParagraph paragraph, int index, T paragraphContainer) throws Exception {
    List<XWPFRun> runs = paragraph.getRuns();
    if (runs.isEmpty()) {
        // a new line must be generated if :
        // - there is next paragraph/table
        // - if the body is a cell (with none vMerge) and contains just this paragraph
        if (isAddNewLine(paragraph, index)) {
            visitEmptyRun(paragraphContainer);
        }/* w ww.  j  a v  a2s.c  o m*/

        // sometimes, POI tells that run is empty
        // but it can be have w:r in the w:pPr
        // <w:p><w:pPr .. <w:r> => See the header1.xml of DocxBig.docx ,
        // => test if it exist w:r
        // CTP p = paragraph.getCTP();
        // CTPPr pPr = p.getPPr();
        // if (pPr != null) {
        // XmlObject[] wRuns =
        // pPr.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:r");
        // if (wRuns != null) {
        // for ( int i = 0; i < wRuns.length; i++ )
        // {
        // XmlObject o = wRuns[i];
        // o.getDomNode().getParentNode()
        // if (o instanceof CTR) {
        // System.err.println(wRuns[i]);
        // }
        //
        // }
        // }
        // }
        // //XmlObject[] t =
        // o.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
        // //paragraph.getCTP().get
    } else {
        // Loop for each element of <w:r, w:fldSimple
        // to keep the order of those elements.
        visitRuns(paragraph, paragraphContainer);
    }

    // Page Break
    // Cannot use paragraph.isPageBreak() because it throws NPE because
    // pageBreak.getVal() can be null.
    CTPPr ppr = paragraph.getCTP().getPPr();
    if (ppr != null) {
        if (ppr.isSetPageBreakBefore()) {
            CTOnOff pageBreak = ppr.getPageBreakBefore();
            if (pageBreak != null
                    && (pageBreak.getVal() == null || pageBreak.getVal().intValue() == STOnOff.INT_TRUE)) {
                pageBreak();
            }
        }
    }
}

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

License:Open Source License

private void visitRuns(XWPFParagraph paragraph, T paragraphContainer) throws Exception {
    boolean fldCharTypeParsing = false;
    boolean pageNumber = false;
    String url = null;/* w  w  w.  j  a v a 2  s .c  o m*/
    List<XmlObject> rListAfterSeparate = null;

    CTP ctp = paragraph.getCTP();
    XmlCursor c = ctp.newCursor();
    c.selectPath("child::*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();
        if (o instanceof CTR) {
            /*
             * Test if it's : <w:r> <w:rPr /> <w:fldChar w:fldCharType="begin" /> </w:r>
             */
            CTR r = (CTR) o;
            STFldCharType.Enum fldCharType = XWPFRunHelper.getFldCharType(r);
            if (fldCharType != null) {
                if (fldCharType.equals(STFldCharType.BEGIN)) {
                    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
                    fldCharTypeParsing = true;
                    rListAfterSeparate = new ArrayList<XmlObject>();
                    pageNumber = false;
                    url = null;
                } else if (fldCharType.equals(STFldCharType.END)) {

                    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
                    fldCharTypeParsing = false;
                    rListAfterSeparate = null;
                    pageNumber = false;
                    processingTotalPageCountField = false;
                    url = null;
                }
            } else {
                if (fldCharTypeParsing) {
                    String instrText = XWPFRunHelper.getInstrText(r);
                    if (instrText != null) {
                        if (StringUtils.isNotEmpty(instrText)) {
                            // test if it's <w:r><w:instrText>PAGE</w:instrText></w:r>
                            boolean instrTextPage = XWPFRunHelper.isInstrTextPage(instrText);
                            if (!instrTextPage) {

                                // test if it's <w:r><w:instrText>NUMPAGES</w:instrText></w:r>
                                processingTotalPageCountField = XWPFRunHelper.isInstrTextNumpages(instrText);
                                if (!totalPageFieldUsed) {
                                    totalPageFieldUsed = true;
                                }

                                // test if it's <w:instrText>HYPERLINK
                                // "http://code.google.com/p/xdocrepor"</w:instrText>
                                String instrTextHyperlink = XWPFRunHelper.getInstrTextHyperlink(instrText);
                                if (instrTextHyperlink != null) {
                                    // test if it's <w:instrText>HYPERLINK \l _Toc29586</w:instrText>
                                    if (instrTextHyperlink.startsWith("\\l ")) {
                                        url = "#" + instrTextHyperlink.substring(3);
                                    } else {
                                        url = instrTextHyperlink;
                                    }
                                }
                            } else {
                                pageNumber = true;
                            }
                        }
                    } else {
                        rListAfterSeparate.add(r);
                    }
                } else {
                    XWPFRun run = new XWPFRun(r, paragraph);
                    visitRun(run, false, null, paragraphContainer);
                }
            }
        } else {
            if (fldCharTypeParsing) {
                rListAfterSeparate.add(o);
            } else {
                visitRun(paragraph, o, paragraphContainer);
            }
        }
    }
    c.dispose();
    process(paragraph, paragraphContainer, pageNumber, url, rListAfterSeparate);
    fldCharTypeParsing = false;
    rListAfterSeparate = null;
    pageNumber = false;
    url = null;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java

License:Open Source License

@Override
protected IITextContainer startVisitParagraph(XWPFParagraph docxParagraph, ListItemContext itemContext,
        IITextContainer pdfParentContainer) throws Exception {
    this.currentRunX = null;

    // create PDF paragraph
    StylableParagraph pdfParagraph = pdfDocument.createParagraph(pdfParentContainer);

    // indentation left
    Float indentationLeft = stylesDocument.getIndentationLeft(docxParagraph);
    if (indentationLeft != null) {
        pdfParagraph.setIndentationLeft(indentationLeft);
    }/*from  w w w.j  av  a 2s  .c o  m*/
    // indentation right
    Float indentationRight = stylesDocument.getIndentationRight(docxParagraph);
    if (indentationRight != null) {
        pdfParagraph.setIndentationRight(indentationRight);
    }
    // indentation first line
    Float indentationFirstLine = stylesDocument.getIndentationFirstLine(docxParagraph);
    if (indentationFirstLine != null) {
        pdfParagraph.setFirstLineIndent(indentationFirstLine);
    }
    // indentation hanging (remove first line)
    Float indentationHanging = stylesDocument.getIndentationHanging(docxParagraph);
    if (indentationHanging != null) {
        pdfParagraph.setFirstLineIndent(-indentationHanging);
    }

    // // spacing before
    Float spacingBefore = stylesDocument.getSpacingBefore(docxParagraph);
    if (spacingBefore != null) {
        pdfParagraph.setSpacingBefore(spacingBefore);
    }

    // spacing after
    Float spacingAfter = stylesDocument.getSpacingAfter(docxParagraph);
    if (spacingAfter != null) {
        pdfParagraph.setSpacingAfter(spacingAfter);
    }

    ParagraphLineSpacing lineSpacing = stylesDocument.getParagraphSpacing(docxParagraph);
    if (lineSpacing != null) {
        if (lineSpacing.getLeading() != null && lineSpacing.getMultipleLeading() != null) {
            pdfParagraph.setLeading(lineSpacing.getLeading(), lineSpacing.getMultipleLeading());
        } else {
            if (lineSpacing.getLeading() != null) {
                pdfParagraph.setLeading(lineSpacing.getLeading());
            }
            if (lineSpacing.getMultipleLeading() != null) {
                pdfParagraph.setMultipliedLeading(lineSpacing.getMultipleLeading());
            }
        }

    }

    // text-align
    ParagraphAlignment alignment = stylesDocument.getParagraphAlignment(docxParagraph);
    if (alignment != null) {
        switch (alignment) {
        case LEFT:
            pdfParagraph.setAlignment(Element.ALIGN_LEFT);
            break;
        case RIGHT:
            pdfParagraph.setAlignment(Element.ALIGN_RIGHT);
            break;
        case CENTER:
            pdfParagraph.setAlignment(Element.ALIGN_CENTER);
            break;
        case BOTH:
            pdfParagraph.setAlignment(Element.ALIGN_JUSTIFIED);
            break;
        default:
            break;
        }
    }

    // background-color
    Color backgroundColor = stylesDocument.getBackgroundColor(docxParagraph);
    if (backgroundColor != null) {
        pdfParagraph.setBackgroundColor(Converter.toBaseColor(backgroundColor));
    }

    // border
    CTBorder borderTop = stylesDocument.getBorderTop(docxParagraph);
    pdfParagraph.setBorder(borderTop, Rectangle.TOP);

    CTBorder borderBottom = stylesDocument.getBorderBottom(docxParagraph);
    pdfParagraph.setBorder(borderBottom, Rectangle.BOTTOM);

    CTBorder borderLeft = stylesDocument.getBorderLeft(docxParagraph);
    pdfParagraph.setBorder(borderLeft, Rectangle.LEFT);

    CTBorder borderRight = stylesDocument.getBorderRight(docxParagraph);
    pdfParagraph.setBorder(borderRight, Rectangle.RIGHT);

    if (itemContext != null) {
        CTLvl lvl = itemContext.getLvl();
        CTPPr lvlPPr = lvl.getPPr();
        if (lvlPPr != null) {
            if (ParagraphIndentationLeftValueProvider.INSTANCE
                    .getValue(docxParagraph.getCTP().getPPr()) == null) {

                // search the indentation from the level properties only if
                // paragraph has not override it
                // see
                // https://code.google.com/p/xdocreport/issues/detail?id=239
                Float indLeft = ParagraphIndentationLeftValueProvider.INSTANCE.getValue(lvlPPr);
                if (indLeft != null) {
                    pdfParagraph.setIndentationLeft(indLeft);
                }
            }
            if (ParagraphIndentationHangingValueProvider.INSTANCE
                    .getValue(docxParagraph.getCTP().getPPr()) == null) {
                // search the hanging from the level properties only if
                // paragraph has not override it
                // see
                // https://code.google.com/p/xdocreport/issues/detail?id=239
                Float hanging = stylesDocument.getIndentationHanging(lvlPPr);
                if (hanging != null) {
                    pdfParagraph.setFirstLineIndent(-hanging);
                }
            }
        }
        CTRPr lvlRPr = lvl.getRPr();
        if (lvlRPr != null) {
            // Font family
            String listItemFontFamily = stylesDocument.getFontFamilyAscii(lvlRPr);

            // Get font size
            Float listItemFontSize = stylesDocument.getFontSize(lvlRPr);

            // Get font style
            int listItemFontStyle = Font.NORMAL;
            Boolean bold = stylesDocument.getFontStyleBold(lvlRPr);
            if (bold != null && bold) {
                listItemFontStyle |= Font.BOLD;
            }
            Boolean italic = stylesDocument.getFontStyleItalic(lvlRPr);
            if (italic != null && italic) {
                listItemFontStyle |= Font.ITALIC;
            }
            Boolean strike = stylesDocument.getFontStyleStrike(lvlRPr);
            if (strike != null && strike) {
                listItemFontStyle |= Font.STRIKETHRU;
            }

            // Font color
            Color listItemFontColor = stylesDocument.getFontColor(lvlRPr);

            pdfParagraph.setListItemFontFamily(listItemFontFamily);
            pdfParagraph.setListItemFontSize(listItemFontSize);
            pdfParagraph.setListItemFontStyle(listItemFontStyle);
            pdfParagraph.setListItemFontColor(Converter.toBaseColor(listItemFontColor));

        }
        pdfParagraph.setListItemText(itemContext.getText());
    }
    return pdfParagraph;
}

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");
    }//w w  w  . ja  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.univrouen.poste.services.WordParser.java

License:Apache License

public void modifyWord(InputStream docx, Map<String, String> textMap, OutputStream out) {
    try {/*  w ww . j  a  v a2  s. c om*/
        XWPFDocument doc = new XWPFDocument(OPCPackage.open(docx));

        // tentative avec les noms {{}}
        for (XWPFParagraph p : doc.getParagraphs()) {

            for (CTBookmark bookmark : p.getCTP().getBookmarkStartList()) {
                log.trace(bookmark.getName());
                for (String key : textMap.keySet()) {
                    String cleanKey = StringUtils.stripAccents(key);
                    cleanKey = cleanKey.replaceAll(" ", "_");
                    cleanKey = cleanKey.replaceAll("\\W", "");
                    if (bookmark.getName().equalsIgnoreCase(cleanKey)) {
                        Node nextNode = bookmark.getDomNode().getNextSibling();
                        while (nextNode != null && nextNode.getNodeName() != null
                                && !(nextNode.getNodeName().contains("bookmarkEnd"))) {
                            p.getCTP().getDomNode().removeChild(nextNode);
                            nextNode = bookmark.getDomNode().getNextSibling();
                        }
                        XWPFRun run = p.createRun();
                        run.setText(textMap.get(key));
                        p.getCTP().getDomNode().insertBefore(run.getCTR().getDomNode(), nextNode);
                    }
                }
            }
        }

        doc.write(out);
    } catch (Exception e) {
        log.error("Pb durant la modification du document word", e);
    }

}