 | OO-Snippets: Embed a Graphic into a TextdocumentCommons| Keywords | embed, graphic, BitmapTable, TextGraphicObject, internal URL |
|---|
| Language | Java |
|---|
| Application | Writer |
|---|
| Authors | Christian Lohmaier (of the Java-snippet) (initial)
Danny Brewer (the idea to use the BitmapTable was his)
|
|---|
| Supported Versions | 2.0.x 1.1.x 1.0.x |
|---|
| Supported OS | All |
|---|
| Question | How can I embed (instead of link to) a graphic into a Document?
You can only insert graphics using a GraphicURL - there doesn't
seem to be a way to embed a graphic
|
|---|
| Answer | The solution is to let the GraphicURL point to an internal Graphic. The problem is that you cannot simply copy a file into the zip-container since OOo will change the name of the graphic to some checksum-based Name. That is where the BitmapTable comes into play. It allows you to specify an external URL and it will embed the graphic into the zip-container and allows you to get the internalURL to it. With that URL, inserting the graphic as embedded is a cakewalk. |
|---|
Embeds the given Image into a Textdocument at the given cursor position
(Anchored as character)
@param grProps&
@param xMSF
@param xCursor
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);
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);
xText.insertTextContent(xCursor, xImage, false);
xBitmapContainer.removeByName("someID");
} catch (Exception e) {
System.out.println("Failed to insert Graphic");
}
}
|
Changelog| Date | User | Modification |
|---|
| 2007-02-05 | cloph | Initial version |
|