Hi Hanbo,
I answer you here, though I think you will have more answers if you send
your question to [EMAIL PROTECTED]
Hanbo Wang escribió:
Hey, guys,
I was encountered such a question: I wanted to write a java program to
extract the binary ole-object in ODP file and convert them into images. I
thought I should divided this task into 2 steps:
First of all, I should extract the object file from the zipped file.
I do not understand what you want:
1. "extract the binary ole-object in ODP file" == you want to store a
copy of the embedded object as an independent file? If so, I think this
isn't a good approach: if you want to read an embedded
object's stream you shouldn't treat the document as a zip file with Java
[N]IO/ZIP API (by the way, this is never a good practice with OOo docs:
you better use OOo UCB API, for example, for currently open documents:
css.ucb.TransientDocumentsContentProvider ).
I think you will find what you need in the module css.embed in
combination with css.ucb (instead of using Java API).
2. if you ONLY want to "convert" the embedded object to an image format:
did you think about the attribute XEmbeddedObjectSupplier2 ::
ReplacementGraphic ? (not present in every kind of embedded object; for
example, in a Java Applet, it will be null)
It will give you access to the XGraphic, then you can store this
XGraphic using XGraphicProvider::storeGraphic() [1]
That's
easy, but the question is what OO has done to this object. I wanted to know
the format of the file. Did OO server add some header information, or just
it was serialized by the programe who created it.
And then, I've checked the content.xml, knowing object's clsid, I can look
up in the registry to know which program created such an object. So that I
will depend on some Java-Com co-operation projects such as Jacob to write
some Java codes to manipulate the COM, and finally diplay the object and
convert it into image.
Can you give me some advice on my ideas, or can you tell me where OO
displayed these ole-objects in Impress or other applications.
Many thanks!
best regars,
Hanbo Wang
2007.9.22
I hope this could help you; if not, do not hesitate to answer again,
(with more details about what you're exactly looking for, please).
Bye and luck,
Ariel.
[1] See sample code:
//Start **********************************************************
/*
* EmbeddedObjectsTest.java
*
* Created on 23.08.2007 - 13:31:04
*
*/
package ar.com.arielconstenlahaile.test.embed;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.lang.XComponent;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextEmbeddedObjectsSupplier;
import com.sun.star.document.XEmbeddedObjectSupplier;
import com.sun.star.document.XEmbeddedObjectSupplier2;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.PropertyState;
import com.sun.star.container.XNamed;
import com.sun.star.container.XNameAccess;
import com.sun.star.graphic.XGraphic;
import com.sun.star.graphic.XGraphicProvider;
import com.sun.star.util.XStringSubstitution;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.container.NoSuchElementException;
/**
*
* @author ArielConstenlaHaile
*/
public class EmbeddedObjectsTest {
private XComponentContext xContext;
private XMultiComponentFactory xMCF;
private XTextDocument xTextDocument;
private String formulaName = "OOoMath test";
private XGraphic xReplacementGraphic;
public EmbeddedObjectsTest(XComponentContext xContext)
throws com.sun.star.uno.Exception {
this.xContext = xContext;
this.xMCF = xContext.getServiceManager();
xTextDocument = createWriterDoc();
createMathFormula();
XGraphic xReplacementGraphic = getReplacementGraphic();
storeGraphic(
xReplacementGraphic,
"OOoMath_Replacement_Graphic_TEST.png");
}
private XTextDocument createWriterDoc() throws
com.sun.star.uno.Exception{
XComponentLoader xDesktopLoader = (XComponentLoader)
UnoRuntime.queryInterface(
XComponentLoader.class,
xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
return (XTextDocument)UnoRuntime.queryInterface(
XTextDocument.class,
xDesktopLoader.loadComponentFromURL(
"private:factory/swriter", "_default", 0, new
PropertyValue[]{}));
}
private void createMathFormula() throws com.sun.star.uno.Exception {
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(XMultiServiceFactory.class,
xTextDocument);
XTextContent xTextContent = (XTextContent)
UnoRuntime.queryInterface(
XTextContent.class,
xDocFactory.createInstance(
"com.sun.star.text.TextEmbeddedObject" ));
XPropertySet xPropertySet = (XPropertySet)
UnoRuntime.queryInterface(
XPropertySet.class, xTextContent);
xPropertySet.setPropertyValue(
"CLSID",
"078B7ABA-54FC-457F-8551-6147e776a997" );
XNamed xnamed = (XNamed) UnoRuntime.queryInterface(
XNamed.class, xTextContent);
xnamed.setName(formulaName);
XTextCursor xTextCursor =
xTextDocument.getText().createTextCursor();
XTextRange xTextRange = (XTextRange) UnoRuntime.queryInterface(
XTextRange.class, xTextCursor);
xTextDocument.getText().insertTextContent(
xTextRange.getStart(), xTextContent, false );
XEmbeddedObjectSupplier xEmbeddedObjectSupplier =
(XEmbeddedObjectSupplier) UnoRuntime.queryInterface(
XEmbeddedObjectSupplier.class, xTextContent);
XComponent xEmbeddedObjectModel =
xEmbeddedObjectSupplier.getEmbeddedObject();
XPropertySet xFormulaProperties = (XPropertySet)
UnoRuntime.queryInterface(
XPropertySet.class, xEmbeddedObjectModel);
xFormulaProperties.setPropertyValue(
"Formula",
"{ { 6 over 7 times 8 } + 5 } over 10" );
}
private XGraphic getReplacementGraphic() {
XGraphic replacementGraphic= null;
try {
XTextEmbeddedObjectsSupplier xTextEmbeddedObjectsSupplier =
(XTextEmbeddedObjectsSupplier) UnoRuntime.queryInterface(
XTextEmbeddedObjectsSupplier.class, xTextDocument);
XNameAccess xEmbeddedObjectsNameAccess =
xTextEmbeddedObjectsSupplier.getEmbeddedObjects();
XEmbeddedObjectSupplier2 xEmbeddedObjectSupplier2 =
(XEmbeddedObjectSupplier2) UnoRuntime.queryInterface(
XEmbeddedObjectSupplier2.class,
xEmbeddedObjectsNameAccess.getByName(formulaName));
if (xEmbeddedObjectSupplier2 != null){
replacementGraphic =
xEmbeddedObjectSupplier2.getReplacementGraphic();
}
} catch (NoSuchElementException ex) {
ex.printStackTrace();
} catch (WrappedTargetException ex) {
ex.printStackTrace();
} finally {
return replacementGraphic;
}
}
private String getTempDir() throws com.sun.star.uno.Exception {
XStringSubstitution xStringSubstitution = (XStringSubstitution)
UnoRuntime.queryInterface(
XStringSubstitution.class,
xMCF.createInstanceWithContext(
"com.sun.star.util.PathSubstitution", xContext));
return xStringSubstitution.getSubstituteVariableValue("$(temp)");
}
private void storeGraphic(
XGraphic xReplacementGraphic,
String fileName
) throws com.sun.star.uno.Exception {
if (xReplacementGraphic!=null){
String tempDir = getTempDir();
PropertyValue[] aMediaProperties = {
new PropertyValue(
"URL", 0,
new String(tempDir + "/" + fileName),
PropertyState.DIRECT_VALUE),
new PropertyValue(
"MimeType", 0,
new String("image/png"),
PropertyState.DIRECT_VALUE)
};
storeGraphic(
xReplacementGraphic,
aMediaProperties);
}
}
private void storeGraphic(
XGraphic xGraphic,
PropertyValue[] aMediaProperties
) throws com.sun.star.uno.Exception {
XGraphicProvider xGraphicProvider = (XGraphicProvider)
UnoRuntime.queryInterface(
XGraphicProvider.class,
xMCF.createInstanceWithContext(
"com.sun.star.graphic.GraphicProvider", xContext));
xGraphicProvider.storeGraphic(xGraphic, aMediaProperties);
}
public static void main(String[] args) {
try {
XComponentContext xCtxt = Bootstrap.bootstrap();
EmbeddedObjectsTest test = new EmbeddedObjectsTest(xCtxt);
} catch (java.lang.Exception e){
e.printStackTrace();
} finally {
System.exit( 0 );
}
}
}
// End ******************************************************
--
Ariel Constenla-Haile
La Plata, Argentina
[EMAIL PROTECTED]
[EMAIL PROTECTED]
http://www.arielconstenlahaile.com.ar/ooo/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]