Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph getRuns

Introduction

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

Prototype

public List<XWPFRun> getRuns() 

Source Link

Usage

From source file:com.qihang.winter.poi.word.parse.ParseWord07.java

License:Apache License

/**
 * ??/* w  w  w. j  a v a2 s .c o  m*/
 * 
 * @author Zerrion
 * @date 2013-11-16
 * @param paragraph
 * @param map
 */
private void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map) throws Exception {
    XWPFRun run;
    XWPFRun currentRun = null;// run,?set,???
    String currentText = "";// ?text
    String text;
    Boolean isfinde = false;// ???{{
    List<Integer> runIndex = new ArrayList<Integer>();// ?run,
    for (int i = 0; i < paragraph.getRuns().size(); i++) {
        run = paragraph.getRuns().get(i);
        text = run.getText(0);
        if (StringUtils.isEmpty(text)) {
            continue;
        } // ""?
        if (isfinde) {
            currentText += text;
            if (currentText.indexOf("{{") == -1) {
                isfinde = false;
                runIndex.clear();
            } else {
                runIndex.add(i);
            }
            if (currentText.indexOf("}}") != -1) {
                changeValues(paragraph, currentRun, currentText, runIndex, map);
                currentText = "";
                isfinde = false;
            }
        } else if (text.indexOf("{") >= 0) {// ?
            currentText = text;
            isfinde = true;
            currentRun = run;
        } else {
            currentText = "";
        }
        if (currentText.indexOf("}}") != -1) {
            changeValues(paragraph, currentRun, currentText, runIndex, map);
            isfinde = false;
        }
    }

}

From source file:com.siemens.sw360.licenseinfo.outputGenerators.DocxUtils.java

License:Open Source License

private static void replaceParagraph(XWPFParagraph paragraph, String placeHolder, String replaceText) {
    for (XWPFRun r : paragraph.getRuns()) {
        String text = r.getText(r.getTextPosition());
        if (text != null && text.contains(placeHolder)) {
            text = text.replace(placeHolder, replaceText);
            r.setText(text, 0);//  w w w.j  a  v a  2  s  .  c  o  m
        }
    }
}

From source file:DocxProcess.DocxTemplateReplacer.java

private TreeMap<Integer, XWPFRun> getPosToRuns(XWPFParagraph paragraph) {
    int pos = 0;/*  w w  w  .  j  ava  2  s  .  c  om*/
    TreeMap<Integer, XWPFRun> map = new TreeMap<Integer, XWPFRun>();
    for (XWPFRun run : paragraph.getRuns()) {
        String runText = run.text();
        if (runText != null && runText.length() > 0) {
            for (int i = 0; i < runText.length(); i++) {
                map.put(pos + i, run);
            }
            pos += runText.length();
        }

    }
    return map;
}

From source file:easyoffice.word.WordMaker.java

private static void replaceText(XWPFDocument doc, HashMap<String, String> data) {

    Set<String> keySet = data.keySet();

    for (String key : keySet) {
        for (XWPFParagraph p : doc.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();

            for (XWPFRun run : runs) {
                if (run.toString().toLowerCase().equals(key)) {
                    run.setText(data.get(key), 0);
                }//w ww . j a v  a  2  s .  c  o  m
            }
        }
    }
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.A.java

License:Open Source License

/**
 * Paragraph from word/document.xml :/*from   w w w  . ja v  a 2 s .  co m*/
 * 
 * <pre>
 *         <w:p w:rsidR="008A2506" w:rsidRDefault="008A2506" w:rsidP="008A2506">
 *             <w:pPr>
 *                 <w:pStyle w:val="Titre" />
 *                 <w:outlineLvl w:val="0" />
 *             </w:pPr>
 *             <w:r>
 *                 <w:t>Office Open XML</w:t>
 *             </w:r>
 *         </w:p>
 * 
 * Titre from word/styles.xml :
 * 
 * <pre>
 *     <w:style w:type="paragraph" w:styleId="Titre">
 *         <w:name w:val="Title" />
 *         <w:aliases w:val="Document Title" />
 *         <w:next w:val="Normal" />
 *         <w:link w:val="TitreCar" />
 *         <w:rsid w:val="008A2506" />
 *         <w:pPr>
 *             <w:widowControl w:val="0" />
 *             <w:jc w:val="center" />
 *         </w:pPr>
 *         <w:rPr>
 *             <w:rFonts w:eastAsia="Times New Roman" w:cs="Arial" />
 *             <w:color w:val="17365D" w:themeColor="text2" w:themeShade="BF" />
 *             <w:sz w:val="192" />
 *             <w:lang w:val="en-CA" w:eastAsia="en-CA" />
 *         </w:rPr>
 *     </w:style>
 * </pre>
 * 
 * @param paragraph
 * @param stylesDocument
 */
private void testParagraphWithTitre(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {

    // Spacing after : call twice (the first set teh value on cache, the second retrieves teh value from the cache.
    Float spacingAfter = stylesDocument.getSpacingAfter(paragraph);
    spacingAfter = stylesDocument.getSpacingAfter(paragraph);

    Assert.assertNotNull(spacingAfter);
    Assert.assertEquals(10, spacingAfter.intValue());

    List<XWPFRun> runs = paragraph.getRuns();
    XWPFRun run = runs.get(0);

    // family = Times New Roman
    String fontFamily = stylesDocument.getFontFamilyAscii(run);
    Assert.assertEquals("Times New Roman", fontFamily);

    // size= 12
    Float fontSize = stylesDocument.getFontSize(run);
    Assert.assertNotNull(fontSize);
    Assert.assertEquals(12, fontSize.intValue());

    // bold = true
    Boolean bold = stylesDocument.getFontStyleBold(run);
    Assert.assertNotNull(bold);
    Assert.assertTrue(bold);

    // italic not defined
    Boolean italic = stylesDocument.getFontStyleItalic(run);
    Assert.assertNull(italic);

    // color =#17365D
    Color color = stylesDocument.getFontColor(run);
    Assert.assertNotNull(color);
    Assert.assertEquals("#000000", ColorHelper.toHexString(color).toUpperCase());

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java

License:Open Source License

/**
 * Paragraph from word/document.xml ://  w ww .j ava 2 s . co m
 * 
 * <pre>
 * <w:p w:rsidR="00F030AA" w:rsidRDefault="00045474" w:rsidP="00045474">
 *         <w:pPr>
 *             <w:pStyle w:val="Style1" />
 *         </w:pPr>
 *         <w:r>
 *             <w:t>A</w:t>
 *         </w:r>
 *     </w:p>
 * </pre>
 * 
 * Style1 from word/styles.xml :
 * 
 * <pre>
 *  <w:style w:type="paragraph" w:customStyle="1" w:styleId="Style1">
 *         <w:name w:val="Style1" />
 *         <w:basedOn w:val="Normal" />
 *         <w:qFormat />
 *         <w:rsid w:val="00045474" />
 *         <w:rPr>
 *             <w:b />
 *         </w:rPr>
 *     </w:style>
 * </pre>
 * 
 * @param paragraph
 * @param stylesDocument
 */
private void testParagraphWithStyle1(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    List<XWPFRun> runs = paragraph.getRuns();
    XWPFRun run = runs.get(0);

    // family = Calibri (Corps)
    String fontFamily = stylesDocument.getFontFamilyAscii(run);
    // should be "Calibri (Corps)" but is null
    // Assert.assertEquals( "Magneto", fontFamily );

    // size= 11
    Float fontSize = stylesDocument.getFontSize(run);
    Assert.assertNotNull(fontSize);
    Assert.assertEquals(11, fontSize.intValue());

    // bold= true
    Boolean bold = stylesDocument.getFontStyleBold(run);
    Assert.assertNotNull(bold);
    Assert.assertTrue(bold);

    // italic not defined
    Boolean italic = stylesDocument.getFontStyleItalic(run);
    Assert.assertNull(italic);

    // color not defined
    Color color = stylesDocument.getFontColor(run);
    Assert.assertNull(color);

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java

License:Open Source License

/**
 * Paragraph from word/document.xml :/*w w w  .j  a  v a2  s  . co  m*/
 * 
 * <pre>
 *         <w:p w:rsidR="00045474" w:rsidRDefault="00045474" w:rsidP="00045474">
 *             <w:pPr>
 *                 <w:pStyle w:val="Style2" />
 *             </w:pPr>
 *             <w:r>
 *                 <w:t>B</w:t>
 *             </w:r>
 *         </w:p>
 * </pre>
 * 
 * Style3 from word/styles.xml :
 * 
 * <pre>
 *     <w:style w:type="paragraph" w:customStyle="1" w:styleId="Style2">
 *         <w:name w:val="Style2" />
 *         <w:basedOn w:val="Style1" />
 *         <w:qFormat />
 *         <w:rsid w:val="00045474" />
 *         <w:rPr>
 *             <w:sz w:val="40" />
 *         </w:rPr>
 *     </w:style>
 * </pre>
 * 
 * @param paragraph
 * @param stylesDocument
 */
private void testParagraphWithStyle2(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    List<XWPFRun> runs = paragraph.getRuns();
    XWPFRun run = runs.get(0);

    // family = Calibri (Corps)
    String fontFamily = stylesDocument.getFontFamilyAscii(run);
    // should be "Calibri (Corps)" but is null
    // Assert.assertEquals( "Magneto", fontFamily );

    // size= 20
    Float fontSize = stylesDocument.getFontSize(run);
    Assert.assertNotNull(fontSize);
    Assert.assertEquals(20, fontSize.intValue());

    // bold= true
    Boolean bold = stylesDocument.getFontStyleBold(run);
    Assert.assertNotNull(bold);
    Assert.assertTrue(bold);

    // italic not defined
    Boolean italic = stylesDocument.getFontStyleItalic(run);
    Assert.assertNull(italic);

    // color not defined
    Color color = stylesDocument.getFontColor(run);
    Assert.assertNull(color);
}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java

License:Open Source License

/**
 * Paragraph from word/document.xml ://  www  .ja  v  a 2  s  . co  m
 * 
 * <pre>
 *      <w:p w:rsidR="00045474" w:rsidRDefault="00045474" w:rsidP="00045474">
 *             <w:pPr>
 *                 <w:pStyle w:val="Style3" />
 *             </w:pPr>
 *             <w:r>
 *                 <w:t>C</w:t>
 *             </w:r>
 *         </w:p>
 * </pre>
 * 
 * Style3 from word/styles.xml :
 * 
 * <pre>
 *  <w:style w:type="paragraph" w:customStyle="1" w:styleId="Style3">
 *         <w:name w:val="Style3" />
 *         <w:basedOn w:val="Style2" />
 *         <w:qFormat />
 *         <w:rsid w:val="00045474" />
 *         <w:rPr>
 *             <w:rFonts w:ascii="Magneto" w:hAnsi="Magneto" />
 *             <w:b w:val="0" />
 *             <w:i />
 *             <w:color w:val="FABF8F" w:themeColor="accent6" w:themeTint="99" />
 *         </w:rPr>
 *     </w:style>
 * </pre>
 * 
 * @param paragraph
 * @param stylesDocument
 */
private void testParagraphWithStyle3(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    List<XWPFRun> runs = paragraph.getRuns();
    XWPFRun run = runs.get(0);

    // family = Magneto
    String fontFamily = stylesDocument.getFontFamilyAscii(run);
    Assert.assertEquals("Magneto", fontFamily);

    // size= 20
    Float fontSize = stylesDocument.getFontSize(run);
    Assert.assertNotNull(fontSize);
    Assert.assertEquals(20, fontSize.intValue());

    // bold= false
    Boolean bold = stylesDocument.getFontStyleBold(run);
    Assert.assertNotNull(bold);
    Assert.assertFalse(bold);

    // italic= true
    Boolean italic = stylesDocument.getFontStyleItalic(run);
    Assert.assertNotNull(italic);
    Assert.assertTrue(italic);

    // color=#FABF8F
    Color color = stylesDocument.getFontColor(run);
    Assert.assertNotNull(color);
    Assert.assertEquals("#FABF8F", ColorHelper.toHexString(color).toUpperCase());

}

From source file:fr.opensagres.poi.xwpf.converter.core.styles.FontStylesBasedOnTestCase.java

License:Open Source License

/**
 * Paragraph from word/document.xml ://from   ww w  .j a va2s.c o  m
 * 
 * <pre>
 * <w:p w:rsidR="00F70C92" w:rsidRDefault="00F70C92" w:rsidP="00F70C92">
 *             <w:pPr>
 *                 <w:pStyle w:val="Style4" />
 *             </w:pPr>
 *             <w:r>
 *                 <w:t>D</w:t>
 *             </w:r>
 *         </w:p>
 * </pre>
 * 
 * Style4 from word/styles.xml :
 * 
 * <pre>
 * <w:style w:type="paragraph" w:customStyle="1" w:styleId="Style4">
 *         <w:name w:val="Style4" />
 *         <w:basedOn w:val="Style3" />
 *         <w:qFormat />
 *         <w:rsid w:val="00F70C92" />
 *         <w:rPr>
 *             <w:rFonts w:ascii="Chiller" w:hAnsi="Chiller" />
 *         </w:rPr>
 *     </w:style>
 * </pre>
 * 
 * @param paragraph
 * @param stylesDocument
 */
private void testParagraphWithStyle4(XWPFParagraph paragraph, XWPFStylesDocument stylesDocument) {
    List<XWPFRun> runs = paragraph.getRuns();
    XWPFRun run = runs.get(0);

    // family = Chiller
    String fontFamily = stylesDocument.getFontFamilyAscii(run);
    Assert.assertEquals("Chiller", fontFamily);

    // size= 20
    Float fontSize = stylesDocument.getFontSize(run);
    Assert.assertNotNull(fontSize);
    Assert.assertEquals(20, fontSize.intValue());

    // italic= true
    Boolean italic = stylesDocument.getFontStyleItalic(run);
    Assert.assertNotNull(italic);
    Assert.assertTrue(italic);

    // color=#FABF8F
    Color color = stylesDocument.getFontColor(run);
    Assert.assertNotNull(color);
    Assert.assertEquals("#FABF8F", ColorHelper.toHexString(color).toUpperCase());
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

protected void visitParagraphBody(XWPFParagraph paragraph, int index, T paragraphContainer) throws Exception {
    List<XWPFRun> runs = paragraph.getRuns();
    if (runs.isEmpty()) {
        // a new line must be generated if :
        // - there is next paragraph/table
        // - if the body is a cell (with none vMerge) and contains just this paragraph
        if (isAddNewLine(paragraph, index)) {
            visitEmptyRun(paragraphContainer);
        }//w  w w.  ja  v  a  2 s.co  m

        // sometimes, POI tells that run is empty
        // but it can be have w:r in the w:pPr
        // <w:p><w:pPr .. <w:r> => See the header1.xml of DocxBig.docx ,
        // => test if it exist w:r
        // CTP p = paragraph.getCTP();
        // CTPPr pPr = p.getPPr();
        // if (pPr != null) {
        // XmlObject[] wRuns =
        // pPr.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:r");
        // if (wRuns != null) {
        // for ( int i = 0; i < wRuns.length; i++ )
        // {
        // XmlObject o = wRuns[i];
        // o.getDomNode().getParentNode()
        // if (o instanceof CTR) {
        // System.err.println(wRuns[i]);
        // }
        //
        // }
        // }
        // }
        // //XmlObject[] t =
        // o.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t");
        // //paragraph.getCTP().get
    } else {
        // Loop for each element of <w:r, w:fldSimple
        // to keep the order of those elements.
        visitRuns(paragraph, paragraphContainer);
    }

    // Page Break
    // Cannot use paragraph.isPageBreak() because it throws NPE because
    // pageBreak.getVal() can be null.
    CTPPr ppr = paragraph.getCTP().getPPr();
    if (ppr != null) {
        if (ppr.isSetPageBreakBefore()) {
            CTOnOff pageBreak = ppr.getPageBreakBefore();
            if (pageBreak != null
                    && (pageBreak.getVal() == null || pageBreak.getVal().intValue() == STOnOff.INT_TRUE)) {
                pageBreak();
            }
        }
    }
}