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

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

Introduction

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

Prototype

@Override
public void setBold(boolean value) 

Source Link

Document

Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a document.

Usage

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Inserts the given {@link MTable}./*w w w. j  a  v a 2  s . com*/
 * 
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param table
 *            the {@link MTable} to insert
 */
private void insertMTable(XWPFRun run, MTable table) {
    final XWPFTable docTable;
    if (generatedDocument instanceof XWPFDocument) {
        if (table.getLabel() != null) {
            XWPFRun captionRun;
            captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        docTable = ((XWPFDocument) generatedDocument).createTable();
    } else if (generatedDocument instanceof XWPFHeaderFooter) {
        final XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) generatedDocument;
        final int index = headerFooter._getHdrFtr().getTblArray().length;
        final CTTbl cttbl = headerFooter._getHdrFtr().insertNewTbl(index);
        docTable = new XWPFTable(cttbl, headerFooter);
        headerFooter.insertTable(index, docTable);
    } else if (generatedDocument instanceof XWPFTableCell) {
        XWPFTableCell tcell = (XWPFTableCell) generatedDocument;
        if (table.getLabel() != null) {
            final XWPFRun captionRun = run;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
            captionRun.setText(table.getLabel());
            captionRun.setBold(true);
        }
        CTTbl ctTbl = tcell.getCTTc().addNewTbl();
        docTable = new XWPFTable(ctTbl, tcell);
        int tableRank = tcell.getTables().size();
        tcell.insertTable(tableRank, docTable);
        // A paragraph is mandatory at the end of a cell, so let's always add one.
        tcell.addParagraph();
    } else {
        docTable = null;
        M2DocUtils.appendMessageRun((XWPFParagraph) run.getParent(), ValidationMessageLevel.ERROR,
                "m:table can't be inserted here.");
    }

    if (docTable != null) {
        fillTable(docTable, table);
    }
}

From source file:org.obeonetwork.m2doc.generator.M2DocEvaluator.java

License:Open Source License

/**
 * Apply the given style to the given run. Background color is not taken into account here since it does not apply to runs.
 * /*from   w  ww.j  a v a 2 s .  c o m*/
 * @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.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()));
    }
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a document.//from   www.j a  v  a  2 s .  co  m
 * 
 * @param tableRun
 *            the run after which the table must be created
 * @param first
 *            Whether it's the first table to insert or not
 * @param mtable
 *            The table description
 * @return The newly created table.
 */
private XWPFTable createTableInDocument(XWPFRun tableRun, boolean first, MTable mtable) {
    XWPFTable table;
    if (!first) {
        ((XWPFDocument) body).createParagraph();
    }
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = ((XWPFDocument) body).createParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    table = ((XWPFDocument) body).createTable();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Create a table in a table cell./*from w w  w.j  a  v  a  2 s . c om*/
 * 
 * @param tableRun
 *            The table run
 * @param first
 *            whether it's the 1st table created in this cell
 * @param mtable
 *            The table description
 * @param tcell
 *            The cell in which to create a new table
 * @return The newly creatted table, located in the given cell.
 */
private XWPFTable createTableInCell(XWPFRun tableRun, boolean first, MTable mtable, XWPFTableCell tcell) {
    XWPFTable table;
    if (showTitle() && mtable.getLabel() != null) {
        XWPFRun captionRun;
        if (first) {
            captionRun = tableRun;
            IRunBody runBody = captionRun.getParent();
            if (runBody instanceof XWPFParagraph) {
                ((XWPFParagraph) runBody).setSpacingAfter(0);
            }
        } else {
            XWPFParagraph captionParagraph = tcell.addParagraph();
            captionParagraph.setSpacingAfter(0);
            captionRun = captionParagraph.createRun();
        }
        captionRun.setText(mtable.getLabel());
        captionRun.setBold(true);
    }
    CTTbl ctTbl = tcell.getCTTc().addNewTbl();
    table = new XWPFTable(ctTbl, tcell);
    int tableRank = tcell.getTables().size();
    tcell.insertTable(tableRank, table);
    // A paragraph is mandatory at the end of a cell, so let's always add one.
    tcell.addParagraph();
    return table;
}

From source file:org.obeonetwork.m2doc.generator.TableClientProcessor.java

License:Open Source License

/**
 * Apply the given style to the given run. Background color is not taken into account here since it does not apply to runs.
 * //from  w w w  .ja v a  2 s  .  c o m
 * @param run
 *            The run to style
 * @param style
 *            The style to apply, can be <code>null</code>
 */
private void applyTableClientStyle(XWPFRun run, MStyle style) {
    run.setFontSize(style.getFontSize());
    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);
    run.setColor(hexColor(style.getForegroundColor()));
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseQuery(Query object) {
    // first evaluate the query.
    @SuppressWarnings("restriction")
    IQueryEvaluationEngine evaluator = new QueryEvaluationEngine(queryEnvironment);
    EvaluationResult result = evaluator.eval((AstResult) object.getQuery(),
            definitions.getCurrentDefinitions());
    String strResult;/*from   w  ww  .  j av  a2  s.  c  o m*/
    if (result == null) {
        strResult = "Couldn't parse query.";
    } else if (result.getResult() == null) {
        StringBuilder builder = new StringBuilder();
        getDiagnostic(result.getDiagnostic(), builder);
        strResult = builder.toString();
    } else {
        strResult = result.getResult().toString();
    }
    XWPFRun run = insertFieldRunReplacement(object.getStyleRun(), strResult);
    if (result == null || result.getResult() == null) {
        run.setBold(true);
        run.setColor(ERROR_COLOR);
    }
    return object;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w  ww.ja  v  a  2s  .  c  o  m*/
public AbstractConstruct caseRepetition(Repetition object) {
    // first evaluate the query
    @SuppressWarnings("restriction")
    EvaluationResult result = new QueryEvaluationEngine(queryEnvironment).eval(object.getQuery(),
            definitions.getCurrentDefinitions());
    if (result == null || result.getDiagnostic().getCode() == Diagnostic.ERROR) {
        // insert the tag runs as is.
        for (XWPFRun tagRun : object.getRuns()) {
            insertRun(tagRun);
        }
        // insert the error message.
        XWPFRun run = currentGeneratedParagraph.createRun();
        if (result != null) {
            run.setText(result.getDiagnostic().getMessage());
        } else {
            run.setText("couldn't evaluate expression");
        }
        if (result == null || result.getDiagnostic().getCode() == Diagnostic.ERROR) {
            run.setBold(true);
            run.setColor(ERROR_COLOR);
        }

        for (XWPFRun tagRun : object.getClosingRuns()) {
            insertRun(tagRun);
        }
    } else {
        List<Object> iteration = new ArrayList<Object>();
        if (result.getResult() instanceof Collection) {
            iteration.addAll((Collection<? extends Object>) result.getResult());
        } else {
            iteration.add(result.getResult());
        }
        for (Object val : iteration) {
            this.definitions.setValue(object.getIterationVar(), val);
            for (AbstractConstruct construct : object.getSubConstructs()) {
                doSwitch(construct);
            }
            // if the {gd:endfor} lies on a distinct paragraph, insert a new
            // paragraph there to take this into acount.
            int bodySize = object.getSubConstructs().size();
            if (bodySize > 0 && object.getSubConstructs().get(bodySize - 1).getRuns().size() > 0) {
                AbstractConstruct lastBodyPart = object.getSubConstructs().get(bodySize - 1);
                int runNumber = lastBodyPart.getRuns().size();
                XWPFRun lastRun = lastBodyPart.getRuns().get(runNumber - 1);
                int closingRunNumber = object.getClosingRuns().size();
                if (closingRunNumber > 0 && object.getClosingRuns().get(0).getParent() != lastRun.getParent()) {
                    forceNewParagraph = true;
                }
            }
        }
    }
    return object;

}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseConditionnal(Conditionnal object) {
    @SuppressWarnings("restriction")
    EvaluationResult result = new QueryEvaluationEngine(queryEnvironment).eval(object.getQuery(),
            definitions.getCurrentDefinitions());
    // XXX : result can be null here : check that before using it.
    if (result.getResult() instanceof Boolean) {
        if ((Boolean) result.getResult()) {
            for (AbstractConstruct construct : object.getSubConstructs()) {
                doSwitch(construct);/*from   ww w  .  j a v a 2s  . c  o  m*/
            }
        } else if (object.getAlternative() != null) {
            doSwitch(object.getAlternative());
        } else if (object.getElse() != null) {
            for (AbstractConstruct construct : object.getElse().getSubConstructs()) {
                doSwitch(construct);
            }
        }
    } else {
        for (XWPFRun tagRun : object.getRuns()) {
            insertRun(tagRun);
        }
        XWPFRun run = currentGeneratedParagraph.createRun();
        run.setText(result.getDiagnostic().getMessage());
        if (result.getDiagnostic().getCode() == Diagnostic.ERROR) {
            run.setBold(true);
            run.setColor(ERROR_COLOR);
        }
        for (XWPFRun tagRun : object.getClosingRuns()) {
            insertRun(tagRun);
        }
    }
    return object;
}

From source file:org.obeonetwork.m2doc.generator.TemplateProcessor.java

License:Open Source License

@Override
public AbstractConstruct caseImage(Image object) {
    XWPFRun imageRun = insertRun(object.getStyleRun());
    imageRun.setText("");
    imageRun.getCTR().getInstrTextList().clear();
    String filePath;/*www  .  j a v a2 s  .c o  m*/
    if ("".equals(this.rootProjectPath) || this.rootProjectPath == null) {
        filePath = object.getFileName();
    } else {
        filePath = this.rootProjectPath + "/" + object.getFileName();
    }
    try {
        int heigth = Units.toEMU(object.getHeight());
        int width = Units.toEMU(object.getWidth());
        imageRun.addPicture(new FileInputStream(filePath), getPictureType(object.getFileName()),
                object.getFileName(), width, heigth);
    } catch (InvalidFormatException e) {
        imageRun.setText("Picture in " + object.getFileName() + " has an invalid format.");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    } catch (FileNotFoundException e) {
        imageRun.setText("File " + filePath + " cannot be found.");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    } catch (IOException e) {
        imageRun.setText("An I/O Problem occured while reading file ");
        imageRun.setBold(true);
        imageRun.setColor(ERROR_COLOR);
    }
    return super.caseImage(object);
}

From source file:org.obeonetwork.m2doc.util.M2DocUtils.java

License:Open Source License

/**
 * Set the given message to the given {@link XWPFRun}.
 * //from www.j a va2  s .  co m
 * @param run
 *            the {@link XWPFRun}
 * @param level
 *            the {@link ValidationMessageLevel}
 * @param message
 *            the message
 */
public static void setRunMessage(XWPFRun run, ValidationMessageLevel level, String message) {
    run.setText(message);
    run.setBold(true);
    run.setColor(getColor(level));
}