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

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

Introduction

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

Prototype

public String getFontFamily() 

Source Link

Document

Gets the fonts which shall be used to display the text contents of this run.

Usage

From source file:apachepoitest.DocumentPropertyChecker.java

public static Boolean checkIfRunHasProperty(XWPFRun r, String property, String value) {
    try {//  w w w.  jav a 2  s . co  m
        switch (property) {
        case "COLOR":
            return r.getColor().equals(value);
        case "FONT FAMILY":
            return r.getFontFamily().equalsIgnoreCase(value);
        case "FONT SIZE":
            return r.getFontSize() == Integer.parseInt(value);
        case "BOLD":
            return r.isBold() == Boolean.valueOf(value);
        case "ITALIC":
            return r.isItalic() == Boolean.valueOf(value);
        case "STRIKETHROUGH":
            return r.isStrike() == Boolean.valueOf(value);
        default:
            System.out.println("Property " + property + " does not exist!");
            return false;
        }
    } catch (NullPointerException e) {
        return false;
    }
}

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphElementProperties(List<XWPFRun> rl) {
    System.out.println("\nELEMENTS: ");
    int counter = 1;
    for (XWPFRun r : rl) {
        if (r.toString().trim().length() > 0) {
            System.out.println("#" + counter++ + ": " + r.toString());
        } else {//from ww  w  .j a  v  a  2  s .c o  m
            //Ignore spaces, uncomment to display spaces and comment out "continue"
            //System.out.println("#" + counter++ + ": <SPACE>");
            continue;
        }
        if (r.getColor() != null) {
            System.out.println("COLOR: " + r.getColor());
        }
        if (r.getFontFamily() != null) {
            System.out.println("FONT: " + r.getFontFamily());
        }
        if (r.getFontSize() > 0) {
            System.out.println("FONT SIZE: " + r.getFontSize());
        }
        if (r.getPictureText().length() > 0) {
            System.out.println("PIC TEXT: " + r.getPictureText());
        }
        if (r.getTextPosition() > 0) {
            System.out.println("TEXT POS: " + r.getTextPosition());
        }
        if (r.isBold()) {
            System.out.println("BOLD: " + r.isBold());
        }
        if (r.isItalic()) {
            System.out.println("ITALIC: " + r.isItalic());
        }
        if (r.isStrike()) {
            System.out.println("STRIKETHROUGH: " + r.isStrike());
        }
        if (!r.getUnderline().toString().equals("NONE")) {
            System.out.println("UNDERLINE: " + r.getUnderline().toString());
        }
        if (!r.getSubscript().toString().equals("BASELINE")) {
            System.out.println("Subscript: " + r.getSubscript().toString());
        }
        System.out.println("");
    }
}

From source file:com.deepoove.poi.resolver.TemplateResolver.java

License:Apache License

private static void styleRun(XWPFRun destRun, XWPFRun srcRun) {
    if (null == destRun || null == srcRun)
        return;/* w ww .j av  a 2 s  . c om*/
    destRun.setBold(srcRun.isBold());
    destRun.setColor(srcRun.getColor());
    destRun.setFontFamily(srcRun.getFontFamily());
    int fontSize = srcRun.getFontSize();
    if (-1 != fontSize)
        destRun.setFontSize(fontSize);
    destRun.setItalic(srcRun.isItalic());
    destRun.setStrike(srcRun.isStrike());
    destRun.setUnderline(srcRun.getUnderline());
}

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 w  w.  ja  va  2 s  .  c om

    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:offishell.msoffice.WordTestHelper.java

License:MIT License

/**
 * <p>//from  ww w  .j a v a  2 s  .  c  o  m
 * Assertion helper.
 * </p>
 * 
 * @param para
 * @param value
 */
public default void checkRun(XWPFRun run, Assertion value) {
    assert value.of(run.text());
    assert value.of(run.getFontFamily());
    assert value.of(run.getFontSize());
    assert value.of(run.isBold());
    assert value.of(run.isCapitalized());
    assert value.of(run.isDoubleStrikeThrough());
    assert value.of(run.isEmbossed());
    assert value.of(run.isHighlighted());
    assert value.of(run.isImprinted());
}

From source file:offishell.word.WordHeleper.java

License:MIT License

/**
 * <p>/*ww  w .  j av  a 2s . c o  m*/
 * Helper method to clone {@link XWPFRun}.
 * </p>
 * 
 * @param in
 * @param out
 * @param model
 */
public static void copy(XWPFRun in, XWPFRun out, UnaryOperator<String> converter) {
    // copy
    out.setBold(in.isBold());
    out.setCapitalized(in.isCapitalized());
    out.setCharacterSpacing(in.getCharacterSpacing());
    out.setColor(in.getColor());
    out.setDoubleStrikethrough(in.isDoubleStrikeThrough());
    out.setEmbossed(in.isEmbossed());
    out.setFontFamily(in.getFontFamily());
    out.setFontSize(in.getFontSize());
    out.setImprinted(in.isImprinted());
    out.setItalic(in.isItalic());
    out.setKerning(in.getKerning());
    out.setShadow(in.isShadowed());
    out.setSmallCaps(in.isSmallCaps());
    out.setStrikeThrough(in.isStrikeThrough());
    out.setVerticalAlignment(out.getVerticalAlignment().toString());
    out.setTextPosition(in.getTextPosition());
    out.setUnderline(in.getUnderline());

    // copy context
    CTR inCTR = in.getCTR();
    CTRPr inPR = inCTR.getRPr();
    CTR outCTR = out.getCTR();
    CTRPr outPR = outCTR.isSetRPr() ? outCTR.getRPr() : outCTR.addNewRPr();
    outPR.set(inCTR.getRPr());
    out.setVerticalAlignment(
            inPR == null || inPR.getVertAlign() == null ? "baseline" : inPR.getVertAlign().toString());

    // // copy tab
    // CTEmpty[] tabs = inCTR.getTabArray();
    //
    // if (tabs.length != 0) {
    // out.addTab();
    // }
    outCTR.setAnnotationRefArray(inCTR.getAnnotationRefList().toArray(CTEmpty[]::new));
    outCTR.setBrArray(inCTR.getBrList().toArray(CTBr[]::new));
    outCTR.setCommentReferenceArray(inCTR.getCommentReferenceList().toArray(CTMarkup[]::new));
    outCTR.setContinuationSeparatorArray(inCTR.getContinuationSeparatorList().toArray(CTEmpty[]::new));
    outCTR.setCrArray(inCTR.getCrList().toArray(CTEmpty[]::new));
    outCTR.setDelInstrTextArray(inCTR.getDelInstrTextList().toArray(CTText[]::new));
    outCTR.setDrawingArray(inCTR.getDrawingList().toArray(CTDrawing[]::new));
    outCTR.setEndnoteRefArray(inCTR.getEndnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setFldCharArray(inCTR.getFldCharList().toArray(CTFldChar[]::new));
    outCTR.setFootnoteRefArray(inCTR.getFootnoteRefList().toArray(CTEmpty[]::new));
    outCTR.setInstrTextArray(inCTR.getInstrTextList().toArray(CTText[]::new));
    outCTR.setLastRenderedPageBreakArray(inCTR.getLastRenderedPageBreakList().toArray(CTEmpty[]::new));
    outCTR.setObjectArray(inCTR.getObjectList().toArray(CTObject[]::new));
    outCTR.setPictArray(inCTR.getPictList().toArray(CTPicture[]::new));
    outCTR.setPtabArray(inCTR.getPtabList().toArray(CTPTab[]::new));
    outCTR.setSymArray(inCTR.getSymList().toArray(CTSym[]::new));
    outCTR.setTabArray(inCTR.getTabList().toArray(CTEmpty[]::new));

    // copy image
    for (XWPFPicture inPicture : in.getEmbeddedPictures()) {
        try {
            XWPFPictureData inData = inPicture.getPictureData();
            String outId = out.getDocument().addPictureData(new ByteArrayInputStream(inData.getData()),
                    inData.getPictureType());

            select(CTBlip.class, outCTR).to(blip -> blip.setEmbed(outId));
        } catch (Exception e) {
            throw I.quiet(e);
        }
    }

    // copy text
    write(out, converter.apply(in.text()));
}

From source file:org.articleEditor.insertContent.POIDocxReader.java

License:Apache License

protected void processRun(XWPFRun run) throws BadLocationException {
    charAttrs = new SimpleAttributeSet();

    if (run.getFontSize() > 0) {
        int size = run.getFontSize();
        StyleConstants.setFontSize(charAttrs, size);
    }/*from www . j a va2s . c o m*/
    StyleConstants.setBold(charAttrs, run.isBold());
    StyleConstants.setItalic(charAttrs, run.isItalic());
    StyleConstants.setStrikeThrough(charAttrs, run.isStrike());
    boolean underlined = run.getUnderline() != UnderlinePatterns.NONE;
    StyleConstants.setUnderline(charAttrs, underlined);
    VerticalAlign verticalAlignment = run.getSubscript();
    if (verticalAlignment == VerticalAlign.SUBSCRIPT) {
        StyleConstants.setSubscript(parAttrs, true);
    } else if (verticalAlignment == VerticalAlign.SUPERSCRIPT) {
        StyleConstants.setSuperscript(parAttrs, true);
    } else {
        StyleConstants.setSubscript(parAttrs, false);
        StyleConstants.setSuperscript(parAttrs, false);
    }
    if (run.getFontFamily() != null) {
        StyleConstants.setFontFamily(charAttrs, run.getFontFamily());
    }
    if (run.getColor() != null) {
        String name = run.getColor();
        if (!name.toLowerCase().equals("auto")) {
            Color color = Color.decode("#" + name);
            StyleConstants.setForeground(charAttrs, color);
        }
    }

    // Not working
    if (run.getCTR().getRPr() != null && run.getCTR().getRPr().getHighlight() != null) {
        STHighlightColor.Enum colorEnum = run.getCTR().getRPr().getHighlight().getVal();
        Color color = decodeHighlightName(colorEnum);
        StyleConstants.setBackground(charAttrs, color);
    }

    for (XWPFPicture picture : run.getEmbeddedPictures()) {
        processPicture(picture);
    }
    String text = run.toString();
    document.insertString(currentOffset, text, charAttrs);
    currentOffset += text.length();
}