i have embed an excel in word07,but when i double click the image that represent the excel,show less effective.
i set fixed values for w:dxaOrig and w:dyaOrig because i don't know how to get dynamic values of w:dxaOrig and w:dyaOrig ……
who can help me how to get dynamic values of w:dxaOrig and w:dyaOrig and how to set element of v:shape's style attribute rightly.
below is my part code
- Code: Select all
public static Object insertOLE(WordprocessingMLPackage wordMLPackage,
String fileName, ExcelObj obj) throws Exception
{
org.docx4j.openpackaging.contenttype.CTDefault df = new CTDefault();
df.setContentType("application/vnd.ms-excel");
df.setExtension("xls");
wordMLPackage.getContentTypeManager().addDefaultContentType("xls", df);
InputStream is = new ByteArrayInputStream(obj.getImage());
long length = obj.getImage().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;
}
is.close();
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordMLPackage, bytes);
Relationship relImageObject = wordMLPackage.getMainDocumentPart()
.addTargetPart(imagePart);
Part olePart = createOleBinPart(fileName, obj.getExcel());
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=\"${shapeid}\" 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 ProgID=\"Excel.Sheet.2\" ShapeID=\"${shapeid}\" DrawAspect=\"Content\" ObjectID=\"${ObjectId}\" 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());
Random random = new Random();
String shapeId = Integer.toHexString(random.nextInt());
mappings.put("shapeid", shapeId);
String objectId = Integer.toHexString(random.nextInt());
mappings.put("ObjectId", objectId);
return org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings);
}
thanks in advance