after i insert more than one ole object(excel) into document,document occur error when it is opened.
always the first ole object can't be opened,the second may be opened.
below is my code
- Code: Select all
package org;
import java.io.File;
import java.math.BigInteger;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.wml.CTObject;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.JcEnumeration;
import org.docx4j.wml.STVerticalJc;
public class XPathQuery
{
/**
* @param args
*/
public static void main(String[] args) throws Exception
{
String inputfilepath = "c:/workspace/Table.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
// String xpath = "//w:t";
String xpath = "//w:r[w:t[contains(text(),'ssss')]]";
List<Object> list = documentPart.getJAXBNodesViaXPath(xpath, false);
for(int i=0;i<list.size();i++)
{
org.docx4j.wml.R r = (org.docx4j.wml.R) list.get(i);
org.docx4j.wml.P parent = (org.docx4j.wml.P) r.getParent();
int index = documentPart.getContent().indexOf(parent);
documentPart.getContent().remove(parent);
if(i==0)
{
documentPart.getContent().add(index, SampleOLE.insertOLE(wordMLPackage));
}
else
{
documentPart.getContent().add(index, SampleOLE.insertOLE2(wordMLPackage));
}
}
/** 图是不是也能这么插入试试 */
wordMLPackage.save(new File("c:/workspace/test-tb.docx"));
}
}
- Code: Select all
package org;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBContext;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.TraversalUtil.Callback;
import org.docx4j.openpackaging.contenttype.ContentTypes;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.WordprocessingML.OleObjectBinaryPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Body;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.UnderlineEnumeration;
public class SampleOLE
{
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
public static Object insertOLE(WordprocessingMLPackage wordMLPackage) throws Exception
{
wordMLPackage.getContentTypeManager().addDefaultContentType("bin", ContentTypes.OFFICEDOCUMENT_OLE_OBJECT);
File file = new File("c:\\workspace\\test.png" );
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Relationship relImageObject = wordMLPackage.getMainDocumentPart().addTargetPart(imagePart);
Part olePart = new OleObjectBinaryPart();
FileInputStream oleFile = new FileInputStream("c:\\workspace\\Book1.xls");
((BinaryPart)olePart).setBinaryData(oleFile);
((OleObjectBinaryPart)olePart).initPOIFSFileSystem();
((OleObjectBinaryPart)olePart).writePOIFSFileSystem();
((OleObjectBinaryPart)olePart).viewFile(true);
Relationship relOleObject = wordMLPackage.getMainDocumentPart().addTargetPart(olePart);
String ml = "<w:p w:rsidR=\"0079557B\" w:rsidRDefault=\"0013129D\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
"xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">" +
"<w:r>" +
"<w:object w:dxaOrig=\"10485\" w:dyaOrig=\"4119\">" +
"<v:shapetype id=\"_x0000_t75\" coordsize=\"21600,21600\" o:spt=\"75\" o:preferrelative=\"t\" path=\"m@4@5l@4@11@9@11@9@5xe\" filled=\"f\" stroked=\"f\">" +
"<v:stroke joinstyle=\"miter\"/>" +
"<v:formulas>" +
"<v:f eqn=\"if lineDrawn pixelLineWidth 0\"/>" +
"<v:f eqn=\"sum @0 1 0\"/><v:f eqn=\"sum 0 0 @1\"/>" +
"<v:f eqn=\"prod @2 1 2\"/>" +
"<v:f eqn=\"prod @3 21600 pixelWidth\"/>" +
"<v:f eqn=\"prod @3 21600 pixelHeight\"/>" +
"<v:f eqn=\"sum @0 0 1\"/>" +
"<v:f eqn=\"prod @6 1 2\"/>" +
"<v:f eqn=\"prod @7 21600 pixelWidth\"/>" +
"<v:f eqn=\"sum @8 21600 0\"/>" +
"<v:f eqn=\"prod @7 21600 pixelHeight\"/>" +
"<v:f eqn=\"sum @10 21600 0\"/>" +
"</v:formulas>" +
"<v:path o:extrusionok=\"f\" gradientshapeok=\"t\" o:connecttype=\"rect\"/>" +
"<o:lock v:ext=\"edit\" aspectratio=\"t\"/>" +
"</v:shapetype>" +
"<v:shape id=\"_x0000_i1025\" type=\"#_x0000_t75\" style=\"width:"+(imagePart.getImageInfo().getSize().getWidthPx())+"pt;height:"+(imagePart.getImageInfo().getSize().getHeightPx())+"pt\" o:ole=\"\">" +
"<v:imagedata r:id=\"${ImageObjectRid}\" o:title=\"\"/>" +
"</v:shape>" +
"<o:OLEObject Type=\"Embed\" ProgID=\"Excel.Sheet.2\" ShapeID=\"_x0000_i1025\" DrawAspect=\"Content\" ObjectID=\"_1355911853\" r:id=\"${OLEObjectRid}\"/>" +
"</w:object>" +
"</w:r>" +
"</w:p>";
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("ImageObjectRid", relImageObject.getId());
mappings.put("OLEObjectRid", relOleObject.getId());
return org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings);
}
public static Object insertOLE2(WordprocessingMLPackage wordMLPackage) throws Exception
{
wordMLPackage.getContentTypeManager().addDefaultContentType("bin", ContentTypes.OFFICEDOCUMENT_OLE_OBJECT);
File file = new File("c:\\workspace\\test.png" );//也可以是image1.emf
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordMLPackage, bytes);
Relationship relImageObject = wordMLPackage.getMainDocumentPart().addTargetPart(imagePart);
Part olePart = new OleObjectBinaryPart();
FileInputStream oleFile = new FileInputStream("c:\\workspace\\Book1.xls");
((BinaryPart)olePart).setBinaryData(oleFile);
((OleObjectBinaryPart)olePart).initPOIFSFileSystem();
((OleObjectBinaryPart)olePart).writePOIFSFileSystem();
((OleObjectBinaryPart)olePart).viewFile(true);
Relationship relOleObject = wordMLPackage.getMainDocumentPart().addTargetPart(olePart);
String ml = "<w:p w:rsidR=\"0013129D\" w:rsidRDefault=\"0013129D\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">" +
"<w:r>" +
"<w:object w:dxaOrig=\"10485\" w:dyaOrig=\"4119\">" +
"<v:shape id=\"_x0000_i1026\" type=\"#_x0000_t75\" style=\"width:"+(imagePart.getImageInfo().getSize().getWidthPx())+"pt;height:"+(imagePart.getImageInfo().getSize().getHeightPx())+"pt\" o:ole=\"\">" +
"<v:imagedata r:id=\"${ImageObjectRid}\" o:title=\"\"/>" +
"</v:shape>" +
"<o:OLEObject Type=\"Embed\" ProgID=\"Excel.Sheet.2\" ShapeID=\"_x0000_i1026\" DrawAspect=\"Content\" ObjectID=\"_1355911854\" r:id=\"${OLEObjectRid}\"/>" +
"</w:object>" +
"</w:r>" +
"</w:p>";
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("ImageObjectRid", relImageObject.getId());
mappings.put("OLEObjectRid", relOleObject.getId());
return org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings);
}
}
attche are my example files.
thanks in advance!
tosswang