2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 import com
.sun
.star
.beans
.PropertyValue
;
21 import com
.sun
.star
.beans
.XPropertySet
;
23 import com
.sun
.star
.container
.XNamed
;
25 import com
.sun
.star
.drawing
.XDrawPage
;
26 import com
.sun
.star
.drawing
.XDrawPageSupplier
;
28 import com
.sun
.star
.lang
.XComponent
;
29 import com
.sun
.star
.lang
.XMultiServiceFactory
;
30 import com
.sun
.star
.lang
.XMultiComponentFactory
;
32 import com
.sun
.star
.text
.XText
;
33 import com
.sun
.star
.text
.XTextContent
;
34 import com
.sun
.star
.text
.XTextCursor
;
35 import com
.sun
.star
.text
.XTextDocument
;
37 import com
.sun
.star
.graphic
.XGraphic
;
38 import com
.sun
.star
.graphic
.XGraphicProvider
;
39 import com
.sun
.star
.graphic
.GraphicProvider
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.XComponentContext
;
44 public class WriterTools
{
45 public static XTextDocument
createTextDoc(XMultiServiceFactory xMSF
) {
46 PropertyValue
[] Args
= new PropertyValue
[0];
47 XComponent comp
= DesktopTools
.openNewDoc(xMSF
, "swriter", Args
);
48 XTextDocument WriterDoc
= UnoRuntime
.queryInterface(
49 XTextDocument
.class, comp
);
52 } // finish createTextDoc
54 public static XTextDocument
loadTextDoc(XMultiServiceFactory xMSF
,
56 PropertyValue
[] Args
= new PropertyValue
[0];
57 XTextDocument WriterDoc
= loadTextDoc(xMSF
, url
, Args
);
60 } // finish createTextDoc
62 private static XTextDocument
loadTextDoc(XMultiServiceFactory xMSF
,
63 String url
, PropertyValue
[] Args
) {
64 XComponent comp
= DesktopTools
.loadDoc(xMSF
, url
, Args
);
65 XTextDocument WriterDoc
= UnoRuntime
.queryInterface(
66 XTextDocument
.class, comp
);
69 } // finish createTextDoc
71 public static XDrawPage
getDrawPage(XTextDocument aDoc
) {
75 XDrawPageSupplier oDPS
= UnoRuntime
.queryInterface(
76 XDrawPageSupplier
.class, aDoc
);
77 oDP
= oDPS
.getDrawPage();
78 } catch (Exception e
) {
79 throw new IllegalArgumentException("Couldn't get drawpage", e
);
85 public static void insertTextGraphic(XTextDocument aDoc
,
86 XMultiServiceFactory xMSF
, XComponentContext xContext
, int hpos
,
87 int vpos
, int width
, int height
,
88 String pic
, String name
) {
90 Object oGObject
= xMSF
.createInstance(
91 "com.sun.star.text.GraphicObject");
93 XGraphicProvider xGraphicProvider
= UnoRuntime
.queryInterface(
94 XGraphicProvider
.class,
95 xContext
.getServiceManager().createInstanceWithContext(
96 "com.sun.star.graphic.GraphicProvider", xContext
));
98 String fullURL
= util
.utils
.getFullTestURL(pic
);
100 PropertyValue
[] aMediaProps
= new PropertyValue
[] { new PropertyValue() };
101 aMediaProps
[0].Name
= "URL";
102 aMediaProps
[0].Value
= fullURL
;
104 XGraphic xGraphic
= UnoRuntime
.queryInterface(XGraphic
.class,
105 xGraphicProvider
.queryGraphic(aMediaProps
));
107 XText the_text
= aDoc
.getText();
108 XTextCursor the_cursor
= the_text
.createTextCursor();
109 XTextContent the_content
= UnoRuntime
.queryInterface(
110 XTextContent
.class, oGObject
);
111 the_text
.insertTextContent(the_cursor
, the_content
, true);
113 XPropertySet oProps
= UnoRuntime
.queryInterface(
114 XPropertySet
.class, oGObject
);
116 oProps
.setPropertyValue("Graphic", xGraphic
);
117 oProps
.setPropertyValue("HoriOrientPosition", Integer
.valueOf(hpos
));
118 oProps
.setPropertyValue("VertOrientPosition", Integer
.valueOf(vpos
));
119 oProps
.setPropertyValue("Width", Integer
.valueOf(width
));
120 oProps
.setPropertyValue("Height", Integer
.valueOf(height
));
122 XNamed the_name
= UnoRuntime
.queryInterface(XNamed
.class,
124 the_name
.setName(name
);
125 } catch (Exception ex
) {
126 System
.out
.println("Exception while inserting TextGraphic");
127 ex
.printStackTrace();