--- src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (revision 1236322) +++ src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (working copy) @@ -48,6 +48,7 @@ import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTEmpty; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; @@ -74,6 +75,8 @@ * XWPFRun object defines a region of text with a common set of properties * * @author Yegor Kozlov + * @author Gregg Morris (gregg dot morris at gmail dot com) - added getColor(), setColor() + * */ public class XWPFRun { private CTR run; @@ -245,6 +248,31 @@ } /** + * Get text color. The returned value is a string in the hex form "RRGGBB". + */ + public String getColor() { + String color = null; + if (run.isSetRPr()) { + CTRPr pr = run.getRPr(); + if (pr.isSetColor()) { + CTColor clr = pr.getColor(); + color = clr.xgetVal().getStringValue(); + } + } + return color; + } + + /** + * Set text color. + * @param rgbStr - the desired color, in the hex form "RRGGBB". + */ + public void setColor(String rgbStr) { + CTRPr pr = run.isSetRPr() ? run.getRPr() : run.addNewRPr(); + CTColor color = pr.isSetColor() ? pr.getColor() : pr.addNewColor(); + color.setVal(rgbStr); + } + + /** * Return the string content of this text run * * @return the text of this text run or null if not set --- src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (revision 1236322) +++ src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java (working copy) @@ -161,6 +161,13 @@ assertEquals(2400, rpr.getPosition().getVal().longValue()); } + public void testSetGetColor() { + XWPFRun run = new XWPFRun(ctRun, p); + run.setColor("0F0F0F"); + String clr = run.getColor(); + assertEquals("0F0F0F", clr); + } + public void testAddCarriageReturn() { ctRun.addNewT().setStringValue("TEST STRING");