Hi jason,
Is there possibility to add outlook message as a OLE object in the existing middle of the document? if it possible means could someone please share the code?
Thanks in Advance,
Indhu.
<w:p>
<w:r>
<w:object w:dxaOrig="1454" w:dyaOrig="941">
<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:73.2pt;height:46.8pt" o:ole="">
<v:imagedata r:id="rId4" o:title=""/>
</v:shape>
<o:OLEObject Type="Embed" ProgID="Package" ShapeID="_x0000_i1025" DrawAspect="Icon" ObjectID="_1352572164" r:id="rId5"/>
</w:object>
</w:r>
</w:p>
<v:imagedata r:id="rId4" o:title=""/>
Injecting the outlook message in the correct format may be a challenge. See viewtopic.php?f=6&t=72 for generic embedding info; the web may well contain something more specific to outlook. Refer also to the relevant Microsoft OLE specs.
i have created the document and inserted the outlook msg as a ole object and then i have also found that inserted object is a OleObjectBinaryPart
public void initPOIFSFileSystem() throws IOException
public void viewFile(boolean verbose) throws IOException
package creatingWordDoc;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import javax.xml.bind.JAXBContext;
import org.docx4j.XmlUtils;
import org.docx4j.model.datastorage.BindingHandler;
import org.docx4j.model.datastorage.CustomXmlDataStorage;
import org.docx4j.model.datastorage.OpenDoPEHandler;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.Document;
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.OleObjectBinaryPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.relationships.*;
import org.docx4j.openpackaging.contenttype.*;
public class sampleOLE
{
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
public static void main(String[] args) throws Exception
{
String outputfilepath = "E:/test_OLE.docx";
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
wordMLPackage.getContentTypeManager().addDefaultContentType("bin", ContentTypes.OFFICEDOCUMENT_OLE_OBJECT);
// 2. creating Image Part
File file = new File("image1.emf" );
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
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;
}
// Ensure all the bytes have been read in
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);
// 3. creating OLE part
Part olePart = new OleObjectBinaryPart();
FileInputStream oleFile = new FileInputStream("E:/Hi.msg");
((BinaryPart)olePart).setBinaryData(oleFile);
((OleObjectBinaryPart)olePart).initPOIFSFileSystem();
((OleObjectBinaryPart)olePart).writePOIFSFileSystem();
((OleObjectBinaryPart)olePart).viewFile(true);
Relationship relOleObject = wordMLPackage.getMainDocumentPart().addTargetPart(olePart);
// 3. contains $OLEObjectRid
String ml = "<w:p w:rsidR=\"001563A4\" w:rsidRDefault=\"007223E2\" 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=\"1543\" w:dyaOrig=\"993\"><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:77.25pt;height:49.5pt\" o:ole=\"\"><v:imagedata r:id=\"${ImageObjectRid}\" o:title=\"\"/></v:shape><o:OLEObject Type=\"Embed\" ProgID=\"Package\" ShapeID=\"_x0000_i1025\" DrawAspect=\"Icon\" 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());
wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings));
// 4. Save it
wordMLPackage.save(new java.io.File(outputfilepath));
System.out.println( "Saved output to:" + outputfilepath );
}
}
// 3. creating OLE part
Part olePart = new OleObjectBinaryPart();
FileInputStream oleFile = new FileInputStream("E:/Hi.msg");
((BinaryPart)olePart).setBinaryData(oleFile);
Users browsing this forum: No registered users and 55 guests