Example usage for org.apache.poi.xwpf.usermodel XWPFRun getCTR

List of usage examples for org.apache.poi.xwpf.usermodel XWPFRun getCTR

Introduction

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

Prototype

@Internal
public CTR getCTR() 

Source Link

Document

Get the currently used CTR object

Usage

From source file:cn.afterturn.easypoi.word.entity.MyXWPFDocument.java

License:Apache License

public void createPicture(XWPFRun run, String blipId, int id, int width, int height) {
    final int emu = 9525;
    width *= emu;/*  w  w w .ja v  a 2 s.  c o  m*/
    height *= emu;
    CTInline inline = run.getCTR().addNewDrawing().addNewInline();
    String picXml = String.format(PICXML, id, blipId, width, height);
    XmlToken xmlToken = null;
    try {
        xmlToken = XmlToken.Factory.parse(picXml);
    } catch (XmlException xe) {
        LOGGER.error(xe.getMessage(), xe);
    }
    inline.set(xmlToken);

    inline.setDistT(0);
    inline.setDistB(0);
    inline.setDistL(0);
    inline.setDistR(0);

    CTPositiveSize2D extent = inline.addNewExtent();
    extent.setCx(width);
    extent.setCy(height);

    CTNonVisualDrawingProps docPr = inline.addNewDocPr();
    docPr.setId(id);
    docPr.setName("Picture " + id);
    docPr.setDescr("Generated");
}

From source file:com.bxf.hradmin.testgen.service.impl.DocxTestGenerator.java

License:Open Source License

private XWPFRun createRun(XWPFParagraph paragraph) {
    //  & ?//from  ww  w.ja  v  a 2s.  c  om
    XWPFRun run = paragraph.createRun();
    run.setFontSize(fontSize);
    CTFonts rFonts = run.getCTR().getRPr().addNewRFonts();
    rFonts.setEastAsia(fontEastAsiaFamily);
    rFonts.setAscii(fontAsciiFamily);
    return run;
}

From source file:com.deepoove.poi.NiceXWPFDocument.java

License:Apache License

public void addPicture(String blipId, int id, int width, int height, XWPFRun run) {
    final int EMU = 9525;
    width *= EMU;//from  www  . j  a v  a  2  s .  c  om
    height *= EMU;
    // String blipId =
    // getAllPictures().get(id).getPackageRelationship().getId();

    CTInline inline = run.getCTR().addNewDrawing().addNewInline();

    String picXml = "" + "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
            + "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
            + "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
            + "         <pic:nvPicPr>" + "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>"
            + "            <pic:cNvPicPr/>" + "         </pic:nvPicPr>" + "         <pic:blipFill>"
            + "            <a:blip r:embed=\"" + blipId
            + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
            + "            <a:stretch>" + "               <a:fillRect/>" + "            </a:stretch>"
            + "         </pic:blipFill>" + "         <pic:spPr>" + "            <a:xfrm>"
            + "               <a:off x=\"0\" y=\"0\"/>" + "               <a:ext cx=\"" + width + "\" cy=\""
            + height + "\"/>" + "            </a:xfrm>" + "            <a:prstGeom prst=\"rect\">"
            + "               <a:avLst/>" + "            </a:prstGeom>" + "         </pic:spPr>"
            + "      </pic:pic>" + "   </a:graphicData>" + "</a:graphic>";

    // CTGraphicalObjectData graphicData =
    // inline.addNewGraphic().addNewGraphicData();
    XmlToken xmlToken = null;
    try {
        xmlToken = XmlToken.Factory.parse(picXml);
    } catch (XmlException xe) {
        xe.printStackTrace();
    }
    inline.set(xmlToken);
    // graphicData.set(xmlToken);

    inline.setDistT(0);
    inline.setDistB(0);
    inline.setDistL(0);
    inline.setDistR(0);

    CTPositiveSize2D extent = inline.addNewExtent();
    extent.setCx(width);
    extent.setCy(height);

    CTNonVisualDrawingProps docPr = inline.addNewDocPr();
    docPr.setId(id);
    docPr.setName("Picture " + id);
    docPr.setDescr("Generated");
}

From source file:com.qihang.winter.poi.word.entity.MyXWPFDocument.java

License:Apache License

public void createPicture(XWPFRun run, String blipId, int id, int width, int height) {
    final int EMU = 9525;
    width *= EMU;//from   w  w w. j ava2s .c o m
    height *= EMU;
    CTInline inline = run.getCTR().addNewDrawing().addNewInline();
    String picXml = String.format(PICXML, id, blipId, width, height);
    XmlToken xmlToken = null;
    try {
        xmlToken = XmlToken.Factory.parse(picXml);
    } catch (XmlException xe) {
        LOGGER.error(xe.getMessage(), xe.fillInStackTrace());
    }
    inline.set(xmlToken);

    inline.setDistT(0);
    inline.setDistB(0);
    inline.setDistL(0);
    inline.setDistR(0);

    CTPositiveSize2D extent = inline.addNewExtent();
    extent.setCx(width);
    extent.setCy(height);

    CTNonVisualDrawingProps docPr = inline.addNewDocPr();
    docPr.setId(id);
    docPr.setName("Picture " + id);
    docPr.setDescr("Generated");
}

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

License:Open Source License

@Override
public void export(Section<LinkType> section, DocumentBuilder manager) throws ExportException {
    // find id for our footnote
    String refNumber = LinkType.getLink(section);

    // add reference to text
    XWPFRun run = manager.getParagraph().createRun();
    run.setSubscript(VerticalAlign.SUPERSCRIPT);
    Article article = section.getArticle();
    String label = getForcedLabel(manager, article, refNumber);
    if (label == null) {
        // add reference to footnote
        CTFtnEdnRef textRef = run.getCTR().addNewFootnoteReference();
        BigInteger noteId = FootnoteExporter.getFootnoteID(manager, article, refNumber);
        textRef.setId(noteId);//www .ja v  a 2  s.c  om
        addForcedLabel(article, refNumber, String.valueOf(noteId));
    } else {
        // otherwise only add text in similar style
        run.setText(label);
        addForcedLabel(article, refNumber, label);
    }
}

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

License:Open Source License

public CTRPr getRPr(XWPFRun run) {
    return run.getCTR().getRPr();
}

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

License:Open Source License

protected void visitRun(XWPFRun run, boolean pageNumber, String url, T paragraphContainer) throws Exception

{
    CTR ctr = run.getCTR();
    CTRPr rPr = ctr.getRPr();/*  ww w .  j ava  2 s. c  om*/
    boolean hasTexStyles = rPr != null && (rPr.getHighlight() != null || rPr.getStrike() != null
            || rPr.getDstrike() != null || rPr.getVertAlign() != null);
    StringBuilder text = new StringBuilder();

    // Loop for each element of <w:run text, tab, image etc
    // to keep the order of thoses elements.
    XmlCursor c = ctr.newCursor();
    c.selectPath("./*");
    while (c.toNextSelection()) {
        XmlObject o = c.getObject();

        if (o instanceof CTText) {
            CTText ctText = (CTText) o;
            String tagName = o.getDomNode().getNodeName();
            // Field Codes (w:instrText, defined in spec sec. 17.16.23)
            // come up as instances of CTText, but we don't want them
            // in the normal text output
            if ("w:instrText".equals(tagName)) {

            } else {
                if (hasTexStyles) {
                    text.append(ctText.getStringValue());
                } else {
                    visitText(ctText, pageNumber, paragraphContainer);
                }
            }
        } else if (o instanceof CTPTab) {
            visitTab((CTPTab) o, paragraphContainer);
        } else if (o instanceof CTBr) {
            visitBR((CTBr) o, paragraphContainer);
        } else if (o instanceof CTEmpty) {
            // Some inline text elements get returned not as
            // themselves, but as CTEmpty, owing to some odd
            // definitions around line 5642 of the XSDs
            // This bit works around it, and replicates the above
            // rules for that case
            String tagName = o.getDomNode().getNodeName();
            if ("w:tab".equals(tagName)) {
                CTTabs tabs = stylesDocument.getParagraphTabs(run.getParagraph());
                visitTabs(tabs, paragraphContainer);
            }
            if ("w:br".equals(tagName)) {
                visitBR(null, paragraphContainer);
            }
            if ("w:cr".equals(tagName)) {
                visitBR(null, paragraphContainer);
            }
        } else if (o instanceof CTDrawing) {
            visitDrawing((CTDrawing) o, paragraphContainer);
        }
    }
    if (hasTexStyles && StringUtils.isNotEmpty(text.toString())) {
        visitStyleText(run, text.toString());
    }
    c.dispose();
}

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();
    }//from  w ww. j a  v  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:fr.opensagres.poi.xwpf.converter.xhtml.internal.XHTMLMapper.java

License:Open Source License

@Override
protected void visitStyleText(XWPFRun run, String text) throws Exception {
    if (run.getFontFamily() == null) {
        run.setFontFamily(getStylesDocument().getFontFamilyAscii(run));
    }//from w ww  . j a  v a  2 s. c  o m

    if (run.getFontSize() <= 0) {
        run.setFontSize(getStylesDocument().getFontSize(run).intValue());
    }

    CTRPr rPr = run.getCTR().getRPr();

    // 1) create attributes

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

    // 1.2) Create "style" attributes.
    CSSStyle cssStyle = getStylesDocument().createCSSStyle(rPr);
    if (cssStyle != null) {
        Color color = RunTextHighlightingValueProvider.INSTANCE.getValue(rPr, getStylesDocument());
        if (color != null)
            cssStyle.addProperty(CSSStylePropertyConstants.BACKGROUND_COLOR, StringUtils.toHexString(color));
        if (Boolean.TRUE.equals(RunFontStyleStrikeValueProvider.INSTANCE.getValue(rPr, getStylesDocument()))
                || rPr.getDstrike() != null)
            cssStyle.addProperty("text-decoration", "line-through");
        if (rPr.getVertAlign() != null) {
            int align = rPr.getVertAlign().getVal().intValue();
            if (STVerticalAlignRun.INT_SUPERSCRIPT == align) {
                cssStyle.addProperty("vertical-align", "super");
            } else if (STVerticalAlignRun.INT_SUBSCRIPT == align) {
                cssStyle.addProperty("vertical-align", "sub");
            }
        }
    }
    runAttributes = createStyleAttribute(cssStyle, runAttributes);
    if (runAttributes != null) {
        startElement(SPAN_ELEMENT, runAttributes);
    }
    if (StringUtils.isNotEmpty(text)) {
        // Escape with HTML characters
        characters(StringEscapeUtils.escapeHtml(text));
    }
    if (runAttributes != null) {
        endElement(SPAN_ELEMENT);
    }
}

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  w w .ja  v a 2  s  . c  o m*/
        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);
    }

}