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

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

Introduction

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

Prototype

public void setFontFamily(String fontFamily) 

Source Link

Document

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

Usage

From source file:genrate_doc.java

void insert_title(XWPFDocument doc) {

    XWPFParagraph p1 = doc.createParagraph();

    p1.setAlignment(ParagraphAlignment.CENTER);

    /*//p1.setBorderBottom(Borders.DOUBLE);
    //p1.setBorderTop(Borders.DOUBLE);/*  w w w.  java 2  s.co  m*/
            
    //p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);
            
    p1.setVerticalAlignment(TextAlignment.TOP);
    */
    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("HACKMAN:");
    r1.setBold(true);
    r1.setCapitalized(true);
    r1.setFontSize(35);
    r1.setFontFamily("Courier");
    //r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFRun r2 = p1.createRun();
    r2.setBold(true);
    r2.setText("yaay :) we did it \n (Hopefully :P)");
    r2.setBold(true);
    r2.setCapitalized(true);
    r2.setFontSize(25);
    r2.setFontFamily("Courier");
    //r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r2.setTextPosition(300);

}

From source file:certificatecreator.CertificateCreator.java

private void createStudentCertificate(String tempPath) {
    //Creates student certificate.
    //Read in template file and define output file.
    File inFile = new File(tempPath);
    File outFile = new File(userPath + File.separator + selectedStudent + "Certificate.docx");
    //Create output file. Throw alert if tempPath references nonexisting file.
    try {/*from  www. j  a v a  2  s. c o m*/
        copy(inFile, outFile);
        //Create internal word document based on copied template file. Write content.
        try {
            XWPFDocument certificate = new XWPFDocument(new FileInputStream(outFile));
            XWPFParagraph p1 = certificate.createParagraph();
            p1.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun r1 = p1.createRun();
            r1.setFontFamily("Candara");
            r1.setFontSize(40);
            r1.addBreak();
            r1.setText("Lawton Elementary Congratulates");
            XWPFRun r2 = p1.createRun();
            r2.setFontFamily("Candara");
            r2.setFontSize(36);
            r2.setBold(true);
            r2.addBreak();
            r2.addBreak();
            r2.setText(selectedStudent);
            r2.addBreak();
            r2.addBreak();
            XWPFRun r3 = p1.createRun();
            r3.setFontFamily("Candara");
            r3.setFontSize(26);
            r3.setText("For being a Lawton CARES winner on");
            r3.addBreak();
            String date = getDate();
            r3.setText(date);
            r3.addBreak();
            r3.addBreak();
            r3.addBreak();
            r3.addBreak();
            XWPFRun r4 = p1.createRun();
            r4.setColor("5B9BD5");
            r4.setFontFamily("Candara");
            r4.setFontSize(26);
            r4.setText("Compassion+Attitude+Respect+Effort+Safety=CARES");
            //Write internal document to copied templated file.
            try {
                FileOutputStream out = new FileOutputStream(outFile.toString());
                try {
                    certificate.write(out);
                    out.close();
                } catch (IOException io) {
                    System.err.println("Could not write file: " + io);
                }
            } catch (FileNotFoundException fnf) {
                System.err.println("Could not find output file: " + fnf);
            }
        } catch (IOException io) {
            System.err.println("Copy of template could not be found: " + io);
        }
    } catch (IOException io) {
        System.err.println("Method copy failed:" + io);
    } catch (NullPointerException npe) {
        System.err.println("Method copy failed:" + npe);
        Alert templateNotFound = new Alert(AlertType.WARNING,
                "Specified certificate template was not found. " + "Please choose a different template.");
        templateNotFound.showAndWait();
    }
}

From source file:ch.admin.searchreplace.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);/*from   w  w  w  .j a  v  a2s.c  o  m*/
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

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

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    // BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    // p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
    r4.setItalic(true);
    // This would imply that this break shall be treated as a simple line break, and break the line after that
    // word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.deepoove.poi.policy.TextRenderPolicy.java

License:Apache License

private void styleRun(XWPFRun run, Style style) {
    if (null != style) {
        String color = style.getColor();
        String fontFamily = style.getFontFamily();
        int fontSize = style.getFontSize();
        Boolean bold = style.isBold();
        Boolean italic = style.isItalic();
        Boolean strike = style.isStrike();
        if (null != color)
            run.setColor(color);/*from  ww  w  .  ja v  a  2 s  . co m*/
        if (0 != fontSize)
            run.setFontSize(fontSize);
        if (null != fontFamily)
            run.setFontFamily(fontFamily);
        if (null != bold)
            run.setBold(bold);
        if (null != italic)
            run.setItalic(italic);
        if (null != strike)
            run.setStrike(strike);
    }
}

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  w w .  ja  v  a2  s. c  o  m
    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:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

public static void createSimpleTable() throws Exception {
    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);/*  www . ja v a 2 s. c om*/
    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();
}

From source file:com.deepoove.poi.tl.SimpleTable.java

License:Apache License

/**
 * Create a table with some row and column styling. I "manually" add the
 * style name to the table, but don't check to see if the style actually
 * exists in the document. Since I'm creating it from scratch, it obviously
 * won't exist. When opened in MS Word, the table style becomes "Normal".
 * I manually set alternating row colors. This could be done using Themes,
 * but that's left as an exercise for the reader. The cells in the last
 * column of the table have 10pt. "Courier" font.
 * I make no claims that this is the "right" way to do it, but it worked
 * for me. Given the scarcity of XWPF examples, I thought this may prove
 * instructive and give you ideas for your own solutions.
        /*from w w w. j a  va 2  s  .  c  o  m*/
 * @throws Exception
 */
public static void createStyledTable() throws Exception {
    // Create a new document from scratch
    XWPFDocument doc = new XWPFDocument();
    // -- OR --
    // open an existing empty document with styles already defined
    //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));

    // Create a new table with 6 rows and 3 columns
    int nRows = 6;
    int nCols = 3;
    XWPFTable table = doc.createTable(nRows, nCols);

    // Set the table style. If the style is not defined, the table style
    // will become "Normal".
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTString styleStr = tblPr.addNewTblStyle();
    styleStr.setVal("StyledTable");

    // Get a list of the rows in the table
    List<XWPFTableRow> rows = table.getRows();
    int rowCt = 0;
    int colCt = 0;
    for (XWPFTableRow row : rows) {
        // get table row properties (trPr)
        CTTrPr trPr = row.getCtRow().addNewTrPr();
        // set row height; units = twentieth of a point, 360 = 0.25"
        CTHeight ht = trPr.addNewTrHeight();
        ht.setVal(BigInteger.valueOf(360));

        // get the cells in this row
        List<XWPFTableCell> cells = row.getTableCells();
        // add content to each cell
        for (XWPFTableCell cell : cells) {
            // get a table cell properties element (tcPr)
            CTTcPr tcpr = cell.getCTTc().addNewTcPr();
            // set vertical alignment to "center"
            CTVerticalJc va = tcpr.addNewVAlign();
            va.setVal(STVerticalJc.CENTER);

            // create cell color element
            CTShd ctshd = tcpr.addNewShd();
            ctshd.setColor("auto");
            ctshd.setVal(STShd.CLEAR);
            if (rowCt == 0) {
                // header row
                ctshd.setFill("A7BFDE");
            } else if (rowCt % 2 == 0) {
                // even row
                ctshd.setFill("D3DFEE");
            } else {
                // odd row
                ctshd.setFill("EDF2F8");
            }

            // get 1st paragraph in cell's paragraph list
            XWPFParagraph para = cell.getParagraphs().get(0);
            // create a run to contain the content
            XWPFRun rh = para.createRun();
            // style cell as desired
            if (colCt == nCols - 1) {
                // last column is 10pt Courier
                rh.setFontSize(10);
                rh.setFontFamily("Courier");
            }
            if (rowCt == 0) {
                // header row
                rh.setText("header row, col " + colCt);
                rh.setBold(true);
                para.setAlignment(ParagraphAlignment.CENTER);
            } else if (rowCt % 2 == 0) {
                // even row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            } else {
                // odd row
                rh.setText("row " + rowCt + ", col " + colCt);
                para.setAlignment(ParagraphAlignment.LEFT);
            }
            colCt++;
        } // for cell
        colCt = 0;
        rowCt++;
    } // for row

    // write the file
    FileOutputStream out = new FileOutputStream("styledTable.docx");
    doc.write(out);
    out.close();
}

From source file:com.ecm.poi.SimpleDocument.java

License:Apache License

@Test
public void xwpfTest() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);/*w  w w . j  a v  a 2 s . c om*/
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

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

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
    r4.setItalic(true);
    //This would imply that this break shall be treated as a simple line break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("d:\\temp\\simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

public static void fillWordDocument(XWPFDocument doc) {
    try {//from   www  .  j  a  v a2  s.c o m
        XWPFParagraph p1 = doc.createParagraph();
        p1.setAlignment(ParagraphAlignment.CENTER);
        p1.setBorderBottom(Borders.DOUBLE);
        p1.setBorderTop(Borders.DOUBLE);

        p1.setBorderRight(Borders.DOUBLE);
        p1.setBorderLeft(Borders.DOUBLE);
        p1.setBorderBetween(Borders.SINGLE);

        p1.setVerticalAlignment(TextAlignment.TOP);

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

        XWPFParagraph p2 = doc.createParagraph();
        p2.setAlignment(ParagraphAlignment.RIGHT);

        //BORDERS
        p2.setBorderBottom(Borders.DOUBLE);
        p2.setBorderTop(Borders.DOUBLE);
        p2.setBorderRight(Borders.DOUBLE);
        p2.setBorderLeft(Borders.DOUBLE);
        p2.setBorderBetween(Borders.SINGLE);

        XWPFRun r2 = p2.createRun();
        r2.setText("jumped over the lazy dog");
        r2.setStrike(true);
        r2.setFontSize(20);

        XWPFRun r3 = p2.createRun();
        r3.setText("and went away");
        r3.setStrike(true);
        r3.setFontSize(20);
        r3.setSubscript(VerticalAlign.SUPERSCRIPT);

        XWPFParagraph p3 = doc.createParagraph();
        p3.setWordWrap(true);
        p3.setPageBreak(true);

        p3.setAlignment(ParagraphAlignment.BOTH);
        p3.setSpacingLineRule(LineSpacingRule.EXACT);

        p3.setIndentationFirstLine(600);

        XWPFRun r4 = p3.createRun();
        r4.setTextPosition(200);
        r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
                + "The slings and arrows of outrageous fortune, "
                + "Or to take arms against a sea of troubles, "
                + "And by opposing end them? To die: to sleep; ");
        r4.addBreak(BreakType.PAGE);
        r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
                + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
                + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
        r4.setItalic(true);

        //This would imply that this break shall be treated as a simple line break, and break the line after that word:

        XWPFRun r5 = p3.createRun();
        r5.setTextPosition(-10);
        r5.setText("For in that sleep of death what dreams may come");
        r5.addCarriageReturn();
        r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
                + "That makes calamity of so long life;");
        r5.addBreak();
        r5.setText("For who would bear the whips and scorns of time,"
                + "The oppressor's wrong, the proud man's contumely,");

        r5.addBreak(BreakClear.ALL);
        r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
                + ".......");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordLabel.java

License:Apache License

private void applyStyleToFocWordLabel(XWPFRun run, XWPFParagraph paragraph) {
    FocXMLAttributes xmlAttributes = getXmlAttribute();
    if (xmlAttributes != null) {
        String style = xmlAttributes.getValue(FXML.ATT_STYLE);
        if (style != null && !style.isEmpty()) {
            StringTokenizer styleParsing = new StringTokenizer(style, ",");

            while (styleParsing.hasMoreTokens()) {
                String styleToken = styleParsing.nextToken();

                if (styleToken.equals(FXML.MS_STYLE_VAL_BOLD)) {
                    run.setBold(true);//  w  ww  . j a va  2 s . co  m
                } else if (styleToken.startsWith(FXML.MS_STYLE_VAL_FONT_SIZE)
                        && !styleToken.startsWith(FXML.MS_STYLE_VAL_FONT_FAMILY)) {
                    String fontSize = styleToken.substring(1, styleToken.length());
                    int size = 0;
                    try {
                        size = Integer.parseInt(fontSize);
                    } catch (Exception ex) {
                        Globals.logException(ex);
                    }
                    run.setFontSize(size);
                } else if (styleToken.equals(FXML.MS_STYLE_VAL_ITALIC)) {
                    run.setItalic(true);
                } else if (styleToken.equals(FXML.MS_STYLE_VAL_UNDERLINE)) {
                    run.setUnderline(UnderlinePatterns.DASH);
                } else if (styleToken.startsWith(FXML.MS_STYLE_VAL_FONT_FAMILY)) {
                    String fontfamily = styleToken.substring(styleToken.indexOf("-") + 1, styleToken.length());
                    run.setFontFamily(fontfamily);
                } else if (styleToken.equals(FXML.MS_STYLE_VAL_ALIGN_LEFT)) {
                    paragraph.setAlignment(ParagraphAlignment.LEFT);
                } else if (styleToken.equals(FXML.MS_STYLE_VAL_ALIGN_RIGHT)) {
                    paragraph.setAlignment(ParagraphAlignment.RIGHT);
                } else if (styleToken.equals(FXML.MS_STYLE_VAL_ALIGN_CENTER)) {
                    paragraph.setAlignment(ParagraphAlignment.CENTER);
                }
            }
        }
    }
}