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

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFRun#setColor() . 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: TemplateValidationGenerator.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Inserts the given {@link TemplateValidationMessage} in a run next to the one of the given construct subject to the message.
 * 
 * @param message
 *            the {@link TemplateValidationMessage} to insert
 *            the error message to insert in a run
 * @param offset
 *            the offset in number of {@link XWPFRun} to insert after {@link TemplateValidationMessage#getLocation() message location}
 * @return the number of inserted {@link XWPFRun}
 */
private int addRunError(TemplateValidationMessage message, int offset) {
    int res = 0;

    if (message.getLocation().getParent() instanceof XWPFParagraph) {
        XWPFParagraph paragraph = (XWPFParagraph) message.getLocation().getParent();
        final int basePosition = paragraph.getRuns().indexOf(message.getLocation()) + offset;

        // separation run.
        res++;
        final String color = M2DocUtils.getColor(message.getLevel());
        XWPFRun newBlankRun = paragraph.insertNewRun(basePosition + res);
        newBlankRun.setText(BLANK_SEPARATOR);

        res++;
        final XWPFRun separationRun = paragraph.insertNewRun(basePosition + res);
        separationRun.setText(LOCATION_SEPARATOR);
        separationRun.setColor(color);
        separationRun.setFontSize(ERROR_MESSAGE_FONT_SIZE);
        final CTHighlight separationHighlight = separationRun.getCTR().getRPr().addNewHighlight();
        separationHighlight.setVal(STHighlightColor.LIGHT_GRAY);

        res++;
        final XWPFRun messageRun = paragraph.insertNewRun(basePosition + res);
        messageRun.setText(message.getMessage());
        messageRun.setColor(color);
        messageRun.setFontSize(ERROR_MESSAGE_FONT_SIZE);
        final CTHighlight messageHighlight = messageRun.getCTR().getRPr().addNewHighlight();
        messageHighlight.setVal(STHighlightColor.LIGHT_GRAY);
    }

    return res;
}
 
Example 3
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 4
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);
}