Hi *,
attached is a codesnippet to insert a graphic as embedded, and not as
linked.
It is more or less some workaround, but it seems to work just fine.
The workaround is to use the BitmapTable to let OOo insert the file into
the zip-container (including the name-change to the checksum based one
that makes it impossible to just copy a file in the container). This
gives you the internal URL that can be used as GraphicURL.
While still a workaround, it is quite a comfortable one :-)
The idea to use the BitmapTable is from Danny Brewer (found the
corresponding Basic-Code in Andrew Pitonyak's Macro-Doc)
ciao
Christian
--
NP: Marilyn Manson - Antichrist Superstar
<?xml version="1.0"?>
<snippet language="Java" application="Writer">
<keywords>
<keyword>embed</keyword>
<keyword>graphic</keyword>
<keyword>BitmapTable</keyword>
<keyword>TextGraphicObject</keyword>
<keyword>internal URL</keyword>
</keywords>
<authors>
<author id="cloph" initial="true" email="[EMAIL PROTECTED]"
copyright="Public Domain / none">Christian Lohmaier (of the
Java-snippet)</author>
<author id="" initial="false" email="">Danny Brewer (the idea to use
the BitmapTable was his)</author>
</authors>
<question heading="Embed a Graphic into a Textdocument">How can I embed
(instead of link to) a graphic into a Document?
<p>You can only insert graphics using a GraphicURL - there doesn't</p>
<p>seem to be a way to embed a graphic</p>
</question>
<answer>
<p>The solution is to let the GraphicURL point to an internal Graphic.</p>
<p>The problem is that you cannot simply copy a file into the </p>
<p>zip-container since OOo will change the name of the graphic to </p>
<p>some checksum-based Name.</p>
<p>That is where the BitmapTable comes into play. It allows you to </p>
<p>specify an external URL and it will embed the graphic into the </p>
<p>zip-container and allows you to get the internalURL to it.</p>
<p>With that URL, inserting the graphic as embedded is a cakewalk.</p>
<listing>/**
* Embeds the given Image into a Textdocument at the given cursor position
* (Anchored as character)
*
* @param grProps OOo-style URL and width & height of the graphic
* @param xMSF the factory to create services from
* @param xCursor the cursor where to insert the graphic
*/
private void embedGraphic(GraphicInfo grProps,
XMultiServiceFactory xMSF, XTextCursor xCursor) {
XNameContainer xBitmapContainer = null;
XText xText = xCursor.getText();
XTextContent xImage = null;
String internalURL = null;
try {
xBitmapContainer = (XNameContainer) UnoRuntime.queryInterface(
XNameContainer.class, xMSF.createInstance(
"com.sun.star.drawing.BitmapTable"));
xImage = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class, xMSF.createInstance(
"com.sun.star.text.TextGraphicObject"));
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xImage);
// helper-stuff to let OOo create an internal name of the
graphic
// that can be used later (internal name consists of various
checksums)
xBitmapContainer.insertByName("someID",
grProps.unoURL);
internalURL = AnyConverter.toString(xBitmapContainer
.getByName("someID"));
xProps.setPropertyValue("AnchorType",
com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
xProps.setPropertyValue("GraphicURL", internalURL);
xProps.setPropertyValue("Width", (int)
grProps.widthOfGraphic);
xProps.setPropertyValue("Height", (int)
grProps.heightOfGraphic);
// inser the graphic at the cursor position
xText.insertTextContent(xCursor, xImage, false);
// remove the helper-entry
xBitmapContainer.removeByName("someID");
} catch (Exception e) {
System.out.println("Failed to insert Graphic");
}
}
</listing>
</answer>
<versions>
<version number="2.0.x" status="tested"/>
<version number="1.1.x" status="can not work"/>
<version number="1.0.x" status="can not work"/>
</versions>
<operating-systems>
<operating-system name="All"/>
</operating-systems>
<changelog>
<change author-id="cloph" date="2007-02-05">Initial version</change>
</changelog>
</snippet>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]