Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFRun#setBold()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFRun#setBold() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StyleUtils.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
/**
 * 重复样式
 * 
 * @param destRun
 *            新建的run
 * @param srcRun
 *            原始run
 */
public static void styleRun(XWPFRun destRun, XWPFRun srcRun) {
    if (null == destRun || null == srcRun) return;
    CTR ctr = srcRun.getCTR();
    if (ctr.isSetRPr() && ctr.getRPr().isSetRStyle()) {
        String val = ctr.getRPr().getRStyle().getVal();
        if (StringUtils.isNotBlank(val)) {
            CTRPr pr = destRun.getCTR().isSetRPr() ? destRun.getCTR().getRPr()
                    : destRun.getCTR().addNewRPr();
            CTString rStyle = pr.isSetRStyle() ? pr.getRStyle() : pr.addNewRStyle();
            rStyle.setVal(val);
        }
    }
    if (Boolean.TRUE.equals(srcRun.isBold())) destRun.setBold(srcRun.isBold());
    destRun.setColor(srcRun.getColor());
    // destRun.setCharacterSpacing(srcRun.getCharacterSpacing());
    if (StringUtils.isNotBlank(srcRun.getFontFamily()))
        destRun.setFontFamily(srcRun.getFontFamily());
    int fontSize = srcRun.getFontSize();
    if (-1 != fontSize) destRun.setFontSize(fontSize);
    if (Boolean.TRUE.equals(srcRun.isItalic())) destRun.setItalic(srcRun.isItalic());
    if (Boolean.TRUE.equals(srcRun.isStrikeThrough()))
        destRun.setStrikeThrough(srcRun.isStrikeThrough());
    destRun.setUnderline(srcRun.getUnderline());
}
 
Example 2
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Apply the given style to the given run. Background color is not taken into account here since it does not apply to runs.
 * 
 * @param run
 *            The run to style
 * @param style
 *            The style to apply, can be <code>null</code>
 */
private void applyMStyle(XWPFRun run, MStyle style) {
    if (style.getFontSize() != -1) {
        run.setFontSize(style.getFontSize());
    }
    if (style.getFontName() != null) {
        run.setFontFamily(style.getFontName());
    }
    if (style.getFontModifiers() != -1) {
        run.setBold((style.getFontModifiers() & MStyle.FONT_BOLD) != 0);
        run.setItalic((style.getFontModifiers() & MStyle.FONT_ITALIC) != 0);
        if ((style.getFontModifiers() & MStyle.FONT_UNDERLINE) != 0) {
            run.setUnderline(UnderlinePatterns.SINGLE);
        }
        run.setStrikeThrough((style.getFontModifiers() & MStyle.FONT_STRIKE_THROUGH) != 0);
    }
    if (style.getForegroundColor() != null) {
        run.setColor(hexColor(style.getForegroundColor()));
    }
    if (style.getBackgroundColor() != null) {
        final CTRPr ctrpr;
        if (run.getCTR().getRPr() != null) {
            ctrpr = run.getCTR().getRPr();
        } else {
            ctrpr = run.getCTR().addNewRPr();
        }
        final CTShd ctshd;
        if (ctrpr.getShd() != null) {
            ctshd = ctrpr.getShd();
        } else {
            ctshd = ctrpr.addNewShd();
        }
        ctshd.setVal(STShd.CLEAR);
        ctshd.setColor("auto");
        ctshd.setFill(hexColor(style.getBackgroundColor()));
    }
}
 
Example 3
Source File: BookmarkManager.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Inserts a reference to the given {@link CTBookmark} with the given text in the given {@link XWPFParagraph}.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph}
 * @param name
 *            the bookmark name
 * @param text
 *            the text
 * @return the {@link CTText} corresponding to the reference.
 */
private CTText insertPendingReference(XWPFParagraph paragraph, String name, String text) {
    final byte[] id = getReferenceID(name);
    final XWPFRun beginRun = paragraph.createRun();
    beginRun.getCTR().setRsidR(id);
    beginRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);

    final XWPFRun preservedRun = paragraph.createRun();
    preservedRun.getCTR().setRsidR(id);
    final CTText pgcttext = preservedRun.getCTR().addNewInstrText();
    pgcttext.setSpace(Space.PRESERVE);

    final XWPFRun separateRun = paragraph.createRun();
    separateRun.getCTR().setRsidR(id);
    separateRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);

    final XWPFRun textRun = paragraph.createRun();
    textRun.getCTR().setRsidR(id);
    textRun.getCTR().addNewRPr().addNewNoProof();
    textRun.setText(text);
    textRun.setBold(true);

    final XWPFRun endRun = paragraph.createRun();
    endRun.getCTR().setRsidR(id);
    endRun.getCTR().addNewFldChar().setFldCharType(STFldCharType.END);

    return pgcttext;
}
 
Example 4
Source File: SimpleTable.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void createSimpleTable() throws Exception {
    @SuppressWarnings("resource")
XWPFDocument doc = new XWPFDocument();

    XWPFTable table = doc.createTable(3, 3);

    table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");

    // table cells have a list of paragraphs; there is an initial
    // paragraph created when the cell is created. If you create a
    // paragraph in the document to put in the cell, it will also
    // appear in the document following the table, which is probably
    // not the desired result.
    XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setItalic(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    table.getRow(2).getCell(2).setText("only text");

    FileOutputStream out = new FileOutputStream("simpleTable.docx");
    doc.write(out);
    out.close();
}
 
Example 5
Source File: StyleUtils.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
/**
 * 设置run的样式
 * 
 * @param run
 * @param style
 */
public static void styleRun(XWPFRun run, Style style) {
    if (null == run || null == style) return;
    String color = style.getColor();
    String fontFamily = style.getFontFamily();
    int fontSize = style.getFontSize();
    Boolean bold = style.isBold();
    Boolean italic = style.isItalic();
    Boolean strike = style.isStrike();
    Boolean underLine = style.isUnderLine();
    Enum highlightColor = style.getHighlightColor();
    int twips = style.getCharacterSpacing();
    String vertAlign = style.getVertAlign();
    CTRPr pr = run.getCTR().isSetRPr() ? run.getCTR().getRPr() : run.getCTR().addNewRPr();
    if (StringUtils.isNotBlank(color)) {
        // run.setColor(color);
        // issue 326
        CTColor ctColor = pr.isSetColor() ? pr.getColor() : pr.addNewColor();
        ctColor.setVal(color);
        if (ctColor.isSetThemeColor()) ctColor.unsetThemeColor();
    }
    if (0 != fontSize) run.setFontSize(fontSize);
    if (StringUtils.isNotBlank(fontFamily)) {
        run.setFontFamily(fontFamily);
        CTFonts fonts = pr.isSetRFonts() ? pr.getRFonts() : pr.addNewRFonts();
        fonts.setAscii(fontFamily);
        fonts.setHAnsi(fontFamily);
        fonts.setCs(fontFamily);
        fonts.setEastAsia(fontFamily);
    }
    if (null != highlightColor) {
        CTHighlight highlight = pr.isSetHighlight() ? pr.getHighlight() : pr.addNewHighlight();
        STHighlightColor hColor = highlight.xgetVal();
        if (hColor == null) {
            hColor = STHighlightColor.Factory.newInstance();
        }
        STHighlightColor.Enum val = STHighlightColor.Enum.forString(highlightColor.toString());
        if (val != null) {
            hColor.setStringValue(val.toString());
            highlight.xsetVal(hColor);
        }
    }
    if (null != bold) run.setBold(bold);
    if (null != italic) run.setItalic(italic);
    if (null != strike) run.setStrikeThrough(strike);
    if (Boolean.TRUE.equals(underLine)) {
        run.setUnderline(UnderlinePatterns.SINGLE);
    }
    // in twentieths of a point
    if (0 != twips) run.setCharacterSpacing(20*twips);
    if (StringUtils.isNotBlank(vertAlign)) {
        run.setVerticalAlignment(vertAlign);
    }
}
 
Example 6
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Set the given message to the given {@link XWPFRun}.
 * 
 * @param run
 *            the {@link XWPFRun}
 * @param level
 *            the {@link ValidationMessageLevel}
 * @param message
 *            the message
 * @return the {@link TemplateValidationMessage}
 */
public static TemplateValidationMessage setRunMessage(XWPFRun run, ValidationMessageLevel level, String message) {
    run.setText(message);
    run.setBold(true);
    run.setColor(getColor(level));

    return new TemplateValidationMessage(level, message, run);
}