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