now,i want to set text color of a docx,i can't find suitable method,please help me.....below is my code's snippet
Using java Syntax Highlighting
public class OpenMainDocumentAndTraverse extends AbstractSample {
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
inputfilepath = System.getProperty("user.dir")
+ "/sample-docs/sample-docx.xml";
}
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
.getJaxbElement();
Body body = wmlDocumentEl.getBody();
new TraversalUtil(body,
new Callback() {
String indent = "";
public List<Object> apply(Object o) {
String text = "";
if (o instanceof org.docx4j.wml.Text)
{
org.docx4j.wml.Text t =(org.docx4j.wml.Text) o;
org.docx4j.wml.Color color=new org.docx4j.wml.Color();
color.setVal("FF0000");
/**------------------------------------------------------------------------------------------------------how to get instance of org.docx4j.wml.R**/
text = t.getValue();
if(text.equals("[Name]")) {
t.setValue("Arnold");
}
}
System.out.println(indent + o.getClass().getName() + " \""
+ text + "\"");
return null;
}
public boolean shouldTraverse(Object o) {
return true;
}
// Depth first
public void walkJAXBElements(Object parent) {
indent += " ";
List children = getChildren(parent);
if (children != null) {
for (Object o : children) {
// if its wrapped in javax.xml.bind.JAXBElement, get its
// value
o = XmlUtils.unwrap(o);
this.apply(o);
if (this.shouldTraverse(o)) {
walkJAXBElements(o);
}
}
}
indent = indent.substring(0, indent.length() - 4);
}
public List<Object> getChildren(Object o) {
return TraversalUtil.getChildrenImpl(o);
}
}
);
wordMLPackage.save(new File("c:\\workspace\\test_out.docx"));
}
}
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
/**
* @param args
*/
public static void main(String[] args) throws Exception {
try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
inputfilepath = System.getProperty("user.dir")
+ "/sample-docs/sample-docx.xml";
}
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart
.getJaxbElement();
Body body = wmlDocumentEl.getBody();
new TraversalUtil(body,
new Callback() {
String indent = "";
public List<Object> apply(Object o) {
String text = "";
if (o instanceof org.docx4j.wml.Text)
{
org.docx4j.wml.Text t =(org.docx4j.wml.Text) o;
org.docx4j.wml.Color color=new org.docx4j.wml.Color();
color.setVal("FF0000");
/**------------------------------------------------------------------------------------------------------how to get instance of org.docx4j.wml.R**/
text = t.getValue();
if(text.equals("[Name]")) {
t.setValue("Arnold");
}
}
System.out.println(indent + o.getClass().getName() + " \""
+ text + "\"");
return null;
}
public boolean shouldTraverse(Object o) {
return true;
}
// Depth first
public void walkJAXBElements(Object parent) {
indent += " ";
List children = getChildren(parent);
if (children != null) {
for (Object o : children) {
// if its wrapped in javax.xml.bind.JAXBElement, get its
// value
o = XmlUtils.unwrap(o);
this.apply(o);
if (this.shouldTraverse(o)) {
walkJAXBElements(o);
}
}
}
indent = indent.substring(0, indent.length() - 4);
}
public List<Object> getChildren(Object o) {
return TraversalUtil.getChildrenImpl(o);
}
}
);
wordMLPackage.save(new File("c:\\workspace\\test_out.docx"));
}
}
Parsed in 0.020 seconds, using GeSHi 1.0.8.4