Using java Syntax Highlighting
public static byte[] insertPicToDocx(InputStream in,List<ByteArrayInputStream> fushionChartList,List<String> chartIndicatorType)throws Exception
{
LoadFromZipNG z = new LoadFromZipNG();
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)z.get(in);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.getParts().get(new PartName("/word/_rels/document.xml.rels"));
/**2011-5-25 新图的ID+图的描述**/
final HashMap<String,String> picMap=new HashMap<String,String>();
/**2011-5-25 将所有生成的图片绑定到文档**/
for(int i=0;i<fushionChartList.size();i++)
{
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, getBytesFromFile(fushionChartList.get(i)));
picMap.put(imagePart.getSourceRelationship().getId(), chartIndicatorType.get(i));
}
org.docx4j.wml.Document wmlDocumentEl = 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.Drawing)
{
String desc=((Anchor) ((org.docx4j.wml.Drawing) o).getAnchorOrInline().get(0)).getDocPr()
.getDescr();
for (int k = 0; k < picMap.keySet().size(); k++)
{
String key =(String) picMap.keySet().toArray()[k];
String value = "[["+picMap.get(key)+"]]";
if(!value.equals(desc))
{
continue;
}
else
{
((Anchor) ((org.docx4j.wml.Drawing) o)
.getAnchorOrInline().get(0)).getGraphic()
.getGraphicData().getPic().getBlipFill()
.getBlip().setEmbed(key);
break;
}
}
}
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);
System.out.println("类型:"+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);
}
}
);
ByteArrayOutputStream fos=new ByteArrayOutputStream();
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(fos);
byte[] reval=fos.toByteArray();
return reval;
}
{
LoadFromZipNG z = new LoadFromZipNG();
WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage)z.get(in);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.getParts().get(new PartName("/word/_rels/document.xml.rels"));
/**2011-5-25 新图的ID+图的描述**/
final HashMap<String,String> picMap=new HashMap<String,String>();
/**2011-5-25 将所有生成的图片绑定到文档**/
for(int i=0;i<fushionChartList.size();i++)
{
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, getBytesFromFile(fushionChartList.get(i)));
picMap.put(imagePart.getSourceRelationship().getId(), chartIndicatorType.get(i));
}
org.docx4j.wml.Document wmlDocumentEl = 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.Drawing)
{
String desc=((Anchor) ((org.docx4j.wml.Drawing) o).getAnchorOrInline().get(0)).getDocPr()
.getDescr();
for (int k = 0; k < picMap.keySet().size(); k++)
{
String key =(String) picMap.keySet().toArray()[k];
String value = "[["+picMap.get(key)+"]]";
if(!value.equals(desc))
{
continue;
}
else
{
((Anchor) ((org.docx4j.wml.Drawing) o)
.getAnchorOrInline().get(0)).getGraphic()
.getGraphicData().getPic().getBlipFill()
.getBlip().setEmbed(key);
break;
}
}
}
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);
System.out.println("类型:"+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);
}
}
);
ByteArrayOutputStream fos=new ByteArrayOutputStream();
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(fos);
byte[] reval=fos.toByteArray();
return reval;
}
Parsed in 0.022 seconds, using GeSHi 1.0.8.4
now,my code occurs a issue, when i first run this code,there is information
JAXB: Using RI WARN :
[2011-05-26 11:18:31,988] org.docx4j.XmlUtils:116 - Using default SAXParserFactory: null
in eclipse's console, how can i do ?
thanks in advance
tosswang