1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import com.sun.star.awt.Point;
25cdf0e10cSrcweir import com.sun.star.awt.Size;
26cdf0e10cSrcweir import com.sun.star.awt.FontWeight;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
29cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
30cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
31cdf0e10cSrcweir import com.sun.star.beans.XPropertyState;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.connection.XConnector;
38cdf0e10cSrcweir import com.sun.star.connection.XConnection;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
41cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
42cdf0e10cSrcweir import com.sun.star.container.XNamed;
43cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
44cdf0e10cSrcweir import com.sun.star.container.XIndexReplace;
45cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
46cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir import com.sun.star.drawing.XShape;
49cdf0e10cSrcweir import com.sun.star.drawing.XShapeGrouper;
50cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
51cdf0e10cSrcweir import com.sun.star.drawing.XDrawPageSupplier;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
54cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
55cdf0e10cSrcweir import com.sun.star.frame.XModel;
56cdf0e10cSrcweir import com.sun.star.frame.XController;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir import com.sun.star.lang.XComponent;
59cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
60cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
61cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir import com.sun.star.style.NumberingType;
64cdf0e10cSrcweir import com.sun.star.style.XStyle;
65cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir import com.sun.star.table.XCell;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
70cdf0e10cSrcweir import com.sun.star.text.ReferenceFieldSource;
71cdf0e10cSrcweir import com.sun.star.text.ReferenceFieldPart;
72cdf0e10cSrcweir import com.sun.star.text.TextColumn;
73cdf0e10cSrcweir import com.sun.star.text.TextContentAnchorType;
74cdf0e10cSrcweir import com.sun.star.text.XAutoTextContainer;
75cdf0e10cSrcweir import com.sun.star.text.XAutoTextGroup;
76cdf0e10cSrcweir import com.sun.star.text.XAutoTextEntry;
77cdf0e10cSrcweir import com.sun.star.text.XDependentTextField;
78cdf0e10cSrcweir import com.sun.star.text.XDocumentIndex;
79cdf0e10cSrcweir import com.sun.star.text.XFootnote;
80cdf0e10cSrcweir import com.sun.star.text.XFootnotesSupplier;
81cdf0e10cSrcweir import com.sun.star.text.XParagraphCursor;
82cdf0e10cSrcweir import com.sun.star.text.XReferenceMarksSupplier;
83cdf0e10cSrcweir import com.sun.star.text.XRelativeTextContentInsert;
84cdf0e10cSrcweir import com.sun.star.text.XSentenceCursor;
85cdf0e10cSrcweir import com.sun.star.text.XSimpleText;
86cdf0e10cSrcweir import com.sun.star.text.XText;
87cdf0e10cSrcweir import com.sun.star.text.XTextColumns;
88cdf0e10cSrcweir import com.sun.star.text.XTextContent;
89cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
90cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
91cdf0e10cSrcweir import com.sun.star.text.XTextField;
92cdf0e10cSrcweir import com.sun.star.text.XTextFrame;
93cdf0e10cSrcweir import com.sun.star.text.XTextRange;
94cdf0e10cSrcweir import com.sun.star.text.XTextSection;
95cdf0e10cSrcweir import com.sun.star.text.XTextTable;
96cdf0e10cSrcweir import com.sun.star.text.XTextTableCursor;
97cdf0e10cSrcweir import com.sun.star.text.XTextTablesSupplier;
98cdf0e10cSrcweir import com.sun.star.text.XTextFieldsSupplier;
99cdf0e10cSrcweir import com.sun.star.text.XBookmarksSupplier;
100cdf0e10cSrcweir import com.sun.star.text.XTextViewCursorSupplier;
101cdf0e10cSrcweir import com.sun.star.text.XTextViewCursor;
102cdf0e10cSrcweir import com.sun.star.text.XPageCursor;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir import com.sun.star.text.XWordCursor;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
107cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
108cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
109cdf0e10cSrcweir import com.sun.star.uno.XInterface;
110cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir import com.sun.star.util.XRefreshable;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir import com.sun.star.frame.XStorable;
115cdf0e10cSrcweir import com.sun.star.view.XPrintable;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir import java.lang.Math;
118cdf0e10cSrcweir import java.util.Random;
119cdf0e10cSrcweir import java.util.Hashtable;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir /*
122cdf0e10cSrcweir  * TextDocuments.java
123cdf0e10cSrcweir  *
124cdf0e10cSrcweir  * Created on 11. April 2002, 08:47
125cdf0e10cSrcweir  */
126cdf0e10cSrcweir 
127cdf0e10cSrcweir /**
128cdf0e10cSrcweir  *
129cdf0e10cSrcweir  * @author  Martin Gallwey, Dietrich Schulten
130cdf0e10cSrcweir  */
131cdf0e10cSrcweir public class TextDocuments {
132cdf0e10cSrcweir     // adjust these constant to your local printer!
133cdf0e10cSrcweir     private static String sOutputDir;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     private String aPrinterName = "\\\\so-print\\xml3sof";
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     private XComponentContext mxRemoteContext = null;
138cdf0e10cSrcweir     private XMultiComponentFactory mxRemoteServiceManager = null;
139cdf0e10cSrcweir     private XTextDocument mxDoc = null;
140cdf0e10cSrcweir     private XMultiServiceFactory mxDocFactory = null;
141cdf0e10cSrcweir     private XMultiServiceFactory mxFactory = null;
142cdf0e10cSrcweir     private XPropertySet  mxDocProps = null;
143cdf0e10cSrcweir     private XText mxDocText = null;
144cdf0e10cSrcweir     private XTextCursor mxDocCursor = null;
145cdf0e10cSrcweir     private XTextContent mxFishSection = null;
146cdf0e10cSrcweir     private Random maRandom = null;
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     /** Creates a new instance of TextDocuments */
TextDocuments()149cdf0e10cSrcweir     public TextDocuments() {
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     /**
153cdf0e10cSrcweir      * @param args the command line arguments
154cdf0e10cSrcweir      */
main(String[] args)155cdf0e10cSrcweir     public static void main(String[] args) {
156cdf0e10cSrcweir     TextDocuments textDocuments1 = new TextDocuments();
157cdf0e10cSrcweir     try {
158cdf0e10cSrcweir             // output directory for store test;
159cdf0e10cSrcweir             sOutputDir = args[0];
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             textDocuments1.runDemo();
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir         catch (java.lang.Exception e){
164cdf0e10cSrcweir             System.out.println(e.getMessage());
165cdf0e10cSrcweir             e.printStackTrace();
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         finally {
168cdf0e10cSrcweir             System.exit(0);
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
runDemo()172cdf0e10cSrcweir     protected void runDemo() throws java.lang.Exception {
173cdf0e10cSrcweir             storePrintExample(); // depends on printer name
174cdf0e10cSrcweir             templateExample();
175cdf0e10cSrcweir             viewCursorExample(); // makes changes to the current document,
176cdf0e10cSrcweir                                  // use with care
177cdf0e10cSrcweir             editingExample();
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     /** Sample for use of templates
181cdf0e10cSrcweir      *  This sample uses the file TextTemplateWithUserFields.odt from the Samples
182cdf0e10cSrcweir      *  folder. The file contains a number of User text fields (Variables - User)
183cdf0e10cSrcweir      *  and a bookmark which we use to fill in various values
184cdf0e10cSrcweir      */
templateExample()185cdf0e10cSrcweir     protected void templateExample() throws java.lang.Exception {
186cdf0e10cSrcweir         // create a small hashtable that simulates a rowset
187cdf0e10cSrcweir         Hashtable recipient = new Hashtable();
188cdf0e10cSrcweir         recipient.put("Company", "Manatee Books");
189cdf0e10cSrcweir         recipient.put("Contact", "Rod Martin");
190cdf0e10cSrcweir         recipient.put("ZIP", "34567");
191cdf0e10cSrcweir         recipient.put("City", "Fort Lauderdale");
192cdf0e10cSrcweir         recipient.put("State", "Florida");
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         // load template with User fields and bookmark
195cdf0e10cSrcweir         java.io.File sourceFile = new java.io.File("TextTemplateWithUserFields.odt");
196cdf0e10cSrcweir         StringBuffer sTemplateFileUrl = new StringBuffer("file:///");
197cdf0e10cSrcweir         sTemplateFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
198cdf0e10cSrcweir 
199cdf0e10cSrcweir         XComponent xTemplateComponent =
200cdf0e10cSrcweir             newDocComponentFromTemplate( sTemplateFileUrl.toString() );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         // get XTextFieldsSupplier, XBookmarksSupplier interfaces
203cdf0e10cSrcweir         XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)
204cdf0e10cSrcweir             UnoRuntime.queryInterface(XTextFieldsSupplier.class,
205cdf0e10cSrcweir                                       xTemplateComponent);
206cdf0e10cSrcweir         XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)
207cdf0e10cSrcweir             UnoRuntime.queryInterface(XBookmarksSupplier.class, xTemplateComponent);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         // access the TextFields and the TextFieldMasters collections
210cdf0e10cSrcweir         XNameAccess xNamedFieldMasters = xTextFieldsSupplier.getTextFieldMasters();
211cdf0e10cSrcweir         XEnumerationAccess xEnumeratedFields = xTextFieldsSupplier.getTextFields();
212cdf0e10cSrcweir 
213cdf0e10cSrcweir         // iterate over hashtable and insert values into field masters
214cdf0e10cSrcweir         java.util.Enumeration keys = recipient.keys();
215cdf0e10cSrcweir         while(keys.hasMoreElements()) {
216cdf0e10cSrcweir             // get column name
217cdf0e10cSrcweir             String key = (String)keys.nextElement();
218cdf0e10cSrcweir 
219cdf0e10cSrcweir             // access corresponding field master
220cdf0e10cSrcweir             Object fieldMaster = xNamedFieldMasters.getByName(
221cdf0e10cSrcweir                 "com.sun.star.text.fieldmaster.User." + key);
222cdf0e10cSrcweir 
223cdf0e10cSrcweir             // query the XPropertySet interface, we need to set the Content property
224cdf0e10cSrcweir             XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
225cdf0e10cSrcweir                 XPropertySet.class, fieldMaster);
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             // insert the column value into field master
228cdf0e10cSrcweir             xPropertySet.setPropertyValue("Content", recipient.get(key));
229cdf0e10cSrcweir         }
230cdf0e10cSrcweir         // afterwards we must refresh the textfields collection
231cdf0e10cSrcweir         XRefreshable xRefreshable = (XRefreshable)UnoRuntime.queryInterface(
232cdf0e10cSrcweir             XRefreshable.class, xEnumeratedFields);
233cdf0e10cSrcweir         xRefreshable.refresh();
234cdf0e10cSrcweir 
235cdf0e10cSrcweir         // accessing the Bookmarks collection of the document
236cdf0e10cSrcweir         XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
237cdf0e10cSrcweir 
238cdf0e10cSrcweir         // find the bookmark named "Subscription"
239cdf0e10cSrcweir         Object bookmark = xNamedBookmarks.getByName("Subscription");
240cdf0e10cSrcweir         // we need its XTextRange which is available from getAnchor(),
241cdf0e10cSrcweir         // so query for XTextContent
242cdf0e10cSrcweir         XTextContent xBookmarkContent = (XTextContent)UnoRuntime.queryInterface(
243cdf0e10cSrcweir             XTextContent.class, bookmark);
244cdf0e10cSrcweir         // get the anchor of the bookmark (its XTextRange)
245cdf0e10cSrcweir         XTextRange xBookmarkRange = xBookmarkContent.getAnchor();
246cdf0e10cSrcweir         // set string at the bookmark position
247cdf0e10cSrcweir         xBookmarkRange.setString("subscription for the Manatee Journal");
248cdf0e10cSrcweir 
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     /** Sample for document changes, starting at the current view cursor position
252cdf0e10cSrcweir      *  The sample changes the paragraph style and the character style at the
253cdf0e10cSrcweir      *  current view cursor selection Open the sample file ViewCursorExampleFile,
254cdf0e10cSrcweir      *  select some text and run the example.
255cdf0e10cSrcweir      *  The current paragraph will be set to Quotations paragraph style.
256cdf0e10cSrcweir      *  The selected text will be set to Quotation character style.
257cdf0e10cSrcweir      */
viewCursorExample()258cdf0e10cSrcweir     private void viewCursorExample() throws java.lang.Exception {
259cdf0e10cSrcweir         // get the remote service manager
260cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
261cdf0e10cSrcweir         // get the Desktop service
262cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
263cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
264cdf0e10cSrcweir         // query its XDesktop interface, we need the current component
265cdf0e10cSrcweir         XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
266cdf0e10cSrcweir             XDesktop.class, desktop);
267cdf0e10cSrcweir         // retrieve the current component and access the controller
268cdf0e10cSrcweir         XComponent xCurrentComponent = xDesktop.getCurrentComponent();
269cdf0e10cSrcweir         XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class,
270cdf0e10cSrcweir                                                           xCurrentComponent);
271cdf0e10cSrcweir         XController xController = xModel.getCurrentController();
272cdf0e10cSrcweir         // the controller gives us the TextViewCursor
273cdf0e10cSrcweir         XTextViewCursorSupplier xViewCursorSupplier =
274cdf0e10cSrcweir             (XTextViewCursorSupplier)UnoRuntime.queryInterface(
275cdf0e10cSrcweir                 XTextViewCursorSupplier.class, xController);
276cdf0e10cSrcweir         XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
277cdf0e10cSrcweir 
278cdf0e10cSrcweir         // query its XPropertySet interface, we want to set character and paragraph
279cdf0e10cSrcweir         // properties
280cdf0e10cSrcweir         XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
281cdf0e10cSrcweir             XPropertySet.class, xViewCursor);
282cdf0e10cSrcweir         // set the appropriate properties for character and paragraph style
283cdf0e10cSrcweir         xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
284cdf0e10cSrcweir         xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");
285cdf0e10cSrcweir         // print the current page number
286cdf0e10cSrcweir         XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
287cdf0e10cSrcweir             XPageCursor.class, xViewCursor);
288cdf0e10cSrcweir         System.out.println("The current page number is " + xPageCursor.getPage());
289cdf0e10cSrcweir         // the model cursor is much more powerful, so
290cdf0e10cSrcweir         // we create a model cursor at the current view cursor position with the
291cdf0e10cSrcweir         // following steps:
292cdf0e10cSrcweir         // get the Text service from the TextViewCursor, it is an XTextRange:
293cdf0e10cSrcweir          XText xDocumentText = xViewCursor.getText();
294cdf0e10cSrcweir         // create a model cursor from the viewcursor
295cdf0e10cSrcweir         XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(
296cdf0e10cSrcweir             xViewCursor.getStart());
297cdf0e10cSrcweir         // now we could query XWordCursor, XSentenceCursor and XParagraphCursor
298cdf0e10cSrcweir         // or XDocumentInsertable, XSortable or XContentEnumerationAccess
299cdf0e10cSrcweir         // and work with the properties of com.sun.star.text.TextCursor
300cdf0e10cSrcweir         // in this case we just go to the end of the paragraph and add some text.
301cdf0e10cSrcweir         XParagraphCursor xParagraphCursor = (XParagraphCursor)
302cdf0e10cSrcweir             UnoRuntime.queryInterface(XParagraphCursor.class, xModelCursor);
303cdf0e10cSrcweir         // goto the end of the paragraph
304cdf0e10cSrcweir         xParagraphCursor.gotoEndOfParagraph(false);
305cdf0e10cSrcweir         xParagraphCursor.setString(" ***** Fin de semana! ******");
306cdf0e10cSrcweir     }
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 
309cdf0e10cSrcweir     /** Sample for the various editing facilities described in the
310cdf0e10cSrcweir      * developer's manual
311cdf0e10cSrcweir      */
editingExample()312cdf0e10cSrcweir     private void editingExample () throws java.lang.Exception {
313cdf0e10cSrcweir         // create empty swriter document
314cdf0e10cSrcweir         XComponent xEmptyWriterComponent = newDocComponent("swriter");
315cdf0e10cSrcweir         // query its XTextDocument interface to get the text
316cdf0e10cSrcweir         mxDoc = (XTextDocument)UnoRuntime.queryInterface(
317cdf0e10cSrcweir             XTextDocument.class, xEmptyWriterComponent);
318cdf0e10cSrcweir 
319cdf0e10cSrcweir         // get a reference to the body text of the document
320cdf0e10cSrcweir         mxDocText = mxDoc.getText();
321cdf0e10cSrcweir 
322cdf0e10cSrcweir         // Get a reference to the document's property set. This contains document
323cdf0e10cSrcweir         // information like the current word count
324cdf0e10cSrcweir         mxDocProps = (XPropertySet) UnoRuntime.queryInterface(
325cdf0e10cSrcweir             XPropertySet.class, mxDoc );
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         // Simple text insertion example
328cdf0e10cSrcweir         BodyTextExample ();
329cdf0e10cSrcweir         // Example using text ranges to insert strings at the beginning or end
330cdf0e10cSrcweir         // of a text range
331cdf0e10cSrcweir         TextRangeExample ();
332cdf0e10cSrcweir         // Create a document cursor and remember it, it will be used in most
333cdf0e10cSrcweir         // of the following examples
334cdf0e10cSrcweir         mxDocCursor = mxDocText.createTextCursor();
335cdf0e10cSrcweir         // Demonstrate some of the different cursor types (word, sentence)
336cdf0e10cSrcweir         TextCursorExample ();
337cdf0e10cSrcweir 
338cdf0e10cSrcweir         // Access the text document's multi service factory, which we will need
339cdf0e10cSrcweir         // for most of the following examples
340cdf0e10cSrcweir         mxDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
341cdf0e10cSrcweir             XMultiServiceFactory.class, mxDoc );
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         // Examples of text fields, dependant text fields and field masters
344cdf0e10cSrcweir         TextFieldExample ();
345cdf0e10cSrcweir 
346cdf0e10cSrcweir         // Example of using an XEnumerationAccess to iterate over paragraphs and
347cdf0e10cSrcweir         // set properties of each paragraph as we do so
348cdf0e10cSrcweir         ParagraphExample ();
349cdf0e10cSrcweir 
350cdf0e10cSrcweir         // Example of creating and manipulating a text frame
351cdf0e10cSrcweir         TextFrameExample ();
352cdf0e10cSrcweir 
353cdf0e10cSrcweir         // Example of creating and manipulating a text table, text table rows
354cdf0e10cSrcweir         // and text table cells get a new random generator
355cdf0e10cSrcweir         maRandom = new Random();
356cdf0e10cSrcweir         TextTableExample ();
357cdf0e10cSrcweir 
358cdf0e10cSrcweir         // Example of creating, inserting and manipulating text sections, as
359cdf0e10cSrcweir         // well as an example of how to refresh the document
360cdf0e10cSrcweir         TextSectionExample ();
361cdf0e10cSrcweir 
362cdf0e10cSrcweir         // Example of creating a text section over a block of text and formatting
363cdf0e10cSrcweir         // the text section into columns, as well as how to insert an empty
364cdf0e10cSrcweir         // paragraph using the XRelativeTextContentInsert
365cdf0e10cSrcweir         TextColumnsExample ();
366cdf0e10cSrcweir 
367cdf0e10cSrcweir         // Example of creating the NumberingRules service and adjusting
368cdf0e10cSrcweir         // NumberingTypes and NumberingLevels
369cdf0e10cSrcweir         NumberingExample ();
370cdf0e10cSrcweir 
371cdf0e10cSrcweir         // Example of how to use the XStyleFamiliesSupplier interface of the
372cdf0e10cSrcweir         // document and how to create, insert and apply styles
373cdf0e10cSrcweir         StylesExample ();
374cdf0e10cSrcweir         IndexExample ();
375cdf0e10cSrcweir 
376cdf0e10cSrcweir         // Example of how to create and manipulate reference marks and GetReference
377cdf0e10cSrcweir         // text fields
378cdf0e10cSrcweir         ReferenceExample ();
379cdf0e10cSrcweir 
380cdf0e10cSrcweir         // Example of how to create and insert Footnotes and how to use the
381cdf0e10cSrcweir         // XFootnotesSupplier interface of the document
382cdf0e10cSrcweir         FootnoteExample ();
383cdf0e10cSrcweir 
384cdf0e10cSrcweir         // This method demonstrates how to create shapes from the document factory
385cdf0e10cSrcweir         // and how to access the draw page of the document using the
386cdf0e10cSrcweir         // XDrawPageSupplier interface
387cdf0e10cSrcweir         DrawPageExample ();
388cdf0e10cSrcweir 
389cdf0e10cSrcweir         mxFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
390cdf0e10cSrcweir             XMultiServiceFactory.class, mxRemoteServiceManager);
391cdf0e10cSrcweir         // This example demonstrates the use of the AutoTextContainer,
392cdf0e10cSrcweir         // AutoTextGroup and AutoTextEntry services and shows how to create,
393cdf0e10cSrcweir         // insert and modify auto text blocks
394cdf0e10cSrcweir         AutoTextExample ();
395cdf0e10cSrcweir     }
396cdf0e10cSrcweir 
storePrintExample()397cdf0e10cSrcweir     protected void storePrintExample() throws java.lang.Exception {
398cdf0e10cSrcweir         // get the remote service manager
399cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
400cdf0e10cSrcweir         // retrieve the Desktop object, we need its XComponentLoader
401cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
402cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
403cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
404cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
405cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[0];
406cdf0e10cSrcweir 
407cdf0e10cSrcweir         java.io.File sourceFile = new java.io.File("PrintDemo.odt");
408cdf0e10cSrcweir         StringBuffer sLoadFileUrl = new StringBuffer("file:///");
409cdf0e10cSrcweir         sLoadFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
410cdf0e10cSrcweir 
411cdf0e10cSrcweir         XComponent xDoc = xComponentLoader.loadComponentFromURL(
412cdf0e10cSrcweir             sLoadFileUrl.toString(), "_blank", 0, loadProps);
413cdf0e10cSrcweir 
414cdf0e10cSrcweir         if ( xDoc != null ) {
415cdf0e10cSrcweir             sourceFile = new java.io.File(sOutputDir);
416cdf0e10cSrcweir             StringBuffer sStoreFileUrl = new StringBuffer();
417cdf0e10cSrcweir             sStoreFileUrl.append(sourceFile.toURL().toString());
418cdf0e10cSrcweir             sStoreFileUrl.append("somepopularfileformat.doc");
419cdf0e10cSrcweir 
420cdf0e10cSrcweir             storeDocComponent(xDoc, sStoreFileUrl.toString() );
421cdf0e10cSrcweir             printDocComponent(xDoc);
422cdf0e10cSrcweir         }
423cdf0e10cSrcweir     }
424cdf0e10cSrcweir 
getRemoteServiceManager()425cdf0e10cSrcweir     private XMultiComponentFactory getRemoteServiceManager()
426cdf0e10cSrcweir         throws java.lang.Exception
427cdf0e10cSrcweir     {
428cdf0e10cSrcweir         if (mxRemoteContext == null && mxRemoteServiceManager == null) {
429cdf0e10cSrcweir             // get the remote office context. If necessary a new office
430cdf0e10cSrcweir             // process is started
431cdf0e10cSrcweir             mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
432cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
433cdf0e10cSrcweir             mxRemoteServiceManager = mxRemoteContext.getServiceManager();
434cdf0e10cSrcweir         }
435cdf0e10cSrcweir         return mxRemoteServiceManager;
436cdf0e10cSrcweir     }
437cdf0e10cSrcweir 
newDocComponent(String docType)438cdf0e10cSrcweir     protected XComponent newDocComponent(String docType)
439cdf0e10cSrcweir         throws java.lang.Exception
440cdf0e10cSrcweir     {
441cdf0e10cSrcweir         String loadUrl = "private:factory/" + docType;
442cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
443cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
444cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
445cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
446cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
447cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[0];
448cdf0e10cSrcweir         return xComponentLoader.loadComponentFromURL(loadUrl, "_blank",
449cdf0e10cSrcweir                                                      0, loadProps);
450cdf0e10cSrcweir     }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir     /** Load a document as template
453cdf0e10cSrcweir      */
newDocComponentFromTemplate(String loadUrl)454cdf0e10cSrcweir     protected XComponent newDocComponentFromTemplate(String loadUrl)
455cdf0e10cSrcweir         throws java.lang.Exception
456cdf0e10cSrcweir     {
457cdf0e10cSrcweir         // get the remote service manager
458cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
459cdf0e10cSrcweir         // retrieve the Desktop object, we need its XComponentLoader
460cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
461cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
462cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
463cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
464cdf0e10cSrcweir 
465cdf0e10cSrcweir         // define load properties according to com.sun.star.document.MediaDescriptor
466cdf0e10cSrcweir         // the boolean property AsTemplate tells the office to create a new document
467cdf0e10cSrcweir         // from the given file
468cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[1];
469cdf0e10cSrcweir         loadProps[0] = new PropertyValue();
470cdf0e10cSrcweir         loadProps[0].Name = "AsTemplate";
471cdf0e10cSrcweir         loadProps[0].Value = new Boolean(true);
472cdf0e10cSrcweir         // load
473cdf0e10cSrcweir         return xComponentLoader.loadComponentFromURL(loadUrl, "_blank",
474cdf0e10cSrcweir                                                      0, loadProps);
475cdf0e10cSrcweir     }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir     /** Load a document with arguments (text purposes)
478cdf0e10cSrcweir      */
storeDocComponent(XComponent xDoc, String storeUrl)479cdf0e10cSrcweir     protected void storeDocComponent(XComponent xDoc, String storeUrl)
480cdf0e10cSrcweir         throws java.lang.Exception
481cdf0e10cSrcweir     {
482cdf0e10cSrcweir 
483cdf0e10cSrcweir         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(
484cdf0e10cSrcweir             XStorable.class, xDoc);
485cdf0e10cSrcweir         PropertyValue[] storeProps = new PropertyValue[1];
486cdf0e10cSrcweir         storeProps[0] = new PropertyValue();
487cdf0e10cSrcweir         storeProps[0].Name = "FilterName";
488cdf0e10cSrcweir         storeProps[0].Value = "MS Word 97";
489cdf0e10cSrcweir 
490cdf0e10cSrcweir         System.out.println("... store \"PrintDemo.odt\" to \"" + storeUrl + "\".");
491cdf0e10cSrcweir         xStorable.storeAsURL(storeUrl, storeProps);
492cdf0e10cSrcweir     }
493cdf0e10cSrcweir 
printDocComponent(XComponent xDoc)494cdf0e10cSrcweir     protected void printDocComponent(XComponent xDoc) throws java.lang.Exception {
495cdf0e10cSrcweir         XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(
496cdf0e10cSrcweir             XPrintable.class, xDoc);
497cdf0e10cSrcweir         PropertyValue[] printerDesc = new PropertyValue[1];
498cdf0e10cSrcweir         printerDesc[0] = new PropertyValue();
499cdf0e10cSrcweir         printerDesc[0].Name = "Name";
500cdf0e10cSrcweir         printerDesc[0].Value = aPrinterName;
501cdf0e10cSrcweir 
502cdf0e10cSrcweir         xPrintable.setPrinter(printerDesc);
503cdf0e10cSrcweir 
504cdf0e10cSrcweir         PropertyValue[] printOpts = new PropertyValue[1];
505cdf0e10cSrcweir         printOpts[0] = new PropertyValue();
506cdf0e10cSrcweir         printOpts[0].Name = "Pages";
507cdf0e10cSrcweir         printOpts[0].Value = "1";
508cdf0e10cSrcweir 
509cdf0e10cSrcweir         xPrintable.print(printOpts);
510cdf0e10cSrcweir     }
511cdf0e10cSrcweir 
512cdf0e10cSrcweir     // Setting the whole text of a document as one string
BodyTextExample()513cdf0e10cSrcweir     protected void BodyTextExample ()
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir         // Body Text and TextDocument example
516cdf0e10cSrcweir         try
517cdf0e10cSrcweir         {
518cdf0e10cSrcweir             // demonstrate simple text insertion
519cdf0e10cSrcweir             mxDocText.setString ( "This is the new body text of the document."
520cdf0e10cSrcweir                                   + "\n\nThis is on the second line.\n\n" );
521cdf0e10cSrcweir         }
522cdf0e10cSrcweir         catch ( Exception e )
523cdf0e10cSrcweir         {
524cdf0e10cSrcweir             e.printStackTrace();
525cdf0e10cSrcweir         }
526cdf0e10cSrcweir     }
527cdf0e10cSrcweir 
528cdf0e10cSrcweir     // Adding a string at the end or the beginning of text
TextRangeExample()529cdf0e10cSrcweir     protected void TextRangeExample ()
530cdf0e10cSrcweir     {
531cdf0e10cSrcweir         try
532cdf0e10cSrcweir         {
533cdf0e10cSrcweir             // Get a text range refering to the beginning of the text document
534cdf0e10cSrcweir             XTextRange xStart = mxDocText.getStart();
535cdf0e10cSrcweir             // use setString to insert text at the beginning
536cdf0e10cSrcweir             xStart.setString ( "This is text inserted at the beginning.\n\n" );
537cdf0e10cSrcweir             // Get a text range refering to the end of the text document
538cdf0e10cSrcweir             XTextRange xEnd = mxDocText.getEnd();
539cdf0e10cSrcweir             // use setString to insert text at the end
540cdf0e10cSrcweir             xEnd.setString ( "This is text inserted at the end.\n\n" );
541cdf0e10cSrcweir         }
542cdf0e10cSrcweir         catch ( Exception e )
543cdf0e10cSrcweir         {
544cdf0e10cSrcweir             e.printStackTrace();
545cdf0e10cSrcweir         }
546cdf0e10cSrcweir     }
547cdf0e10cSrcweir 
548cdf0e10cSrcweir     /** moving a text cursor, selecting text and overwriting it
549cdf0e10cSrcweir     */
TextCursorExample()550cdf0e10cSrcweir     protected void TextCursorExample ()
551cdf0e10cSrcweir     {
552cdf0e10cSrcweir         try
553cdf0e10cSrcweir         {
554cdf0e10cSrcweir             // First, get the XSentenceCursor interface of our text cursor
555cdf0e10cSrcweir             XSentenceCursor xSentenceCursor = (XSentenceCursor)
556cdf0e10cSrcweir                 UnoRuntime.queryInterface(XSentenceCursor.class, mxDocCursor );
557cdf0e10cSrcweir             // Goto the next cursor, without selecting it
558cdf0e10cSrcweir             xSentenceCursor.gotoNextSentence( false );
559cdf0e10cSrcweir             // Get the XWordCursor interface of our text cursor
560cdf0e10cSrcweir             XWordCursor xWordCursor = (XWordCursor) UnoRuntime.queryInterface(
561cdf0e10cSrcweir                 XWordCursor.class, mxDocCursor );
562cdf0e10cSrcweir             // Skip the first four words of this sentence and select the fifth
563cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
564cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
565cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
566cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
567cdf0e10cSrcweir             xWordCursor.gotoNextWord( true );
568cdf0e10cSrcweir             // Use the XSimpleText interface to insert a word at the current cursor
569cdf0e10cSrcweir             // location, over-writing the current selection (the fifth word
570cdf0e10cSrcweir             // selected above)
571cdf0e10cSrcweir             mxDocText.insertString ( xWordCursor, "old ", true );
572cdf0e10cSrcweir 
573cdf0e10cSrcweir             // Access the property set of the cursor, and set the currently
574cdf0e10cSrcweir             // selected text (which is the string we just inserted) to be bold
575cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
576cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
577cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "CharWeight",
578cdf0e10cSrcweir                              new Float(com.sun.star.awt.FontWeight.BOLD) );
579cdf0e10cSrcweir 
580cdf0e10cSrcweir             // replace the '.' at the end of the sentence with a new string
581cdf0e10cSrcweir             xSentenceCursor.gotoEndOfSentence( false );
582cdf0e10cSrcweir             xWordCursor.gotoPreviousWord( true );
583cdf0e10cSrcweir             mxDocText.insertString (xWordCursor,
584cdf0e10cSrcweir                                     ", which has been changed with text cursors!",
585cdf0e10cSrcweir                                     true);
586cdf0e10cSrcweir         }
587cdf0e10cSrcweir         catch ( Exception e )
588cdf0e10cSrcweir         {
589cdf0e10cSrcweir             e.printStackTrace();
590cdf0e10cSrcweir         }
591cdf0e10cSrcweir     }
592cdf0e10cSrcweir 
593cdf0e10cSrcweir     /** This method inserts both a date field and a user field containing the
594cdf0e10cSrcweir      * number '42'
595cdf0e10cSrcweir      */
TextFieldExample()596cdf0e10cSrcweir     protected void TextFieldExample ()
597cdf0e10cSrcweir     {
598cdf0e10cSrcweir         try
599cdf0e10cSrcweir         {
600cdf0e10cSrcweir             // Use the text document's factory to create a DateTime text field,
601cdf0e10cSrcweir             // and access it's XTextField interface
602cdf0e10cSrcweir             XTextField xDateField = (XTextField) UnoRuntime.queryInterface (
603cdf0e10cSrcweir                 XTextField.class, mxDocFactory.createInstance (
604cdf0e10cSrcweir                     "com.sun.star.text.TextField.DateTime" ) );
605cdf0e10cSrcweir 
606cdf0e10cSrcweir             // Insert it at the end of the document
607cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocText.getEnd(), xDateField, false );
608cdf0e10cSrcweir 
609cdf0e10cSrcweir             // Use the text document's factory to create a user text field,
610cdf0e10cSrcweir             // and access it's XDependentTextField interface
611cdf0e10cSrcweir             XDependentTextField xUserField =
612cdf0e10cSrcweir                 (XDependentTextField) UnoRuntime.queryInterface (
613cdf0e10cSrcweir                     XDependentTextField.class, mxDocFactory.createInstance (
614cdf0e10cSrcweir                         "com.sun.star.text.TextField.User" ) );
615cdf0e10cSrcweir 
616cdf0e10cSrcweir             // Create a fieldmaster for our newly created User Text field, and
617cdf0e10cSrcweir             // access it's XPropertySet interface
618cdf0e10cSrcweir             XPropertySet xMasterPropSet = (XPropertySet)UnoRuntime.queryInterface(
619cdf0e10cSrcweir                 XPropertySet.class, mxDocFactory.createInstance (
620cdf0e10cSrcweir                     "com.sun.star.text.fieldmaster.User" ) );
621cdf0e10cSrcweir 
622cdf0e10cSrcweir             // Set the name and value of the FieldMaster
623cdf0e10cSrcweir             xMasterPropSet.setPropertyValue ( "Name", "UserEmperor" );
624cdf0e10cSrcweir             xMasterPropSet.setPropertyValue ( "Value", new Integer ( 42 ) );
625cdf0e10cSrcweir 
626cdf0e10cSrcweir             // Attach the field master to the user field
627cdf0e10cSrcweir             xUserField.attachTextFieldMaster ( xMasterPropSet );
628cdf0e10cSrcweir 
629cdf0e10cSrcweir             // Move the cursor to the end of the document
630cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
631cdf0e10cSrcweir             // insert a paragraph break using the XSimpleText interface
632cdf0e10cSrcweir             mxDocText.insertControlCharacter (
633cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
634cdf0e10cSrcweir 
635cdf0e10cSrcweir             // Insert the user field at the end of the document
636cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocText.getEnd(), xUserField, false );
637cdf0e10cSrcweir         }
638cdf0e10cSrcweir         catch ( Exception e )
639cdf0e10cSrcweir         {
640cdf0e10cSrcweir             e.printStackTrace();
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir     }
643cdf0e10cSrcweir 
644cdf0e10cSrcweir     /** This method demonstrates how to iterate over paragraphs
645cdf0e10cSrcweir      */
ParagraphExample()646cdf0e10cSrcweir     protected void ParagraphExample ()
647cdf0e10cSrcweir     {
648cdf0e10cSrcweir         try
649cdf0e10cSrcweir         {
650cdf0e10cSrcweir             // The service 'com.sun.star.text.Text' supports the XEnumerationAccess
651cdf0e10cSrcweir             // interface to provide an enumeration of the paragraphs contained by
652cdf0e10cSrcweir             // the text the service refers to.
653cdf0e10cSrcweir 
654cdf0e10cSrcweir             // Here, we access this interface
655cdf0e10cSrcweir             XEnumerationAccess xParaAccess = (XEnumerationAccess)
656cdf0e10cSrcweir                 UnoRuntime.queryInterface(XEnumerationAccess.class, mxDocText );
657cdf0e10cSrcweir             // Call the XEnumerationAccess's only method to access the actual
658cdf0e10cSrcweir             // Enumeration
659cdf0e10cSrcweir             XEnumeration xParaEnum = xParaAccess.createEnumeration();
660cdf0e10cSrcweir 
661cdf0e10cSrcweir             // While there are paragraphs, do things to them
662cdf0e10cSrcweir             while ( xParaEnum.hasMoreElements() )
663cdf0e10cSrcweir             {
664cdf0e10cSrcweir                 // Get a reference to the next paragraphs XServiceInfo interface.
665cdf0e10cSrcweir                 // TextTables are also part of this enumeration access, so we ask
666cdf0e10cSrcweir                 // the element if it is a TextTable, if it doesn't support the
667cdf0e10cSrcweir                 // com.sun.star.text.TextTable service, then it is safe to assume
668cdf0e10cSrcweir                 // that it really is a paragraph
669cdf0e10cSrcweir                 XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
670cdf0e10cSrcweir                     XServiceInfo.class, xParaEnum.nextElement() );
671cdf0e10cSrcweir                 if ( !xInfo.supportsService ( "com.sun.star.text.TextTable" ) )
672cdf0e10cSrcweir                 {
673cdf0e10cSrcweir                     // Access the paragraph's property set...the properties in this
674cdf0e10cSrcweir                     // property set are listed in:
675cdf0e10cSrcweir                     // com.sun.star.style.ParagraphProperties
676cdf0e10cSrcweir                     XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(
677cdf0e10cSrcweir                         XPropertySet.class, xInfo );
678cdf0e10cSrcweir                     // Set the justification to be center justified
679cdf0e10cSrcweir                     xSet.setPropertyValue ( "ParaAdjust",
680cdf0e10cSrcweir                              com.sun.star.style.ParagraphAdjust.CENTER );
681cdf0e10cSrcweir                 }
682cdf0e10cSrcweir             }
683cdf0e10cSrcweir         }
684cdf0e10cSrcweir         catch ( Exception e )
685cdf0e10cSrcweir         {
686cdf0e10cSrcweir             e.printStackTrace();
687cdf0e10cSrcweir         }
688cdf0e10cSrcweir     }
689cdf0e10cSrcweir 
690cdf0e10cSrcweir     /** This method returns a random double which isn't too high or too low
691cdf0e10cSrcweir      */
getRandomDouble()692cdf0e10cSrcweir     protected double getRandomDouble ()
693cdf0e10cSrcweir     {
694cdf0e10cSrcweir         return ( ( maRandom.nextInt() % 1000 ) * maRandom.nextDouble () );
695cdf0e10cSrcweir     }
696cdf0e10cSrcweir 
697cdf0e10cSrcweir     /** This method sets the text colour of the cell refered to by sCellName to
698cdf0e10cSrcweir         white and inserts the string sText in it
699cdf0e10cSrcweir      */
insertIntoCell(String sCellName, String sText, XTextTable xTable)700cdf0e10cSrcweir     protected static void insertIntoCell(String sCellName, String sText,
701cdf0e10cSrcweir                                          XTextTable xTable)
702cdf0e10cSrcweir     {
703cdf0e10cSrcweir         // Access the XText interface of the cell referred to by sCellName
704cdf0e10cSrcweir         XText xCellText = (XText) UnoRuntime.queryInterface(
705cdf0e10cSrcweir             XText.class, xTable.getCellByName ( sCellName ) );
706cdf0e10cSrcweir 
707cdf0e10cSrcweir         // create a text cursor from the cells XText interface
708cdf0e10cSrcweir         XTextCursor xCellCursor = xCellText.createTextCursor();
709cdf0e10cSrcweir         // Get the property set of the cell's TextCursor
710cdf0e10cSrcweir         XPropertySet xCellCursorProps = (XPropertySet)UnoRuntime.queryInterface(
711cdf0e10cSrcweir             XPropertySet.class, xCellCursor );
712cdf0e10cSrcweir 
713cdf0e10cSrcweir         try
714cdf0e10cSrcweir         {
715cdf0e10cSrcweir             // Set the colour of the text to white
716cdf0e10cSrcweir             xCellCursorProps.setPropertyValue( "CharColor", new Integer(16777215));
717cdf0e10cSrcweir         }
718cdf0e10cSrcweir         catch ( Exception e)
719cdf0e10cSrcweir         {
720cdf0e10cSrcweir             e.printStackTrace();
721cdf0e10cSrcweir         }
722cdf0e10cSrcweir         // Set the text in the cell to sText
723cdf0e10cSrcweir         xCellText.setString( sText );
724cdf0e10cSrcweir     }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir     /** This method shows how to create and insert a text table, as well as insert
727cdf0e10cSrcweir 		text and formulae into the cells of the table
728cdf0e10cSrcweir      */
TextTableExample()729cdf0e10cSrcweir     protected void TextTableExample ()
730cdf0e10cSrcweir     {
731cdf0e10cSrcweir         try
732cdf0e10cSrcweir         {
733cdf0e10cSrcweir             // Create a new table from the document's factory
734cdf0e10cSrcweir             XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(
735cdf0e10cSrcweir                 XTextTable.class, mxDocFactory .createInstance(
736cdf0e10cSrcweir                     "com.sun.star.text.TextTable" ) );
737cdf0e10cSrcweir 
738cdf0e10cSrcweir             // Specify that we want the table to have 4 rows and 4 columns
739cdf0e10cSrcweir             xTable.initialize( 4, 4 );
740cdf0e10cSrcweir 
741cdf0e10cSrcweir             // Insert the table into the document
742cdf0e10cSrcweir             mxDocText.insertTextContent( mxDocCursor, xTable, false);
743cdf0e10cSrcweir             // Get an XIndexAccess of the table rows
744cdf0e10cSrcweir             XIndexAccess xRows = xTable.getRows();
745cdf0e10cSrcweir 
746cdf0e10cSrcweir             // Access the property set of the first row (properties listed in
747cdf0e10cSrcweir             // service description: com.sun.star.text.TextTableRow)
748cdf0e10cSrcweir             XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface(
749cdf0e10cSrcweir                 XPropertySet.class, xRows.getByIndex ( 0 ) );
750cdf0e10cSrcweir             // If BackTransparant is false, then the background color is visible
751cdf0e10cSrcweir             xRow.setPropertyValue( "BackTransparent", new Boolean(false));
752cdf0e10cSrcweir             // Specify the color of the background to be dark blue
753cdf0e10cSrcweir             xRow.setPropertyValue( "BackColor", new Integer(6710932));
754cdf0e10cSrcweir 
755cdf0e10cSrcweir             // Access the property set of the whole table
756cdf0e10cSrcweir             XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(
757cdf0e10cSrcweir                 XPropertySet.class, xTable );
758cdf0e10cSrcweir             // We want visible background colors
759cdf0e10cSrcweir             xTableProps.setPropertyValue( "BackTransparent", new Boolean(false));
760cdf0e10cSrcweir             // Set the background colour to light blue
761cdf0e10cSrcweir             xTableProps.setPropertyValue( "BackColor", new Integer(13421823));
762cdf0e10cSrcweir 
763cdf0e10cSrcweir             // set the text (and text colour) of all the cells in the first row
764cdf0e10cSrcweir             // of the table
765cdf0e10cSrcweir             insertIntoCell( "A1", "First Column", xTable );
766cdf0e10cSrcweir             insertIntoCell( "B1", "Second Column", xTable );
767cdf0e10cSrcweir             insertIntoCell( "C1", "Third Column", xTable );
768cdf0e10cSrcweir             insertIntoCell( "D1", "Results", xTable );
769cdf0e10cSrcweir 
770cdf0e10cSrcweir             // Insert random numbers into the first this three cells of each
771cdf0e10cSrcweir             // remaining row
772cdf0e10cSrcweir             xTable.getCellByName( "A2" ).setValue( getRandomDouble() );
773cdf0e10cSrcweir             xTable.getCellByName( "B2" ).setValue( getRandomDouble() );
774cdf0e10cSrcweir             xTable.getCellByName( "C2" ).setValue( getRandomDouble() );
775cdf0e10cSrcweir 
776cdf0e10cSrcweir             xTable.getCellByName( "A3" ).setValue( getRandomDouble() );
777cdf0e10cSrcweir             xTable.getCellByName( "B3" ).setValue( getRandomDouble() );
778cdf0e10cSrcweir             xTable.getCellByName( "C3" ).setValue( getRandomDouble() );
779cdf0e10cSrcweir 
780cdf0e10cSrcweir             xTable.getCellByName( "A4" ).setValue( getRandomDouble() );
781cdf0e10cSrcweir             xTable.getCellByName( "B4" ).setValue( getRandomDouble() );
782cdf0e10cSrcweir             xTable.getCellByName( "C4" ).setValue( getRandomDouble() );
783cdf0e10cSrcweir 
784cdf0e10cSrcweir             // Set the last cell in each row to be a formula that calculates
785cdf0e10cSrcweir             // the sum of the first three cells
786cdf0e10cSrcweir             xTable.getCellByName( "D2" ).setFormula( "sum <A2:C2>" );
787cdf0e10cSrcweir             xTable.getCellByName( "D3" ).setFormula( "sum <A3:C3>" );
788cdf0e10cSrcweir             xTable.getCellByName( "D4" ).setFormula( "sum <A4:C4>" );
789cdf0e10cSrcweir         }
790cdf0e10cSrcweir         catch (Exception e)
791cdf0e10cSrcweir         {
792cdf0e10cSrcweir             e.printStackTrace();
793cdf0e10cSrcweir         }
794cdf0e10cSrcweir     }
795cdf0e10cSrcweir     /** This method shows how to create and manipulate text frames
796cdf0e10cSrcweir      */
TextFrameExample()797cdf0e10cSrcweir     protected void TextFrameExample ()
798cdf0e10cSrcweir     {
799cdf0e10cSrcweir         try
800cdf0e10cSrcweir         {
801cdf0e10cSrcweir             // Use the document's factory to create a new text frame and
802cdf0e10cSrcweir             // immediately access it's XTextFrame interface
803cdf0e10cSrcweir             XTextFrame xFrame = (XTextFrame) UnoRuntime.queryInterface (
804cdf0e10cSrcweir                 XTextFrame.class, mxDocFactory.createInstance (
805cdf0e10cSrcweir                     "com.sun.star.text.TextFrame" ) );
806cdf0e10cSrcweir 
807cdf0e10cSrcweir             // Access the XShape interface of the TextFrame
808cdf0e10cSrcweir             XShape xShape = (XShape)UnoRuntime.queryInterface(XShape.class, xFrame);
809cdf0e10cSrcweir             // Access the XPropertySet interface of the TextFrame
810cdf0e10cSrcweir             XPropertySet xFrameProps = (XPropertySet)UnoRuntime.queryInterface(
811cdf0e10cSrcweir                 XPropertySet.class, xFrame );
812cdf0e10cSrcweir 
813cdf0e10cSrcweir             // Set the size of the new Text Frame using the XShape's 'setSize'
814cdf0e10cSrcweir             // method
815cdf0e10cSrcweir             Size aSize = new Size();
816cdf0e10cSrcweir             aSize.Height = 400;
817cdf0e10cSrcweir             aSize.Width = 15000;
818cdf0e10cSrcweir             xShape.setSize(aSize);
819cdf0e10cSrcweir             // Set the AnchorType to
820cdf0e10cSrcweir             // com.sun.star.text.TextContentAnchorType.AS_CHARACTER
821cdf0e10cSrcweir             xFrameProps.setPropertyValue( "AnchorType",
822cdf0e10cSrcweir                                           TextContentAnchorType.AS_CHARACTER );
823cdf0e10cSrcweir             // Go to the end of the text document
824cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
825cdf0e10cSrcweir             // Insert a new paragraph
826cdf0e10cSrcweir             mxDocText.insertControlCharacter (
827cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
828cdf0e10cSrcweir             // Then insert the new frame
829cdf0e10cSrcweir             mxDocText.insertTextContent(mxDocCursor, xFrame, false);
830cdf0e10cSrcweir 
831cdf0e10cSrcweir             // Access the XText interface of the text contained within the frame
832cdf0e10cSrcweir             XText xFrameText = xFrame.getText();
833cdf0e10cSrcweir             // Create a TextCursor over the frame's contents
834cdf0e10cSrcweir             XTextCursor xFrameCursor = xFrameText.createTextCursor();
835cdf0e10cSrcweir             // Insert some text into the frame
836cdf0e10cSrcweir             xFrameText.insertString(
837cdf0e10cSrcweir                 xFrameCursor, "The first line in the newly created text frame.",
838cdf0e10cSrcweir                 false );
839cdf0e10cSrcweir             xFrameText.insertString(
840cdf0e10cSrcweir                 xFrameCursor, "\nThe second line in the new text frame.", false );
841cdf0e10cSrcweir             // Insert a paragraph break into the document (not the frame)
842cdf0e10cSrcweir             mxDocText.insertControlCharacter (
843cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
844cdf0e10cSrcweir         }
845cdf0e10cSrcweir         catch (Exception e)
846cdf0e10cSrcweir         {
847cdf0e10cSrcweir             e.printStackTrace();
848cdf0e10cSrcweir         }
849cdf0e10cSrcweir     }
850cdf0e10cSrcweir 
851cdf0e10cSrcweir     /** This example demonstrates the use of the AutoTextContainer, AutoTextGroup
852cdf0e10cSrcweir         and AutoTextEntry services and shows how to create, insert and modify
853cdf0e10cSrcweir         auto text blocks
854cdf0e10cSrcweir      */
AutoTextExample()855cdf0e10cSrcweir     protected void AutoTextExample ()
856cdf0e10cSrcweir     {
857cdf0e10cSrcweir         try
858cdf0e10cSrcweir         {
859cdf0e10cSrcweir             // Go to the end of the document
860cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
861cdf0e10cSrcweir             // Insert two paragraphs
862cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
863cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
864cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
865cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
866cdf0e10cSrcweir             // Position the cursor in the second paragraph
867cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
868cdf0e10cSrcweir                 UnoRuntime.queryInterface(XParagraphCursor.class, mxDocCursor );
869cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
870cdf0e10cSrcweir 
871cdf0e10cSrcweir             // Get an XNameAccess interface to all auto text groups from the
872cdf0e10cSrcweir             // document factory
873cdf0e10cSrcweir             XNameAccess xContainer = (XNameAccess) UnoRuntime.queryInterface(
874cdf0e10cSrcweir                 XNameAccess.class, mxFactory.createInstance (
875cdf0e10cSrcweir                     "com.sun.star.text.AutoTextContainer" ) );
876cdf0e10cSrcweir 
877cdf0e10cSrcweir             // Create a new table at the document factory
878cdf0e10cSrcweir             XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(
879cdf0e10cSrcweir                 XTextTable.class, mxDocFactory .createInstance(
880cdf0e10cSrcweir                     "com.sun.star.text.TextTable" ) );
881cdf0e10cSrcweir 
882cdf0e10cSrcweir             // Store the names of all auto text groups in an array of strings
883cdf0e10cSrcweir             String[] aGroupNames = xContainer.getElementNames();
884cdf0e10cSrcweir 
885cdf0e10cSrcweir             // Make sure we have at least one group name
886cdf0e10cSrcweir             if ( aGroupNames.length > 0 )
887cdf0e10cSrcweir             {
888cdf0e10cSrcweir                 // initialise the table to have a row for every autotext group
889cdf0e10cSrcweir                 // in a single column + one additional row for a header
890cdf0e10cSrcweir                 xTable.initialize( aGroupNames.length+1,1);
891cdf0e10cSrcweir 
892cdf0e10cSrcweir                 // Access the XPropertySet of the table
893cdf0e10cSrcweir                 XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(
894cdf0e10cSrcweir                     XPropertySet.class, xTable );
895cdf0e10cSrcweir 
896cdf0e10cSrcweir                 // We want a visible background
897cdf0e10cSrcweir                 xTableProps.setPropertyValue( "BackTransparent",
898cdf0e10cSrcweir                                               new Boolean(false));
899cdf0e10cSrcweir 
900cdf0e10cSrcweir                 // We want the background to be light blue
901cdf0e10cSrcweir                 xTableProps.setPropertyValue( "BackColor", new Integer(13421823));
902cdf0e10cSrcweir 
903cdf0e10cSrcweir                 // Inser the table into the document
904cdf0e10cSrcweir                 mxDocText.insertTextContent( mxDocCursor, xTable, false);
905cdf0e10cSrcweir 
906cdf0e10cSrcweir                 // Get an XIndexAccess to all table rows
907cdf0e10cSrcweir                 XIndexAccess xRows = xTable.getRows();
908cdf0e10cSrcweir 
909cdf0e10cSrcweir                 // Get the first row in the table
910cdf0e10cSrcweir                 XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface(
911cdf0e10cSrcweir                     XPropertySet.class, xRows.getByIndex ( 0 ) );
912cdf0e10cSrcweir 
913cdf0e10cSrcweir                 // We want the background of the first row to be visible too
914cdf0e10cSrcweir                 xRow.setPropertyValue( "BackTransparent", new Boolean(false));
915cdf0e10cSrcweir 
916cdf0e10cSrcweir                 // And let's make it dark blue
917cdf0e10cSrcweir                 xRow.setPropertyValue( "BackColor", new Integer(6710932));
918cdf0e10cSrcweir 
919cdf0e10cSrcweir                 // Put a description of the table contents into the first cell
920cdf0e10cSrcweir                 insertIntoCell( "A1", "AutoText Groups", xTable);
921cdf0e10cSrcweir 
922cdf0e10cSrcweir                 // Create a table cursor pointing at the second cell in the first
923cdf0e10cSrcweir                 // column
924cdf0e10cSrcweir                 XTextTableCursor xTableCursor = xTable.createCursorByCellName("A2");
925cdf0e10cSrcweir 
926cdf0e10cSrcweir                 // Loop over the group names
927cdf0e10cSrcweir                 for ( int i = 0 ; i < aGroupNames.length ; i ++ )
928cdf0e10cSrcweir                 {
929cdf0e10cSrcweir                     // Get the name of the current cell
930cdf0e10cSrcweir                     String sCellName = xTableCursor.getRangeName ();
931cdf0e10cSrcweir 
932cdf0e10cSrcweir                     // Get the XText interface of the current cell
933cdf0e10cSrcweir                     XText xCellText = (XText) UnoRuntime.queryInterface (
934cdf0e10cSrcweir                         XText.class, xTable.getCellByName ( sCellName ) );
935cdf0e10cSrcweir 
936cdf0e10cSrcweir                     // Set the cell contents of the current cell to be
937cdf0e10cSrcweir                     //the name of the of an autotext group
938cdf0e10cSrcweir                     xCellText.setString ( aGroupNames[i] );
939cdf0e10cSrcweir 
940cdf0e10cSrcweir                     // Access the autotext group with this name
941cdf0e10cSrcweir                     XAutoTextGroup xGroup = (XAutoTextGroup)
942cdf0e10cSrcweir                         UnoRuntime.queryInterface (XAutoTextGroup.class,
943cdf0e10cSrcweir                                        xContainer.getByName(aGroupNames[i]));
944cdf0e10cSrcweir 
945cdf0e10cSrcweir                     // Get the titles of each autotext block in this group
946cdf0e10cSrcweir                     String [] aBlockNames = xGroup.getTitles();
947cdf0e10cSrcweir 
948cdf0e10cSrcweir                     // Make sure that the autotext group contains at least one block
949cdf0e10cSrcweir                     if ( aBlockNames.length > 0 )
950cdf0e10cSrcweir                     {
951cdf0e10cSrcweir                         // Split the current cell vertically into two seperate cells
952cdf0e10cSrcweir                         xTableCursor.splitRange ( (short) 1, false );
953cdf0e10cSrcweir 
954cdf0e10cSrcweir                         // Put the cursor in the newly created right hand cell
955cdf0e10cSrcweir                         // and select it
956cdf0e10cSrcweir                         xTableCursor.goRight ( (short) 1, false );
957cdf0e10cSrcweir 
958cdf0e10cSrcweir                         // Split this cell horizontally to make a seperate cell
959cdf0e10cSrcweir                         // for each Autotext block
960cdf0e10cSrcweir                         if ( ( aBlockNames.length -1 ) > 0 )
961cdf0e10cSrcweir                             xTableCursor.splitRange (
962cdf0e10cSrcweir                                 (short) (aBlockNames.length - 1), true );
963cdf0e10cSrcweir 
964cdf0e10cSrcweir                         // loop over the block names
965cdf0e10cSrcweir                         for ( int j = 0 ; j < aBlockNames.length ; j ++ )
966cdf0e10cSrcweir                         {
967cdf0e10cSrcweir                             // Get the XText interface of the current cell
968cdf0e10cSrcweir                             xCellText = (XText) UnoRuntime.queryInterface (
969cdf0e10cSrcweir                                 XText.class, xTable.getCellByName (
970cdf0e10cSrcweir                                     xTableCursor.getRangeName() ) );
971cdf0e10cSrcweir 
972cdf0e10cSrcweir                             // Set the text contents of the current cell to the
973cdf0e10cSrcweir                             // title of an Autotext block
974cdf0e10cSrcweir                             xCellText.setString ( aBlockNames[j] );
975cdf0e10cSrcweir 
976cdf0e10cSrcweir                             // Move the cursor down one cell
977cdf0e10cSrcweir                             xTableCursor.goDown( (short)1, false);
978cdf0e10cSrcweir                         }
979cdf0e10cSrcweir                     }
980cdf0e10cSrcweir                     // Go back to the cell we originally split
981cdf0e10cSrcweir                     xTableCursor.gotoCellByName ( sCellName, false );
982cdf0e10cSrcweir 
983cdf0e10cSrcweir                                     // Go down one cell
984cdf0e10cSrcweir                     xTableCursor.goDown( (short)1, false);
985cdf0e10cSrcweir                 }
986cdf0e10cSrcweir 
987cdf0e10cSrcweir                 XAutoTextGroup xGroup;
988cdf0e10cSrcweir                 String [] aBlockNames;
989cdf0e10cSrcweir 
990cdf0e10cSrcweir                 // Add a depth so that we only generate 200 numbers before giving up
991cdf0e10cSrcweir                 // on finding a random autotext group that contains autotext blocks
992cdf0e10cSrcweir                 int nDepth = 0;
993cdf0e10cSrcweir                 do
994cdf0e10cSrcweir                 {
995cdf0e10cSrcweir                     // Generate a random, positive number which is lower than
996cdf0e10cSrcweir                     // the number of autotext groups
997cdf0e10cSrcweir                     int nRandom = Math.abs ( maRandom.nextInt() %
998cdf0e10cSrcweir                                              aGroupNames.length );
999cdf0e10cSrcweir 
1000cdf0e10cSrcweir                     // Get the autotext group at this name
1001cdf0e10cSrcweir                     xGroup 	= ( XAutoTextGroup ) UnoRuntime.queryInterface (
1002cdf0e10cSrcweir                         XAutoTextGroup.class, xContainer.getByName (
1003cdf0e10cSrcweir                             aGroupNames[ nRandom ] ) );
1004cdf0e10cSrcweir 
1005cdf0e10cSrcweir                     // Fill our string array with the names of all the blocks in
1006cdf0e10cSrcweir                     // this group
1007cdf0e10cSrcweir                     aBlockNames = xGroup.getElementNames();
1008cdf0e10cSrcweir 
1009cdf0e10cSrcweir                     // increment our depth counter
1010cdf0e10cSrcweir                     ++nDepth;
1011cdf0e10cSrcweir                 }
1012cdf0e10cSrcweir                 while ( nDepth < 200 && aBlockNames.length == 0 );
1013cdf0e10cSrcweir                 // If we managed to find a group containg blocks...
1014cdf0e10cSrcweir                 if ( aBlockNames.length > 0 )
1015cdf0e10cSrcweir                 {
1016cdf0e10cSrcweir                     // Pick a random block in this group and get it's
1017cdf0e10cSrcweir                     // XAutoTextEntry interface
1018cdf0e10cSrcweir                     int nRandom = Math.abs ( maRandom.nextInt()
1019cdf0e10cSrcweir                                              % aBlockNames.length );
1020cdf0e10cSrcweir                     XAutoTextEntry xEntry = ( XAutoTextEntry )
1021cdf0e10cSrcweir                         UnoRuntime.queryInterface (
1022cdf0e10cSrcweir                             XAutoTextEntry.class, xGroup.getByName (
1023cdf0e10cSrcweir                                 aBlockNames[ nRandom ] ) );
1024cdf0e10cSrcweir                     // insert the modified autotext block at the end of the document
1025cdf0e10cSrcweir                     xEntry.applyTo ( mxDocCursor );
1026cdf0e10cSrcweir 
1027cdf0e10cSrcweir                     // Get the titles of all text blocks in this AutoText group
1028cdf0e10cSrcweir                     String [] aBlockTitles = xGroup.getTitles();
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir                     // Get the XNamed interface of the autotext group
1031cdf0e10cSrcweir                     XNamed xGroupNamed = ( XNamed ) UnoRuntime.queryInterface (
1032cdf0e10cSrcweir                         XNamed.class, xGroup );
1033cdf0e10cSrcweir 
1034cdf0e10cSrcweir                     // Output the short cut and title of the random block
1035cdf0e10cSrcweir                     //and the name of the group it's from
1036cdf0e10cSrcweir                     System.out.println ( "Inserted the Autotext '" +
1037cdf0e10cSrcweir                                          aBlockTitles[nRandom]
1038cdf0e10cSrcweir                                          + "', shortcut '" + aBlockNames[nRandom]
1039cdf0e10cSrcweir                                          + "' from group '"
1040cdf0e10cSrcweir                                          + xGroupNamed.getName());
1041cdf0e10cSrcweir                 }
1042cdf0e10cSrcweir             }
1043cdf0e10cSrcweir 
1044cdf0e10cSrcweir             // Go to the end of the document
1045cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1046cdf0e10cSrcweir             // Insert new paragraph
1047cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1048cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1049cdf0e10cSrcweir 
1050cdf0e10cSrcweir             // Position cursor in new paragraph
1051cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1052cdf0e10cSrcweir 
1053cdf0e10cSrcweir             // Insert a string in the new paragraph
1054cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor,
1055cdf0e10cSrcweir                                      "Some text for a new autotext block", false );
1056cdf0e10cSrcweir 
1057cdf0e10cSrcweir             // Go to the end of the document
1058cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1059cdf0e10cSrcweir         }
1060cdf0e10cSrcweir         catch (Exception e)
1061cdf0e10cSrcweir         {
1062cdf0e10cSrcweir             e.printStackTrace();
1063cdf0e10cSrcweir         }
1064cdf0e10cSrcweir     }
1065cdf0e10cSrcweir 
1066cdf0e10cSrcweir     /** This method demonstrates how to insert indexes and index marks
1067cdf0e10cSrcweir      */
IndexExample()1068cdf0e10cSrcweir     protected void IndexExample ()
1069cdf0e10cSrcweir     {
1070cdf0e10cSrcweir         try
1071cdf0e10cSrcweir         {
1072cdf0e10cSrcweir             // Go to the end of the document
1073cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1074cdf0e10cSrcweir             // Insert a new paragraph and position the cursor in it
1075cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
1076cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
1077cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1078cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1079cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir             // Create a new ContentIndexMark and get it's XPropertySet interface
1082cdf0e10cSrcweir             XPropertySet xEntry = (XPropertySet)UnoRuntime.queryInterface(
1083cdf0e10cSrcweir                 XPropertySet.class,
1084cdf0e10cSrcweir                 mxDocFactory.createInstance("com.sun.star.text.ContentIndexMark"));
1085cdf0e10cSrcweir 
1086cdf0e10cSrcweir             // Set the text to be displayed in the index
1087cdf0e10cSrcweir             xEntry.setPropertyValue(
1088cdf0e10cSrcweir                 "AlternativeText", "Big dogs! Falling on my head!");
1089cdf0e10cSrcweir 
1090cdf0e10cSrcweir             // The Level property _must_ be set
1091cdf0e10cSrcweir             xEntry.setPropertyValue ( "Level", new Short ( (short) 1 ) );
1092cdf0e10cSrcweir 
1093cdf0e10cSrcweir             // Create a ContentIndex and access it's XPropertySet interface
1094cdf0e10cSrcweir             XPropertySet xIndex = (XPropertySet) UnoRuntime.queryInterface(
1095cdf0e10cSrcweir                 XPropertySet.class,
1096cdf0e10cSrcweir                 mxDocFactory.createInstance ( "com.sun.star.text.ContentIndex" ) );
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir             // Again, the Level property _must_ be set
1099cdf0e10cSrcweir             xIndex.setPropertyValue ( "Level", new Short ( (short) 10 ) );
1100cdf0e10cSrcweir 
1101cdf0e10cSrcweir             // Access the XTextContent interfaces of both the Index and the
1102cdf0e10cSrcweir             // IndexMark
1103cdf0e10cSrcweir             XTextContent xIndexContent = (XTextContent) UnoRuntime.queryInterface(
1104cdf0e10cSrcweir                 XTextContent.class, xIndex );
1105cdf0e10cSrcweir             XTextContent xEntryContent = (XTextContent) UnoRuntime.queryInterface(
1106cdf0e10cSrcweir                 XTextContent.class, xEntry );
1107cdf0e10cSrcweir 
1108cdf0e10cSrcweir             // Insert both in the document
1109cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xEntryContent, false );
1110cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xIndexContent, false );
1111cdf0e10cSrcweir 
1112cdf0e10cSrcweir             // Get the XDocumentIndex interface of the Index
1113cdf0e10cSrcweir             XDocumentIndex xDocIndex = (XDocumentIndex) UnoRuntime.queryInterface(
1114cdf0e10cSrcweir                 XDocumentIndex.class, xIndex );
1115cdf0e10cSrcweir 
1116cdf0e10cSrcweir             // And call it's update method
1117cdf0e10cSrcweir             xDocIndex.update();
1118cdf0e10cSrcweir         }
1119cdf0e10cSrcweir         catch (Exception e)
1120cdf0e10cSrcweir         {
1121cdf0e10cSrcweir             e.printStackTrace();
1122cdf0e10cSrcweir         }
1123cdf0e10cSrcweir     }
1124cdf0e10cSrcweir 
1125cdf0e10cSrcweir     /** This method demonstrates how to create and insert reference marks, and
1126cdf0e10cSrcweir      * GetReference Text Fields
1127cdf0e10cSrcweir      */
ReferenceExample()1128cdf0e10cSrcweir     protected void ReferenceExample ()
1129cdf0e10cSrcweir     {
1130cdf0e10cSrcweir         try
1131cdf0e10cSrcweir         {
1132cdf0e10cSrcweir             // Go to the end of the document
1133cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1134cdf0e10cSrcweir 
1135cdf0e10cSrcweir             // Insert a paragraph break
1136cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1137cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1138cdf0e10cSrcweir 
1139cdf0e10cSrcweir             // Get the Paragraph cursor
1140cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1141cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir             // Move the cursor into the new paragraph
1144cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1145cdf0e10cSrcweir 
1146cdf0e10cSrcweir             // Create a new ReferenceMark and get it's XNamed interface
1147cdf0e10cSrcweir             XNamed xRefMark = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1148cdf0e10cSrcweir                   mxDocFactory.createInstance ("com.sun.star.text.ReferenceMark"));
1149cdf0e10cSrcweir 
1150cdf0e10cSrcweir             // Set the name to TableHeader
1151cdf0e10cSrcweir             xRefMark.setName ( "TableHeader" );
1152cdf0e10cSrcweir 
1153cdf0e10cSrcweir             // Get the TextTablesSupplier interface of the document
1154cdf0e10cSrcweir             XTextTablesSupplier xTableSupplier = ( XTextTablesSupplier )
1155cdf0e10cSrcweir                 UnoRuntime.queryInterface(XTextTablesSupplier.class, mxDoc);
1156cdf0e10cSrcweir 
1157cdf0e10cSrcweir             // Get an XIndexAccess of TextTables
1158cdf0e10cSrcweir             XIndexAccess xTables = (XIndexAccess)UnoRuntime.queryInterface(
1159cdf0e10cSrcweir                 XIndexAccess.class, xTableSupplier.getTextTables());
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir             // We've only inserted one table, so get the first one from index zero
1162cdf0e10cSrcweir             XTextTable xTable = ( XTextTable ) UnoRuntime.queryInterface (
1163cdf0e10cSrcweir                 XTextTable.class, xTables.getByIndex( 0 ) );
1164cdf0e10cSrcweir 
1165cdf0e10cSrcweir             // Get the first cell from the table
1166cdf0e10cSrcweir             XText xTableText = (XText) UnoRuntime.queryInterface(
1167cdf0e10cSrcweir                 XText.class, xTable.getCellByName ( "A1" ) );
1168cdf0e10cSrcweir 
1169cdf0e10cSrcweir             // Get a text cursor for the first cell
1170cdf0e10cSrcweir             XTextCursor xTableCursor = xTableText.createTextCursor();
1171cdf0e10cSrcweir 
1172cdf0e10cSrcweir             // Get the XTextContent interface of the reference mark so we can
1173cdf0e10cSrcweir             // insert it
1174cdf0e10cSrcweir             XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface (
1175cdf0e10cSrcweir                 XTextContent.class, xRefMark );
1176cdf0e10cSrcweir 
1177cdf0e10cSrcweir             // Insert the reference mark into the first cell of the table
1178cdf0e10cSrcweir             xTableText.insertTextContent ( xTableCursor, xContent, false );
1179cdf0e10cSrcweir 
1180cdf0e10cSrcweir             // Create a 'GetReference' text field to refer to the reference mark
1181cdf0e10cSrcweir             // we just inserted, and get it's XPropertySet interface
1182cdf0e10cSrcweir             XPropertySet xFieldProps = (XPropertySet) UnoRuntime.queryInterface(
1183cdf0e10cSrcweir                 XPropertySet.class, mxDocFactory.createInstance (
1184cdf0e10cSrcweir                     "com.sun.star.text.TextField.GetReference" ) );
1185cdf0e10cSrcweir 
1186cdf0e10cSrcweir             // Get the XReferenceMarksSupplier interface of the document
1187cdf0e10cSrcweir             XReferenceMarksSupplier xRefSupplier = ( XReferenceMarksSupplier )
1188cdf0e10cSrcweir                 UnoRuntime.queryInterface( XReferenceMarksSupplier.class, mxDoc );
1189cdf0e10cSrcweir 
1190cdf0e10cSrcweir             // Get an XNameAccess which refers to all inserted reference marks
1191cdf0e10cSrcweir             XNameAccess xMarks = ( XNameAccess ) UnoRuntime.queryInterface (
1192cdf0e10cSrcweir                 XNameAccess.class, xRefSupplier.getReferenceMarks() );
1193cdf0e10cSrcweir 
1194cdf0e10cSrcweir             // Put the names of each reference mark into an array of strings
1195cdf0e10cSrcweir             String[] aNames = xMarks.getElementNames();
1196cdf0e10cSrcweir 
1197cdf0e10cSrcweir             // Make sure that at least 1 reference mark actually exists
1198cdf0e10cSrcweir             // (well, we just inserted one!)
1199cdf0e10cSrcweir             if ( aNames.length > 0 )
1200cdf0e10cSrcweir             {
1201cdf0e10cSrcweir                 // Output the name of the first reference mark ('TableHeader')
1202cdf0e10cSrcweir                 System.out.println (
1203cdf0e10cSrcweir                     "GetReference text field inserted for ReferenceMark : "
1204cdf0e10cSrcweir                     + aNames[0] );
1205cdf0e10cSrcweir 
1206cdf0e10cSrcweir                 // Set the SourceName of the GetReference text field to
1207cdf0e10cSrcweir                 // 'TableHeader'
1208cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "SourceName", aNames[0] );
1209cdf0e10cSrcweir 
1210cdf0e10cSrcweir                 // specify that the source is a reference mark (could also be a
1211cdf0e10cSrcweir                 // footnote, bookmark or sequence field )
1212cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "ReferenceFieldSource",
1213cdf0e10cSrcweir                                 new Short(ReferenceFieldSource.REFERENCE_MARK));
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir                 // We want the reference displayed as 'above' or 'below'
1216cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "ReferenceFieldPart",
1217cdf0e10cSrcweir                                 new Short(ReferenceFieldPart.UP_DOWN));
1218cdf0e10cSrcweir 
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir                 // Get the XTextContent interface of the GetReference text field
1221cdf0e10cSrcweir                 XTextContent xRefContent = (XTextContent) UnoRuntime.queryInterface(
1222cdf0e10cSrcweir                     XTextContent.class, xFieldProps );
1223cdf0e10cSrcweir 
1224cdf0e10cSrcweir                 // Go to the end of the document
1225cdf0e10cSrcweir                 mxDocCursor.gotoEnd( false );
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir                 // Make some text to precede the reference
1228cdf0e10cSrcweir                 mxDocText.insertString(mxDocText.getEnd(), "The table ", false);
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir                 // Insert the text field
1231cdf0e10cSrcweir                 mxDocText.insertTextContent(mxDocText.getEnd(), xRefContent, false);
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir                 // And some text after the reference..
1234cdf0e10cSrcweir                 mxDocText.insertString(mxDocText.getEnd(),
1235cdf0e10cSrcweir                               " contains the sum of some random numbers.", false );
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir                 // Refresh the document
1238cdf0e10cSrcweir                 XRefreshable xRefresh = (XRefreshable) UnoRuntime.queryInterface(
1239cdf0e10cSrcweir                     XRefreshable.class, mxDoc );
1240cdf0e10cSrcweir                 xRefresh.refresh();
1241cdf0e10cSrcweir             }
1242cdf0e10cSrcweir         }
1243cdf0e10cSrcweir         catch (Exception e)
1244cdf0e10cSrcweir         {
1245cdf0e10cSrcweir             e.printStackTrace();
1246cdf0e10cSrcweir         }
1247cdf0e10cSrcweir     }
1248cdf0e10cSrcweir 
1249cdf0e10cSrcweir     /** This method demonstrates how to create and insert footnotes, and how to
1250cdf0e10cSrcweir         access the XFootnotesSupplier interface of the document
1251cdf0e10cSrcweir      */
FootnoteExample()1252cdf0e10cSrcweir     protected void FootnoteExample ()
1253cdf0e10cSrcweir     {
1254cdf0e10cSrcweir         try
1255cdf0e10cSrcweir         {
1256cdf0e10cSrcweir             // Create a new footnote from the document factory and get it's
1257cdf0e10cSrcweir             // XFootnote interface
1258cdf0e10cSrcweir             XFootnote xFootnote = (XFootnote) UnoRuntime.queryInterface(
1259cdf0e10cSrcweir                 XFootnote.class, mxDocFactory.createInstance (
1260cdf0e10cSrcweir                     "com.sun.star.text.Footnote" ) );
1261cdf0e10cSrcweir 
1262cdf0e10cSrcweir             // Set the label to 'Numbers'
1263cdf0e10cSrcweir             xFootnote.setLabel ( "Numbers" );
1264cdf0e10cSrcweir 
1265cdf0e10cSrcweir             // Get the footnotes XTextContent interface so we can...
1266cdf0e10cSrcweir             XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface (
1267cdf0e10cSrcweir                 XTextContent.class, xFootnote );
1268cdf0e10cSrcweir 
1269cdf0e10cSrcweir             // ...insert it into the document
1270cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xContent, false );
1271cdf0e10cSrcweir 
1272cdf0e10cSrcweir             // Get the XFootnotesSupplier interface of the document
1273cdf0e10cSrcweir             XFootnotesSupplier xFootnoteSupplier = (XFootnotesSupplier)
1274cdf0e10cSrcweir                 UnoRuntime.queryInterface(XFootnotesSupplier.class, mxDoc );
1275cdf0e10cSrcweir 
1276cdf0e10cSrcweir             // Get an XIndexAccess interface to all footnotes
1277cdf0e10cSrcweir             XIndexAccess xFootnotes = ( XIndexAccess ) UnoRuntime.queryInterface (
1278cdf0e10cSrcweir                 XIndexAccess.class, xFootnoteSupplier.getFootnotes() );
1279cdf0e10cSrcweir 
1280cdf0e10cSrcweir             // Get the XFootnote interface to the first footnote inserted ('Numbers')
1281cdf0e10cSrcweir             XFootnote xNumbers = ( XFootnote ) UnoRuntime.queryInterface (
1282cdf0e10cSrcweir                 XFootnote.class, xFootnotes.getByIndex( 0 ) );
1283cdf0e10cSrcweir 
1284cdf0e10cSrcweir             // Get the XSimpleText interface to the Footnote
1285cdf0e10cSrcweir             XSimpleText xSimple = (XSimpleText ) UnoRuntime.queryInterface (
1286cdf0e10cSrcweir                 XSimpleText.class, xNumbers );
1287cdf0e10cSrcweir 
1288cdf0e10cSrcweir             // Create a text cursor for the foot note text
1289cdf0e10cSrcweir             XTextRange xRange = (XTextRange ) UnoRuntime.queryInterface (
1290cdf0e10cSrcweir                 XTextRange.class, xSimple.createTextCursor() );
1291cdf0e10cSrcweir 
1292cdf0e10cSrcweir             // And insert the actual text of the footnote.
1293cdf0e10cSrcweir             xSimple.insertString (
1294cdf0e10cSrcweir                 xRange, "  The numbers were generated by using java.util.Random", false );
1295cdf0e10cSrcweir         }
1296cdf0e10cSrcweir         catch (Exception e)
1297cdf0e10cSrcweir         {
1298cdf0e10cSrcweir             e.printStackTrace();
1299cdf0e10cSrcweir         }
1300cdf0e10cSrcweir     }
1301cdf0e10cSrcweir 
1302cdf0e10cSrcweir     /** This method demonstrates how to create and manipulate shapes, and how to
1303cdf0e10cSrcweir         access the draw page of the document to insert shapes
1304cdf0e10cSrcweir      */
DrawPageExample()1305cdf0e10cSrcweir     protected void DrawPageExample ()
1306cdf0e10cSrcweir     {
1307cdf0e10cSrcweir         try
1308cdf0e10cSrcweir         {
1309cdf0e10cSrcweir             // Go to the end of the document
1310cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1311cdf0e10cSrcweir             // Insert two new paragraphs
1312cdf0e10cSrcweir             mxDocText.insertControlCharacter(mxDocCursor,
1313cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false);
1314cdf0e10cSrcweir             mxDocText.insertControlCharacter(mxDocCursor,
1315cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false);
1316cdf0e10cSrcweir 
1317cdf0e10cSrcweir             // Get the XParagraphCursor interface of our document cursor
1318cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1319cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1320cdf0e10cSrcweir 
1321cdf0e10cSrcweir             // Position the cursor in the 2nd paragraph
1322cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1323cdf0e10cSrcweir 
1324cdf0e10cSrcweir             // Create a RectangleShape using the document factory
1325cdf0e10cSrcweir             XShape xRect = (XShape) UnoRuntime.queryInterface(
1326cdf0e10cSrcweir                 XShape.class, mxDocFactory.createInstance (
1327cdf0e10cSrcweir                     "com.sun.star.drawing.RectangleShape" ) );
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir             // Create an EllipseShape using the document factory
1330cdf0e10cSrcweir             XShape xEllipse = (XShape) UnoRuntime.queryInterface(
1331cdf0e10cSrcweir                 XShape.class, mxDocFactory.createInstance (
1332cdf0e10cSrcweir                     "com.sun.star.drawing.EllipseShape" ) );
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir             // Set the size of both the ellipse and the rectangle
1335cdf0e10cSrcweir             Size aSize = new Size();
1336cdf0e10cSrcweir             aSize.Height = 4000;
1337cdf0e10cSrcweir             aSize.Width = 10000;
1338cdf0e10cSrcweir             xRect.setSize(aSize);
1339cdf0e10cSrcweir             aSize.Height = 3000;
1340cdf0e10cSrcweir             aSize.Width = 6000;
1341cdf0e10cSrcweir             xEllipse.setSize ( aSize );
1342cdf0e10cSrcweir 
1343cdf0e10cSrcweir             // Set the position of the Rectangle to the right of the ellipse
1344cdf0e10cSrcweir             Point aPoint = new Point();
1345cdf0e10cSrcweir             aPoint.X = 6100;
1346cdf0e10cSrcweir             aPoint.Y = 0;
1347cdf0e10cSrcweir             xRect.setPosition ( aPoint );
1348cdf0e10cSrcweir 
1349cdf0e10cSrcweir             // Get the XPropertySet interfaces of both shapes
1350cdf0e10cSrcweir             XPropertySet xRectProps = (XPropertySet) UnoRuntime.queryInterface(
1351cdf0e10cSrcweir                 XPropertySet.class, xRect );
1352cdf0e10cSrcweir             XPropertySet xEllipseProps = (XPropertySet) UnoRuntime.queryInterface(
1353cdf0e10cSrcweir                 XPropertySet.class, xEllipse );
1354cdf0e10cSrcweir 
1355cdf0e10cSrcweir             // And set the AnchorTypes of both shapes to 'AT_PARAGRAPH'
1356cdf0e10cSrcweir             xRectProps.setPropertyValue ( "AnchorType",
1357cdf0e10cSrcweir                                           TextContentAnchorType.AT_PARAGRAPH );
1358cdf0e10cSrcweir             xEllipseProps.setPropertyValue ( "AnchorType",
1359cdf0e10cSrcweir                                              TextContentAnchorType.AT_PARAGRAPH );
1360cdf0e10cSrcweir 
1361cdf0e10cSrcweir             // Access the XDrawPageSupplier interface of the document
1362cdf0e10cSrcweir             XDrawPageSupplier xDrawPageSupplier = (XDrawPageSupplier)
1363cdf0e10cSrcweir                 UnoRuntime.queryInterface (XDrawPageSupplier.class, mxDoc );
1364cdf0e10cSrcweir 
1365cdf0e10cSrcweir             // Get the XShapes interface of the draw page
1366cdf0e10cSrcweir             XShapes xShapes = ( XShapes ) UnoRuntime.queryInterface (
1367cdf0e10cSrcweir                 XShapes.class, xDrawPageSupplier.getDrawPage () );
1368cdf0e10cSrcweir 
1369cdf0e10cSrcweir             // Add both shapes
1370cdf0e10cSrcweir             xShapes.add ( xEllipse );
1371cdf0e10cSrcweir             xShapes.add ( xRect );
1372cdf0e10cSrcweir 
1373cdf0e10cSrcweir             /*
1374cdf0e10cSrcweir               This doesn't work, I am assured that FME and AMA are fixing it.
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir               XShapes xGrouper = (XShapes) UnoRuntime.queryInterface(
1377cdf0e10cSrcweir               XShapes.class, mxDocFactory.createInstance (
1378cdf0e10cSrcweir               "com.sun.star.drawing.GroupShape" ) );
1379cdf0e10cSrcweir 
1380cdf0e10cSrcweir               XShape xGrouperShape = (XShape) UnoRuntime.queryInterface(
1381cdf0e10cSrcweir                      XShape.class, xGrouper );
1382cdf0e10cSrcweir               xShapes.add ( xGrouperShape );
1383cdf0e10cSrcweir 
1384cdf0e10cSrcweir               xGrouper.add ( xRect );
1385cdf0e10cSrcweir               xGrouper.add ( xEllipse );
1386cdf0e10cSrcweir 
1387cdf0e10cSrcweir               XShapeGrouper xShapeGrouper = (XShapeGrouper)
1388cdf0e10cSrcweir               UnoRuntime.queryInterface(XShapeGrouper.class, xShapes);
1389cdf0e10cSrcweir               xShapeGrouper.group ( xGrouper );
1390cdf0e10cSrcweir             */
1391cdf0e10cSrcweir 
1392cdf0e10cSrcweir         }
1393cdf0e10cSrcweir         catch (Exception e)
1394cdf0e10cSrcweir         {
1395cdf0e10cSrcweir             e.printStackTrace();
1396cdf0e10cSrcweir         }
1397cdf0e10cSrcweir     }
1398cdf0e10cSrcweir 
1399cdf0e10cSrcweir     /** This method demonstrates how to create, insert and apply styles
1400cdf0e10cSrcweir      */
StylesExample()1401cdf0e10cSrcweir     protected void StylesExample ()
1402cdf0e10cSrcweir     {
1403cdf0e10cSrcweir         try
1404cdf0e10cSrcweir         {
1405cdf0e10cSrcweir             // Go to the end of the document
1406cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1407cdf0e10cSrcweir 
1408cdf0e10cSrcweir             // Insert two paragraph breaks
1409cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1410cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1411cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1412cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1413cdf0e10cSrcweir 
1414cdf0e10cSrcweir             // Create a new style from the document's factory
1415cdf0e10cSrcweir             XStyle xStyle = (XStyle) UnoRuntime.queryInterface(
1416cdf0e10cSrcweir                 XStyle.class, mxDocFactory.createInstance(
1417cdf0e10cSrcweir                     "com.sun.star.style.ParagraphStyle" ) );
1418cdf0e10cSrcweir 
1419cdf0e10cSrcweir             // Access the XPropertySet interface of the new style
1420cdf0e10cSrcweir             XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
1421cdf0e10cSrcweir                 XPropertySet.class, xStyle );
1422cdf0e10cSrcweir 
1423cdf0e10cSrcweir             // Give the new style a light blue background
1424cdf0e10cSrcweir             xStyleProps.setPropertyValue ( "ParaBackColor", new Integer (13421823));
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir             // Get the StyleFamiliesSupplier interface of the document
1427cdf0e10cSrcweir             XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
1428cdf0e10cSrcweir                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, mxDoc);
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir             // Use the StyleFamiliesSupplier interface to get the XNameAccess
1431cdf0e10cSrcweir             // interface of the actual style families
1432cdf0e10cSrcweir             XNameAccess xFamilies = ( XNameAccess ) UnoRuntime.queryInterface (
1433cdf0e10cSrcweir                 XNameAccess.class, xSupplier.getStyleFamilies() );
1434cdf0e10cSrcweir 
1435cdf0e10cSrcweir             // Access the 'ParagraphStyles' Family
1436cdf0e10cSrcweir             XNameContainer xFamily = (XNameContainer ) UnoRuntime.queryInterface (
1437cdf0e10cSrcweir                         XNameContainer.class,
1438cdf0e10cSrcweir                         xFamilies.getByName ( "ParagraphStyles" ) );
1439cdf0e10cSrcweir 
1440cdf0e10cSrcweir             // Insert the newly created style into the ParagraphStyles family
1441cdf0e10cSrcweir             xFamily.insertByName ( "All-Singing All-Dancing Style", xStyle );
1442cdf0e10cSrcweir 
1443cdf0e10cSrcweir             // Get the XParagraphCursor interface of the document cursor
1444cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1445cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1446cdf0e10cSrcweir 
1447cdf0e10cSrcweir             // Select the first paragraph inserted
1448cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1449cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1450cdf0e10cSrcweir 
1451cdf0e10cSrcweir             // Access the property set of the cursor selection
1452cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
1453cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
1454cdf0e10cSrcweir 
1455cdf0e10cSrcweir             // Set the style of the cursor selection to our newly created style
1456cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "ParaStyleName",
1457cdf0e10cSrcweir                                             "All-Singing All-Dancing Style" );
1458cdf0e10cSrcweir 
1459cdf0e10cSrcweir             // Go back to the end
1460cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1461cdf0e10cSrcweir 
1462cdf0e10cSrcweir             // Select the last paragraph in the document
1463cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1464cdf0e10cSrcweir 
1465cdf0e10cSrcweir             // And reset it's style to 'Standard' (the programmatic name for
1466cdf0e10cSrcweir             // the default style)
1467cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "ParaStyleName", "Standard" );
1468cdf0e10cSrcweir         }
1469cdf0e10cSrcweir         catch (Exception e)
1470cdf0e10cSrcweir         {
1471cdf0e10cSrcweir             e.printStackTrace();
1472cdf0e10cSrcweir         }
1473cdf0e10cSrcweir     }
1474cdf0e10cSrcweir 
1475cdf0e10cSrcweir     /** This method demonstrates how to set numbering types and numbering levels
1476cdf0e10cSrcweir         using the com.sun.star.text.NumberingRules service
1477cdf0e10cSrcweir      */
NumberingExample()1478cdf0e10cSrcweir     protected void NumberingExample ()
1479cdf0e10cSrcweir     {
1480cdf0e10cSrcweir         try
1481cdf0e10cSrcweir         {
1482cdf0e10cSrcweir             // Go to the end of the document
1483cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1484cdf0e10cSrcweir             // Get the RelativeTextContentInsert interface of the document
1485cdf0e10cSrcweir             XRelativeTextContentInsert xRelative =
1486cdf0e10cSrcweir                 (XRelativeTextContentInsert ) UnoRuntime.queryInterface (
1487cdf0e10cSrcweir                     XRelativeTextContentInsert.class, mxDocText );
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir             // Use the document's factory to create the NumberingRules service,
1490cdf0e10cSrcweir             // and get it's XIndexAccess interface
1491cdf0e10cSrcweir             XIndexAccess xNum = (XIndexAccess) UnoRuntime.queryInterface(
1492cdf0e10cSrcweir                 XIndexAccess.class,
1493cdf0e10cSrcweir                 mxDocFactory.createInstance( "com.sun.star.text.NumberingRules" ) );
1494cdf0e10cSrcweir 
1495cdf0e10cSrcweir             // Also get the NumberingRule's XIndexReplace interface
1496cdf0e10cSrcweir             XIndexReplace xReplace = (XIndexReplace) UnoRuntime.queryInterface(
1497cdf0e10cSrcweir                 XIndexReplace.class, xNum );
1498cdf0e10cSrcweir 
1499cdf0e10cSrcweir             // Create an array of XPropertySets, one for each of the three
1500cdf0e10cSrcweir             // paragraphs we're about to create
1501cdf0e10cSrcweir             XPropertySet xParas[] = new XPropertySet [ 3 ];
1502cdf0e10cSrcweir             for ( int i = 0 ; i < 3 ; ++ i )
1503cdf0e10cSrcweir             {
1504cdf0e10cSrcweir                 // Create a new paragraph
1505cdf0e10cSrcweir                 XTextContent xNewPara = (XTextContent) UnoRuntime.queryInterface(
1506cdf0e10cSrcweir                     XTextContent.class, mxDocFactory.createInstance(
1507cdf0e10cSrcweir                         "com.sun.star.text.Paragraph" ) );
1508cdf0e10cSrcweir 
1509cdf0e10cSrcweir                 // Get the XPropertySet interface of the new paragraph and put
1510cdf0e10cSrcweir                 // it in our array
1511cdf0e10cSrcweir                 xParas[i] = (XPropertySet) UnoRuntime.queryInterface(
1512cdf0e10cSrcweir                     XPropertySet.class, xNewPara );
1513cdf0e10cSrcweir 
1514cdf0e10cSrcweir                 // Insert the new paragraph into the document after the fish
1515cdf0e10cSrcweir                 // section. As it is an insert relative to the fish section, the
1516cdf0e10cSrcweir                 // first paragraph inserted will be below the next two
1517cdf0e10cSrcweir                 xRelative.insertTextContentAfter ( xNewPara, mxFishSection );
1518cdf0e10cSrcweir 
1519cdf0e10cSrcweir                 // Separate from the above, but also needs to be done three times
1520cdf0e10cSrcweir 
1521cdf0e10cSrcweir                 // Get the PropertyValue sequence for this numbering level
1522cdf0e10cSrcweir                 PropertyValue [] aProps = (PropertyValue [] ) xNum.getByIndex ( i );
1523cdf0e10cSrcweir 
1524cdf0e10cSrcweir                 // Iterate over the PropertyValue's for this numbering level,
1525cdf0e10cSrcweir                 // looking for the 'NumberingType' property
1526cdf0e10cSrcweir                 for ( int j = 0 ; j < aProps.length ; ++j )
1527cdf0e10cSrcweir                 {
1528cdf0e10cSrcweir                     if ( aProps[j].Name.equals ( "NumberingType" ) )
1529cdf0e10cSrcweir                     {
1530cdf0e10cSrcweir                         // Once we find it, set it's value to a new type,
1531cdf0e10cSrcweir                         // dependent on which numbering level we're currently on
1532cdf0e10cSrcweir                         switch ( i )
1533cdf0e10cSrcweir                         {
1534cdf0e10cSrcweir                         case 0 : aProps[j].Value =
1535cdf0e10cSrcweir                                      new Short(NumberingType.ROMAN_UPPER);
1536cdf0e10cSrcweir                             break;
1537cdf0e10cSrcweir                         case 1 : aProps[j].Value =
1538cdf0e10cSrcweir                                      new Short(NumberingType.CHARS_UPPER_LETTER);
1539cdf0e10cSrcweir                             break;
1540cdf0e10cSrcweir                         case 2 : aProps[j].Value =
1541cdf0e10cSrcweir                                      new Short(NumberingType.ARABIC);
1542cdf0e10cSrcweir                             break;
1543cdf0e10cSrcweir                         }
1544cdf0e10cSrcweir                         // Put the updated PropertyValue sequence back into the
1545cdf0e10cSrcweir                         // NumberingRules service
1546cdf0e10cSrcweir                         xReplace.replaceByIndex ( i, aProps );
1547cdf0e10cSrcweir                         break;
1548cdf0e10cSrcweir                     }
1549cdf0e10cSrcweir                 }
1550cdf0e10cSrcweir             }
1551cdf0e10cSrcweir             // Get the XParagraphCursor interface of our text cursro
1552cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1553cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1554cdf0e10cSrcweir             // Go to the end of the document, then select the preceding paragraphs
1555cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1556cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1557cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1558cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1559cdf0e10cSrcweir 
1560cdf0e10cSrcweir             // Get the XPropertySet of the cursor's currently selected text
1561cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
1562cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
1563cdf0e10cSrcweir 
1564cdf0e10cSrcweir             // Set the updated Numbering rules to the cursor's property set
1565cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "NumberingRules", xNum );
1566cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1567cdf0e10cSrcweir 
1568cdf0e10cSrcweir             // Set the first paragraph that was inserted to a numbering level of
1569cdf0e10cSrcweir             // 2 (thus it will have Arabic style numbering)
1570cdf0e10cSrcweir             xParas[0].setPropertyValue ( "NumberingLevel", new Short((short) 2));
1571cdf0e10cSrcweir 
1572cdf0e10cSrcweir             // Set the second paragraph that was inserted to a numbering level of
1573cdf0e10cSrcweir             // 1 (thus it will have 'Chars Upper Letter' style numbering)
1574cdf0e10cSrcweir             xParas[1].setPropertyValue ( "NumberingLevel", new Short((short) 1));
1575cdf0e10cSrcweir 
1576cdf0e10cSrcweir             // Set the third paragraph that was inserted to a numbering level of
1577cdf0e10cSrcweir             // 0 (thus it will have 'Chars Upper Letter' style numbering)
1578cdf0e10cSrcweir             xParas[2].setPropertyValue ( "NumberingLevel", new Short((short) 0));
1579cdf0e10cSrcweir             }
1580cdf0e10cSrcweir         catch (Exception e)
1581cdf0e10cSrcweir         {
1582cdf0e10cSrcweir             e.printStackTrace();
1583cdf0e10cSrcweir         }
1584cdf0e10cSrcweir     }
1585cdf0e10cSrcweir 
1586cdf0e10cSrcweir     /** This method demonstrates how to create linked and unlinked sections
1587cdf0e10cSrcweir      */
TextSectionExample()1588cdf0e10cSrcweir     protected void TextSectionExample ()
1589cdf0e10cSrcweir     {
1590cdf0e10cSrcweir         try
1591cdf0e10cSrcweir         {
1592cdf0e10cSrcweir             // Go to the end of the document
1593cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1594cdf0e10cSrcweir             // Insert two paragraph breaks
1595cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1596cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1597cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1598cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, true );
1599cdf0e10cSrcweir 
1600cdf0e10cSrcweir             // Create a new TextSection from the document factory and access
1601cdf0e10cSrcweir             // it's XNamed interface
1602cdf0e10cSrcweir             XNamed xChildNamed = (XNamed) UnoRuntime.queryInterface(
1603cdf0e10cSrcweir                 XNamed.class, mxDocFactory.createInstance(
1604cdf0e10cSrcweir                     "com.sun.star.text.TextSection" ) );
1605cdf0e10cSrcweir             // Set the new sections name to 'Child_Section'
1606cdf0e10cSrcweir             xChildNamed.setName ( "Child_Section" );
1607cdf0e10cSrcweir 
1608cdf0e10cSrcweir             // Access the Child_Section's XTextContent interface and insert it
1609cdf0e10cSrcweir             // into the document
1610cdf0e10cSrcweir             XTextContent xChildSection = (XTextContent) UnoRuntime.queryInterface(
1611cdf0e10cSrcweir                 XTextContent.class, xChildNamed );
1612cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xChildSection, false );
1613cdf0e10cSrcweir 
1614cdf0e10cSrcweir             // Access the XParagraphCursor interface of our text cursor
1615cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1616cdf0e10cSrcweir                 UnoRuntime.queryInterface(XParagraphCursor.class, mxDocCursor);
1617cdf0e10cSrcweir 
1618cdf0e10cSrcweir             // Go back one paragraph (into Child_Section)
1619cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1620cdf0e10cSrcweir 
1621cdf0e10cSrcweir             // Insert a string into the Child_Section
1622cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor, "This is a test", false );
1623cdf0e10cSrcweir 
1624cdf0e10cSrcweir             // Go to the end of the document
1625cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1626cdf0e10cSrcweir 
1627cdf0e10cSrcweir             // Go back two paragraphs
1628cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1629cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1630cdf0e10cSrcweir             // Go to the end of the document, selecting the two paragraphs
1631cdf0e10cSrcweir             mxDocCursor.gotoEnd ( true );
1632cdf0e10cSrcweir 
1633cdf0e10cSrcweir             // Create another text section and access it's XNamed interface
1634cdf0e10cSrcweir             XNamed xParentNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1635cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1636cdf0e10cSrcweir 
1637cdf0e10cSrcweir             // Set this text section's name to Parent_Section
1638cdf0e10cSrcweir             xParentNamed.setName ( "Parent_Section" );
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir             // Access the Parent_Section's XTextContent interface ...
1641cdf0e10cSrcweir             XTextContent xParentSection = (XTextContent) UnoRuntime.queryInterface(
1642cdf0e10cSrcweir                 XTextContent.class, xParentNamed );
1643cdf0e10cSrcweir             // ...and insert it into the document
1644cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xParentSection, false );
1645cdf0e10cSrcweir 
1646cdf0e10cSrcweir             // Go to the end of the document
1647cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1648cdf0e10cSrcweir             // Insert a new paragraph
1649cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1650cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1651cdf0e10cSrcweir             // And select the new pargraph
1652cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1653cdf0e10cSrcweir 
1654cdf0e10cSrcweir             // Create a new Text Section and access it's XNamed interface
1655cdf0e10cSrcweir             XNamed xLinkNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1656cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1657cdf0e10cSrcweir             // Set the new text section's name to Linked_Section
1658cdf0e10cSrcweir             xLinkNamed.setName ( "Linked_Section" );
1659cdf0e10cSrcweir 
1660cdf0e10cSrcweir             // Access the Linked_Section's XTextContent interface
1661cdf0e10cSrcweir             XTextContent xLinkedSection = (XTextContent) UnoRuntime.queryInterface(
1662cdf0e10cSrcweir                 XTextContent.class, xLinkNamed );
1663cdf0e10cSrcweir             // And insert the Linked_Section into the document
1664cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xLinkedSection, false );
1665cdf0e10cSrcweir 
1666cdf0e10cSrcweir             // Access the Linked_Section's XPropertySet interface
1667cdf0e10cSrcweir             XPropertySet xLinkProps = (XPropertySet)UnoRuntime.queryInterface(
1668cdf0e10cSrcweir                 XPropertySet.class, xLinkNamed );
1669cdf0e10cSrcweir             // Set the linked section to be linked to the Child_Section
1670cdf0e10cSrcweir             xLinkProps.setPropertyValue ( "LinkRegion", "Child_Section" );
1671cdf0e10cSrcweir 
1672cdf0e10cSrcweir             // Access the XPropertySet interface of the Child_Section
1673cdf0e10cSrcweir             XPropertySet xChildProps = (XPropertySet) UnoRuntime.queryInterface(
1674cdf0e10cSrcweir                 XPropertySet.class, xChildNamed );
1675cdf0e10cSrcweir             // Set the Child_Section's background colour to blue
1676cdf0e10cSrcweir             xChildProps.setPropertyValue( "BackColor", new Integer(13421823));
1677cdf0e10cSrcweir 
1678cdf0e10cSrcweir             // Refresh the document, so the linked section matches the Child_Section
1679cdf0e10cSrcweir             XRefreshable xRefresh = (XRefreshable) UnoRuntime.queryInterface(
1680cdf0e10cSrcweir                 XRefreshable.class, mxDoc );
1681cdf0e10cSrcweir             xRefresh.refresh();
1682cdf0e10cSrcweir         }
1683cdf0e10cSrcweir         catch (Exception e)
1684cdf0e10cSrcweir         {
1685cdf0e10cSrcweir             e.printStackTrace();
1686cdf0e10cSrcweir         }
1687cdf0e10cSrcweir     }
1688cdf0e10cSrcweir 
1689cdf0e10cSrcweir     /** This method demonstrates the XTextColumns interface and how to insert a
1690cdf0e10cSrcweir         blank paragraph using the XRelativeTextContentInsert interface
1691cdf0e10cSrcweir      */
TextColumnsExample()1692cdf0e10cSrcweir     protected void TextColumnsExample ()
1693cdf0e10cSrcweir     {
1694cdf0e10cSrcweir         try
1695cdf0e10cSrcweir         {
1696cdf0e10cSrcweir             // Go to the end of the doucment
1697cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1698cdf0e10cSrcweir             // insert a new paragraph
1699cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1700cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1701cdf0e10cSrcweir 
1702cdf0e10cSrcweir             // insert the string 'I am a fish.' 100 times
1703cdf0e10cSrcweir             for ( int i = 0 ; i < 100 ; ++i )
1704cdf0e10cSrcweir             {
1705cdf0e10cSrcweir                 mxDocText.insertString ( mxDocCursor, "I am a fish.", false );
1706cdf0e10cSrcweir             }
1707cdf0e10cSrcweir             // insert a paragraph break after the text
1708cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1709cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1710cdf0e10cSrcweir 
1711cdf0e10cSrcweir             // Get the XParagraphCursor interface of our text cursor
1712cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1713cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1714cdf0e10cSrcweir             // Jump back before all the text we just inserted
1715cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1716cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1717cdf0e10cSrcweir 
1718cdf0e10cSrcweir             // Insert a string at the beginning of the block of text
1719cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor, "Fish section begins:", false );
1720cdf0e10cSrcweir 
1721cdf0e10cSrcweir             // Then select all of the text
1722cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1723cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1724cdf0e10cSrcweir 
1725cdf0e10cSrcweir             // Create a new text section and get it's XNamed interface
1726cdf0e10cSrcweir             XNamed xSectionNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1727cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1728cdf0e10cSrcweir 
1729cdf0e10cSrcweir             // Set the name of our new section (appropiately) to 'Fish'
1730cdf0e10cSrcweir             xSectionNamed.setName ( "Fish" );
1731cdf0e10cSrcweir 
1732cdf0e10cSrcweir             // Create the TextColumns service and get it's XTextColumns interface
1733cdf0e10cSrcweir             XTextColumns xColumns = (XTextColumns) UnoRuntime.queryInterface(
1734cdf0e10cSrcweir                 XTextColumns.class,
1735cdf0e10cSrcweir                 mxDocFactory.createInstance ( "com.sun.star.text.TextColumns" ) );
1736cdf0e10cSrcweir 
1737cdf0e10cSrcweir             // We want three columns
1738cdf0e10cSrcweir             xColumns.setColumnCount ( (short) 3 );
1739cdf0e10cSrcweir 
1740cdf0e10cSrcweir             // Get the TextColumns, and make the middle one narrow with a larger
1741cdf0e10cSrcweir             // margin on the left than the right
1742cdf0e10cSrcweir             TextColumn[]  aSequence = xColumns.getColumns ();
1743cdf0e10cSrcweir             aSequence[1].Width /= 2;
1744cdf0e10cSrcweir             aSequence[1].LeftMargin = 350;
1745cdf0e10cSrcweir             aSequence[1].RightMargin = 200;
1746cdf0e10cSrcweir             // Set the updated TextColumns back to the XTextColumns
1747cdf0e10cSrcweir             xColumns.setColumns ( aSequence );
1748cdf0e10cSrcweir 
1749cdf0e10cSrcweir             // Get the property set interface of our 'Fish' section
1750cdf0e10cSrcweir             XPropertySet xSectionProps = (XPropertySet) UnoRuntime.queryInterface(
1751cdf0e10cSrcweir                 XPropertySet.class, xSectionNamed );
1752cdf0e10cSrcweir 
1753cdf0e10cSrcweir             // Set the columns to the Text Section
1754cdf0e10cSrcweir             xSectionProps.setPropertyValue ( "TextColumns", xColumns );
1755cdf0e10cSrcweir 
1756cdf0e10cSrcweir             // Get the XTextContent interface of our 'Fish' section
1757cdf0e10cSrcweir             mxFishSection = (XTextContent) UnoRuntime.queryInterface(
1758cdf0e10cSrcweir                 XTextContent.class, xSectionNamed );
1759cdf0e10cSrcweir 
1760cdf0e10cSrcweir             // Insert the 'Fish' section over the currently selected text
1761cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, mxFishSection, true );
1762cdf0e10cSrcweir 
1763cdf0e10cSrcweir             // Get the wonderful XRelativeTextContentInsert interface
1764cdf0e10cSrcweir             XRelativeTextContentInsert xRelative = (XRelativeTextContentInsert )
1765cdf0e10cSrcweir                 UnoRuntime.queryInterface (
1766cdf0e10cSrcweir                     XRelativeTextContentInsert.class, mxDocText );
1767cdf0e10cSrcweir 
1768cdf0e10cSrcweir             // Create a new empty paragraph and get it's XTextContent interface
1769cdf0e10cSrcweir             XTextContent xNewPara = (XTextContent) UnoRuntime.queryInterface(
1770cdf0e10cSrcweir                 XTextContent.class,
1771cdf0e10cSrcweir                 mxDocFactory.createInstance("com.sun.star.text.Paragraph"));
1772cdf0e10cSrcweir 
1773cdf0e10cSrcweir             // Insert the empty paragraph after the fish Text Section
1774cdf0e10cSrcweir             xRelative.insertTextContentAfter ( xNewPara, mxFishSection );
1775cdf0e10cSrcweir         }
1776cdf0e10cSrcweir         catch (Exception e)
1777cdf0e10cSrcweir         {
1778cdf0e10cSrcweir             e.printStackTrace();
1779cdf0e10cSrcweir         }
1780cdf0e10cSrcweir     }
1781cdf0e10cSrcweir }
1782