134dd1e25SAndrew Rist /************************************************************** 2*ffea6052Smseidel * 334dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 434dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 534dd1e25SAndrew Rist * distributed with this work for additional information 634dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 734dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 834dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 934dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10*ffea6052Smseidel * 1134dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ffea6052Smseidel * 1334dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 1434dd1e25SAndrew Rist * software distributed under the License is distributed on an 1534dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1634dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 1734dd1e25SAndrew Rist * specific language governing permissions and limitations 1834dd1e25SAndrew Rist * under the License. 19*ffea6052Smseidel * 2034dd1e25SAndrew Rist *************************************************************/ 2134dd1e25SAndrew Rist 2234dd1e25SAndrew 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 * 129*ffea6052Smseidel * @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"; 136*ffea6052Smseidel 137cdf0e10cSrcweir private XComponentContext mxRemoteContext = null; 138*ffea6052Smseidel private XMultiComponentFactory mxRemoteServiceManager = null; 139cdf0e10cSrcweir private XTextDocument mxDoc = null; 140cdf0e10cSrcweir private XMultiServiceFactory mxDocFactory = null; 141cdf0e10cSrcweir private XMultiServiceFactory mxFactory = null; 142*ffea6052Smseidel private XPropertySet mxDocProps = null; 143cdf0e10cSrcweir private XText mxDocText = null; 144cdf0e10cSrcweir private XTextCursor mxDocCursor = null; 145cdf0e10cSrcweir private XTextContent mxFishSection = null; 146cdf0e10cSrcweir private Random maRandom = null; 147*ffea6052Smseidel 148cdf0e10cSrcweir /** Creates a new instance of TextDocuments */ TextDocuments()149cdf0e10cSrcweir public TextDocuments() { 150cdf0e10cSrcweir } 151*ffea6052Smseidel 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]; 160*ffea6052Smseidel 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 } 171*ffea6052Smseidel runDemo()172cdf0e10cSrcweir protected void runDemo() throws java.lang.Exception { 173cdf0e10cSrcweir storePrintExample(); // depends on printer name 174*ffea6052Smseidel templateExample(); 175cdf0e10cSrcweir viewCursorExample(); // makes changes to the current document, 176cdf0e10cSrcweir // use with care 177cdf0e10cSrcweir editingExample(); 178cdf0e10cSrcweir } 179*ffea6052Smseidel 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"); 193*ffea6052Smseidel 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 199*ffea6052Smseidel XComponent xTemplateComponent = 200cdf0e10cSrcweir newDocComponentFromTemplate( sTemplateFileUrl.toString() ); 201*ffea6052Smseidel 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); 208*ffea6052Smseidel 209cdf0e10cSrcweir // access the TextFields and the TextFieldMasters collections 210cdf0e10cSrcweir XNameAccess xNamedFieldMasters = xTextFieldsSupplier.getTextFieldMasters(); 211cdf0e10cSrcweir XEnumerationAccess xEnumeratedFields = xTextFieldsSupplier.getTextFields(); 212*ffea6052Smseidel 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(); 218*ffea6052Smseidel 219cdf0e10cSrcweir // access corresponding field master 220cdf0e10cSrcweir Object fieldMaster = xNamedFieldMasters.getByName( 221cdf0e10cSrcweir "com.sun.star.text.fieldmaster.User." + key); 222*ffea6052Smseidel 223cdf0e10cSrcweir // query the XPropertySet interface, we need to set the Content property 224cdf0e10cSrcweir XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface( 225cdf0e10cSrcweir XPropertySet.class, fieldMaster); 226*ffea6052Smseidel 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(); 237*ffea6052Smseidel 238cdf0e10cSrcweir // find the bookmark named "Subscription" 239cdf0e10cSrcweir Object bookmark = xNamedBookmarks.getByName("Subscription"); 240*ffea6052Smseidel // 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"); 248*ffea6052Smseidel 249cdf0e10cSrcweir } 250*ffea6052Smseidel 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. 255*ffea6052Smseidel * 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 273*ffea6052Smseidel XTextViewCursorSupplier xViewCursorSupplier = 274cdf0e10cSrcweir (XTextViewCursorSupplier)UnoRuntime.queryInterface( 275cdf0e10cSrcweir XTextViewCursorSupplier.class, xController); 276cdf0e10cSrcweir XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor(); 277*ffea6052Smseidel 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()); 289*ffea6052Smseidel // 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 } 307*ffea6052Smseidel 308*ffea6052Smseidel 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); 318*ffea6052Smseidel 319cdf0e10cSrcweir // get a reference to the body text of the document 320cdf0e10cSrcweir mxDocText = mxDoc.getText(); 321*ffea6052Smseidel 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 ); 326*ffea6052Smseidel 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 331*ffea6052Smseidel TextRangeExample (); 332cdf0e10cSrcweir // Create a document cursor and remember it, it will be used in most 333cdf0e10cSrcweir // of the following examples 334*ffea6052Smseidel mxDocCursor = mxDocText.createTextCursor(); 335cdf0e10cSrcweir // Demonstrate some of the different cursor types (word, sentence) 336cdf0e10cSrcweir TextCursorExample (); 337*ffea6052Smseidel 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 ); 342*ffea6052Smseidel 343a893be29SPedro Giffuni // Examples of text fields, dependent text fields and field masters 344cdf0e10cSrcweir TextFieldExample (); 345*ffea6052Smseidel 346cdf0e10cSrcweir // Example of using an XEnumerationAccess to iterate over paragraphs and 347cdf0e10cSrcweir // set properties of each paragraph as we do so 348*ffea6052Smseidel ParagraphExample (); 349*ffea6052Smseidel 350cdf0e10cSrcweir // Example of creating and manipulating a text frame 351cdf0e10cSrcweir TextFrameExample (); 352*ffea6052Smseidel 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 (); 357*ffea6052Smseidel 358cdf0e10cSrcweir // Example of creating, inserting and manipulating text sections, as 359cdf0e10cSrcweir // well as an example of how to refresh the document 360*ffea6052Smseidel 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 (); 375*ffea6052Smseidel 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 (); 388*ffea6052Smseidel 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 394*ffea6052Smseidel AutoTextExample (); 395cdf0e10cSrcweir } 396*ffea6052Smseidel 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('\\', '/')); 410*ffea6052Smseidel 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 } 424*ffea6052Smseidel getRemoteServiceManager()425cdf0e10cSrcweir private XMultiComponentFactory getRemoteServiceManager() 426cdf0e10cSrcweir throws java.lang.Exception 427*ffea6052Smseidel { 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; 436*ffea6052Smseidel } 437*ffea6052Smseidel 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", 449*ffea6052Smseidel 0, loadProps); 450cdf0e10cSrcweir } 451*ffea6052Smseidel 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); 464*ffea6052Smseidel 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"; 471*ffea6052Smseidel loadProps[0].Value = new Boolean(true); 472cdf0e10cSrcweir // load 473cdf0e10cSrcweir return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 474*ffea6052Smseidel 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 { 482*ffea6052Smseidel 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"; 488*ffea6052Smseidel storeProps[0].Value = "MS Word 97"; 489cdf0e10cSrcweir 490cdf0e10cSrcweir System.out.println("... store \"PrintDemo.odt\" to \"" + storeUrl + "\"."); 491*ffea6052Smseidel xStorable.storeAsURL(storeUrl, storeProps); 492cdf0e10cSrcweir } 493*ffea6052Smseidel 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 502*ffea6052Smseidel xPrintable.setPrinter(printerDesc); 503*ffea6052Smseidel 504cdf0e10cSrcweir PropertyValue[] printOpts = new PropertyValue[1]; 505cdf0e10cSrcweir printOpts[0] = new PropertyValue(); 506cdf0e10cSrcweir printOpts[0].Name = "Pages"; 507*ffea6052Smseidel printOpts[0].Value = "1"; 508*ffea6052Smseidel 509*ffea6052Smseidel xPrintable.print(printOpts); 510cdf0e10cSrcweir } 511*ffea6052Smseidel 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 519*ffea6052Smseidel 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 } 527*ffea6052Smseidel 528cdf0e10cSrcweir // Adding a string at the end or the beginning of text TextRangeExample()529cdf0e10cSrcweir protected void TextRangeExample () 530cdf0e10cSrcweir { 531cdf0e10cSrcweir try 532cdf0e10cSrcweir { 533a893be29SPedro Giffuni // Get a text range referring 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" ); 537a893be29SPedro Giffuni // Get a text range referring 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 } 547*ffea6052Smseidel 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 ); 583*ffea6052Smseidel mxDocText.insertString (xWordCursor, 584cdf0e10cSrcweir ", which has been changed with text cursors!", 585cdf0e10cSrcweir true); 586cdf0e10cSrcweir } 587cdf0e10cSrcweir catch ( Exception e ) 588cdf0e10cSrcweir { 589cdf0e10cSrcweir e.printStackTrace(); 590*ffea6052Smseidel } 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 { 600*ffea6052Smseidel // Use the text document's factory to create a DateTime text field, 601*ffea6052Smseidel // and access its 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 609*ffea6052Smseidel // Use the text document's factory to create a user text field, 610*ffea6052Smseidel // and access its XDependentTextField interface 611*ffea6052Smseidel 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 617*ffea6052Smseidel // access its 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 ) ); 625*ffea6052Smseidel 626cdf0e10cSrcweir // Attach the field master to the user field 627cdf0e10cSrcweir xUserField.attachTextFieldMaster ( xMasterPropSet ); 628*ffea6052Smseidel 629cdf0e10cSrcweir // Move the cursor to the end of the document 630cdf0e10cSrcweir mxDocCursor.gotoEnd( false ); 631cdf0e10cSrcweir // insert a paragraph break using the XSimpleText interface 632*ffea6052Smseidel mxDocText.insertControlCharacter ( 633cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 634*ffea6052Smseidel 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 } 642*ffea6052Smseidel } 643*ffea6052Smseidel 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 652*ffea6052Smseidel // 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 } 689*ffea6052Smseidel 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 } 696*ffea6052Smseidel 697*ffea6052Smseidel /** This method sets the text color of the cell referred 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, 701*ffea6052Smseidel XTextTable xTable) 702cdf0e10cSrcweir { 703*ffea6052Smseidel // Access the XText interface of the cell referred to by sCellName 704cdf0e10cSrcweir XText xCellText = (XText) UnoRuntime.queryInterface( 705cdf0e10cSrcweir XText.class, xTable.getCellByName ( sCellName ) ); 706*ffea6052Smseidel 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 ); 712*ffea6052Smseidel 713*ffea6052Smseidel try 714cdf0e10cSrcweir { 715*ffea6052Smseidel // Set the color of the text to white 716cdf0e10cSrcweir xCellCursorProps.setPropertyValue( "CharColor", new Integer(16777215)); 717*ffea6052Smseidel } 718*ffea6052Smseidel catch ( Exception e) 719cdf0e10cSrcweir { 720cdf0e10cSrcweir e.printStackTrace(); 721cdf0e10cSrcweir } 722cdf0e10cSrcweir // Set the text in the cell to sText 723cdf0e10cSrcweir xCellText.setString( sText ); 724cdf0e10cSrcweir } 725*ffea6052Smseidel 726cdf0e10cSrcweir /** This method shows how to create and insert a text table, as well as insert 727*ffea6052Smseidel text and formula into the cells of the table 728cdf0e10cSrcweir */ TextTableExample()729cdf0e10cSrcweir protected void TextTableExample () 730cdf0e10cSrcweir { 731*ffea6052Smseidel try 732cdf0e10cSrcweir { 733cdf0e10cSrcweir // Create a new table from the document's factory 734*ffea6052Smseidel XTextTable xTable = (XTextTable) UnoRuntime.queryInterface( 735cdf0e10cSrcweir XTextTable.class, mxDocFactory .createInstance( 736cdf0e10cSrcweir "com.sun.star.text.TextTable" ) ); 737*ffea6052Smseidel 738cdf0e10cSrcweir // Specify that we want the table to have 4 rows and 4 columns 739cdf0e10cSrcweir xTable.initialize( 4, 4 ); 740*ffea6052Smseidel 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(); 745*ffea6052Smseidel 746cdf0e10cSrcweir // Access the property set of the first row (properties listed in 747cdf0e10cSrcweir // service description: com.sun.star.text.TextTableRow) 748*ffea6052Smseidel 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)); 754*ffea6052Smseidel 755cdf0e10cSrcweir // Access the property set of the whole table 756*ffea6052Smseidel XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface( 757cdf0e10cSrcweir XPropertySet.class, xTable ); 758cdf0e10cSrcweir // We want visible background colors 759cdf0e10cSrcweir xTableProps.setPropertyValue( "BackTransparent", new Boolean(false)); 760*ffea6052Smseidel // Set the background color to light blue 761cdf0e10cSrcweir xTableProps.setPropertyValue( "BackColor", new Integer(13421823)); 762*ffea6052Smseidel 763*ffea6052Smseidel // set the text (and text color) 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() ); 775*ffea6052Smseidel 776cdf0e10cSrcweir xTable.getCellByName( "A3" ).setValue( getRandomDouble() ); 777cdf0e10cSrcweir xTable.getCellByName( "B3" ).setValue( getRandomDouble() ); 778cdf0e10cSrcweir xTable.getCellByName( "C3" ).setValue( getRandomDouble() ); 779*ffea6052Smseidel 780cdf0e10cSrcweir xTable.getCellByName( "A4" ).setValue( getRandomDouble() ); 781cdf0e10cSrcweir xTable.getCellByName( "B4" ).setValue( getRandomDouble() ); 782cdf0e10cSrcweir xTable.getCellByName( "C4" ).setValue( getRandomDouble() ); 783*ffea6052Smseidel 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>" ); 789*ffea6052Smseidel } 790*ffea6052Smseidel 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 { 799*ffea6052Smseidel try 800cdf0e10cSrcweir { 801cdf0e10cSrcweir // Use the document's factory to create a new text frame and 802*ffea6052Smseidel // immediately access its XTextFrame interface 803cdf0e10cSrcweir XTextFrame xFrame = (XTextFrame) UnoRuntime.queryInterface ( 804cdf0e10cSrcweir XTextFrame.class, mxDocFactory.createInstance ( 805cdf0e10cSrcweir "com.sun.star.text.TextFrame" ) ); 806*ffea6052Smseidel 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 ); 812*ffea6052Smseidel 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 826*ffea6052Smseidel mxDocText.insertControlCharacter ( 827cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 828cdf0e10cSrcweir // Then insert the new frame 829cdf0e10cSrcweir mxDocText.insertTextContent(mxDocCursor, xFrame, false); 830*ffea6052Smseidel 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 836*ffea6052Smseidel xFrameText.insertString( 837cdf0e10cSrcweir xFrameCursor, "The first line in the newly created text frame.", 838cdf0e10cSrcweir false ); 839*ffea6052Smseidel xFrameText.insertString( 840cdf0e10cSrcweir xFrameCursor, "\nThe second line in the new text frame.", false ); 841cdf0e10cSrcweir // Insert a paragraph break into the document (not the frame) 842*ffea6052Smseidel mxDocText.insertControlCharacter ( 843cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 844*ffea6052Smseidel } 845*ffea6052Smseidel catch (Exception e) 846cdf0e10cSrcweir { 847cdf0e10cSrcweir e.printStackTrace(); 848cdf0e10cSrcweir } 849cdf0e10cSrcweir } 850*ffea6052Smseidel 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 862*ffea6052Smseidel mxDocText.insertControlCharacter ( mxDocCursor, 863cdf0e10cSrcweir ControlCharacter.PARAGRAPH_BREAK, false ); 864*ffea6052Smseidel 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 873*ffea6052Smseidel XNameAccess xContainer = (XNameAccess) UnoRuntime.queryInterface( 874cdf0e10cSrcweir XNameAccess.class, mxFactory.createInstance ( 875cdf0e10cSrcweir "com.sun.star.text.AutoTextContainer" ) ); 876cdf0e10cSrcweir 877*ffea6052Smseidel // Create a new table at the document factory 878*ffea6052Smseidel XTextTable xTable = (XTextTable) UnoRuntime.queryInterface( 879*ffea6052Smseidel XTextTable.class, mxDocFactory .createInstance( 880cdf0e10cSrcweir "com.sun.star.text.TextTable" ) ); 881*ffea6052Smseidel 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 886*ffea6052Smseidel if ( aGroupNames.length > 0 ) 887cdf0e10cSrcweir { 888*ffea6052Smseidel // initialize 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 ); 895*ffea6052Smseidel 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 903*ffea6052Smseidel // Insert 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(); 908*ffea6052Smseidel 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)); 915*ffea6052Smseidel 916cdf0e10cSrcweir // And let's make it dark blue 917cdf0e10cSrcweir xRow.setPropertyValue( "BackColor", new Integer(6710932)); 918*ffea6052Smseidel 919cdf0e10cSrcweir // Put a description of the table contents into the first cell 920cdf0e10cSrcweir insertIntoCell( "A1", "AutoText Groups", xTable); 921*ffea6052Smseidel 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 (); 931*ffea6052Smseidel 932cdf0e10cSrcweir // Get the XText interface of the current cell 933*ffea6052Smseidel XText xCellText = (XText) UnoRuntime.queryInterface ( 934cdf0e10cSrcweir XText.class, xTable.getCellByName ( sCellName ) ); 935*ffea6052Smseidel 936*ffea6052Smseidel // Set the cell contents of the current cell to be 937cdf0e10cSrcweir //the name of the of an autotext group 938cdf0e10cSrcweir xCellText.setString ( aGroupNames[i] ); 939*ffea6052Smseidel 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(); 947*ffea6052Smseidel 948cdf0e10cSrcweir // Make sure that the autotext group contains at least one block 949cdf0e10cSrcweir if ( aBlockNames.length > 0 ) 950cdf0e10cSrcweir { 951a893be29SPedro Giffuni // Split the current cell vertically into two separate cells 952cdf0e10cSrcweir xTableCursor.splitRange ( (short) 1, false ); 953*ffea6052Smseidel 954*ffea6052Smseidel // Put the cursor in the newly created right hand cell 955cdf0e10cSrcweir // and select it 956cdf0e10cSrcweir xTableCursor.goRight ( (short) 1, false ); 957*ffea6052Smseidel 958*ffea6052Smseidel // Split this cell horizontally to make a separate cell 959cdf0e10cSrcweir // for each Autotext block 960cdf0e10cSrcweir if ( ( aBlockNames.length -1 ) > 0 ) 961*ffea6052Smseidel xTableCursor.splitRange ( 962cdf0e10cSrcweir (short) (aBlockNames.length - 1), true ); 963*ffea6052Smseidel 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() ) ); 971*ffea6052Smseidel 972cdf0e10cSrcweir // Set the text contents of the current cell to the 973cdf0e10cSrcweir // title of an Autotext block 974cdf0e10cSrcweir xCellText.setString ( aBlockNames[j] ); 975*ffea6052Smseidel 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 ); 982*ffea6052Smseidel 983cdf0e10cSrcweir // Go down one cell 984cdf0e10cSrcweir xTableCursor.goDown( (short)1, false); 985cdf0e10cSrcweir } 986*ffea6052Smseidel 987cdf0e10cSrcweir XAutoTextGroup xGroup; 988cdf0e10cSrcweir String [] aBlockNames; 989*ffea6052Smseidel 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 { 995*ffea6052Smseidel // 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 ); 999*ffea6052Smseidel 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(); 1008*ffea6052Smseidel 1009cdf0e10cSrcweir // increment our depth counter 1010cdf0e10cSrcweir ++nDepth; 1011cdf0e10cSrcweir } 1012cdf0e10cSrcweir while ( nDepth < 200 && aBlockNames.length == 0 ); 1013*ffea6052Smseidel // If we managed to find a group containing blocks... 1014cdf0e10cSrcweir if ( aBlockNames.length > 0 ) 1015cdf0e10cSrcweir { 1016*ffea6052Smseidel // Pick a random block in this group and get its 1017cdf0e10cSrcweir // XAutoTextEntry interface 1018*ffea6052Smseidel int nRandom = Math.abs ( maRandom.nextInt() 1019cdf0e10cSrcweir % aBlockNames.length ); 1020cdf0e10cSrcweir XAutoTextEntry xEntry = ( XAutoTextEntry ) 1021*ffea6052Smseidel 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 ); 1026*ffea6052Smseidel 1027cdf0e10cSrcweir // Get the titles of all text blocks in this AutoText group 1028cdf0e10cSrcweir String [] aBlockTitles = xGroup.getTitles(); 1029*ffea6052Smseidel 1030cdf0e10cSrcweir // Get the XNamed interface of the autotext group 1031cdf0e10cSrcweir XNamed xGroupNamed = ( XNamed ) UnoRuntime.queryInterface ( 1032cdf0e10cSrcweir XNamed.class, xGroup ); 1033*ffea6052Smseidel 1034*ffea6052Smseidel // Output the short cut and title of the random block 1035*ffea6052Smseidel // and the name of the group it's from 1036cdf0e10cSrcweir System.out.println ( "Inserted the Autotext '" + 1037*ffea6052Smseidel 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 1047*ffea6052Smseidel mxDocText.insertControlCharacter ( 1048cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1049*ffea6052Smseidel 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 } 1060*ffea6052Smseidel catch (Exception e) 1061cdf0e10cSrcweir { 1062cdf0e10cSrcweir e.printStackTrace(); 1063cdf0e10cSrcweir } 1064cdf0e10cSrcweir } 1065*ffea6052Smseidel 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 ); 1077*ffea6052Smseidel XParagraphCursor xParaCursor = (XParagraphCursor) 1078cdf0e10cSrcweir UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor ); 1079cdf0e10cSrcweir xParaCursor.gotoPreviousParagraph ( false ); 1080*ffea6052Smseidel 1081*ffea6052Smseidel // Create a new ContentIndexMark and get its XPropertySet interface 1082cdf0e10cSrcweir XPropertySet xEntry = (XPropertySet)UnoRuntime.queryInterface( 1083*ffea6052Smseidel 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!"); 1089*ffea6052Smseidel 1090cdf0e10cSrcweir // The Level property _must_ be set 1091cdf0e10cSrcweir xEntry.setPropertyValue ( "Level", new Short ( (short) 1 ) ); 1092*ffea6052Smseidel 1093*ffea6052Smseidel // Create a ContentIndex and access its XPropertySet interface 1094cdf0e10cSrcweir XPropertySet xIndex = (XPropertySet) UnoRuntime.queryInterface( 1095*ffea6052Smseidel 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 1103*ffea6052Smseidel XTextContent xIndexContent = (XTextContent) UnoRuntime.queryInterface( 1104cdf0e10cSrcweir XTextContent.class, xIndex ); 1105*ffea6052Smseidel XTextContent xEntryContent = (XTextContent) UnoRuntime.queryInterface( 1106cdf0e10cSrcweir XTextContent.class, xEntry ); 1107*ffea6052Smseidel 1108cdf0e10cSrcweir // Insert both in the document 1109cdf0e10cSrcweir mxDocText.insertTextContent ( mxDocCursor, xEntryContent, false ); 1110cdf0e10cSrcweir mxDocText.insertTextContent ( mxDocCursor, xIndexContent, false ); 1111*ffea6052Smseidel 1112cdf0e10cSrcweir // Get the XDocumentIndex interface of the Index 1113*ffea6052Smseidel XDocumentIndex xDocIndex = (XDocumentIndex) UnoRuntime.queryInterface( 1114cdf0e10cSrcweir XDocumentIndex.class, xIndex ); 1115*ffea6052Smseidel 1116*ffea6052Smseidel // And call its update method 1117cdf0e10cSrcweir xDocIndex.update(); 1118cdf0e10cSrcweir } 1119*ffea6052Smseidel catch (Exception e) 1120cdf0e10cSrcweir { 1121cdf0e10cSrcweir e.printStackTrace(); 1122cdf0e10cSrcweir } 1123cdf0e10cSrcweir } 1124*ffea6052Smseidel 1125cdf0e10cSrcweir /** This method demonstrates how to create and insert reference marks, and 1126*ffea6052Smseidel * 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 ); 1134*ffea6052Smseidel 1135cdf0e10cSrcweir // Insert a paragraph break 1136*ffea6052Smseidel mxDocText.insertControlCharacter ( 1137cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1138*ffea6052Smseidel 1139cdf0e10cSrcweir // Get the Paragraph cursor 1140*ffea6052Smseidel XParagraphCursor xParaCursor = (XParagraphCursor) 1141cdf0e10cSrcweir UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor ); 1142*ffea6052Smseidel 1143cdf0e10cSrcweir // Move the cursor into the new paragraph 1144cdf0e10cSrcweir xParaCursor.gotoPreviousParagraph ( false ); 1145*ffea6052Smseidel 1146*ffea6052Smseidel // Create a new ReferenceMark and get its 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" ); 1152*ffea6052Smseidel 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" ) ); 1168*ffea6052Smseidel 1169cdf0e10cSrcweir // Get a text cursor for the first cell 1170cdf0e10cSrcweir XTextCursor xTableCursor = xTableText.createTextCursor(); 1171*ffea6052Smseidel 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 ); 1179*ffea6052Smseidel 1180cdf0e10cSrcweir // Create a 'GetReference' text field to refer to the reference mark 1181*ffea6052Smseidel // we just inserted, and get its 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(); 1196*ffea6052Smseidel 1197*ffea6052Smseidel // 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 ( 1203*ffea6052Smseidel "GetReference text field inserted for ReferenceMark : " 1204cdf0e10cSrcweir + aNames[0] ); 1205*ffea6052Smseidel 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' 1216*ffea6052Smseidel xFieldProps.setPropertyValue ( "ReferenceFieldPart", 1217cdf0e10cSrcweir new Short(ReferenceFieldPart.UP_DOWN)); 1218cdf0e10cSrcweir 1219*ffea6052Smseidel 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 ); 1226*ffea6052Smseidel 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.. 1234*ffea6052Smseidel 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 } 1243*ffea6052Smseidel catch (Exception e) 1244cdf0e10cSrcweir { 1245cdf0e10cSrcweir e.printStackTrace(); 1246cdf0e10cSrcweir } 1247cdf0e10cSrcweir } 1248*ffea6052Smseidel 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 { 1256*ffea6052Smseidel // Create a new footnote from the document factory and get its 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" ); 1264*ffea6052Smseidel 1265cdf0e10cSrcweir // Get the footnotes XTextContent interface so we can... 1266*ffea6052Smseidel XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface ( 1267cdf0e10cSrcweir XTextContent.class, xFootnote ); 1268*ffea6052Smseidel 1269cdf0e10cSrcweir // ...insert it into the document 1270cdf0e10cSrcweir mxDocText.insertTextContent ( mxDocCursor, xContent, false ); 1271*ffea6052Smseidel 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') 1281*ffea6052Smseidel 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 ); 1287*ffea6052Smseidel 1288cdf0e10cSrcweir // Create a text cursor for the foot note text 1289cdf0e10cSrcweir XTextRange xRange = (XTextRange ) UnoRuntime.queryInterface ( 1290cdf0e10cSrcweir XTextRange.class, xSimple.createTextCursor() ); 1291*ffea6052Smseidel 1292cdf0e10cSrcweir // And insert the actual text of the footnote. 1293*ffea6052Smseidel xSimple.insertString ( 1294cdf0e10cSrcweir xRange, " The numbers were generated by using java.util.Random", false ); 1295cdf0e10cSrcweir } 1296*ffea6052Smseidel catch (Exception e) 1297cdf0e10cSrcweir { 1298cdf0e10cSrcweir e.printStackTrace(); 1299cdf0e10cSrcweir } 1300cdf0e10cSrcweir } 1301*ffea6052Smseidel 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 1318*ffea6052Smseidel XParagraphCursor xParaCursor = (XParagraphCursor) 1319cdf0e10cSrcweir UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor ); 1320cdf0e10cSrcweir 1321cdf0e10cSrcweir // Position the cursor in the 2nd paragraph 1322cdf0e10cSrcweir xParaCursor.gotoPreviousParagraph ( false ); 1323*ffea6052Smseidel 1324cdf0e10cSrcweir // Create a RectangleShape using the document factory 1325*ffea6052Smseidel XShape xRect = (XShape) UnoRuntime.queryInterface( 1326cdf0e10cSrcweir XShape.class, mxDocFactory.createInstance ( 1327cdf0e10cSrcweir "com.sun.star.drawing.RectangleShape" ) ); 1328*ffea6052Smseidel 1329cdf0e10cSrcweir // Create an EllipseShape using the document factory 1330*ffea6052Smseidel XShape xEllipse = (XShape) UnoRuntime.queryInterface( 1331*ffea6052Smseidel XShape.class, mxDocFactory.createInstance ( 1332cdf0e10cSrcweir "com.sun.star.drawing.EllipseShape" ) ); 1333*ffea6052Smseidel 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 ); 1342*ffea6052Smseidel 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 ); 1348*ffea6052Smseidel 1349cdf0e10cSrcweir // Get the XPropertySet interfaces of both shapes 1350*ffea6052Smseidel XPropertySet xRectProps = (XPropertySet) UnoRuntime.queryInterface( 1351cdf0e10cSrcweir XPropertySet.class, xRect ); 1352cdf0e10cSrcweir XPropertySet xEllipseProps = (XPropertySet) UnoRuntime.queryInterface( 1353cdf0e10cSrcweir XPropertySet.class, xEllipse ); 1354*ffea6052Smseidel 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 1366*ffea6052Smseidel XShapes xShapes = ( XShapes ) UnoRuntime.queryInterface ( 1367cdf0e10cSrcweir XShapes.class, xDrawPageSupplier.getDrawPage () ); 1368*ffea6052Smseidel 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. 1375*ffea6052Smseidel 1376*ffea6052Smseidel XShapes xGrouper = (XShapes) UnoRuntime.queryInterface( 1377*ffea6052Smseidel XShapes.class, mxDocFactory.createInstance ( 1378cdf0e10cSrcweir "com.sun.star.drawing.GroupShape" ) ); 1379*ffea6052Smseidel 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 */ 1391*ffea6052Smseidel 1392cdf0e10cSrcweir } 1393*ffea6052Smseidel catch (Exception e) 1394cdf0e10cSrcweir { 1395cdf0e10cSrcweir e.printStackTrace(); 1396cdf0e10cSrcweir } 1397*ffea6052Smseidel } 1398*ffea6052Smseidel 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 ); 1407*ffea6052Smseidel 1408cdf0e10cSrcweir // Insert two paragraph breaks 1409*ffea6052Smseidel mxDocText.insertControlCharacter ( 1410cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1411*ffea6052Smseidel mxDocText.insertControlCharacter ( 1412cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1413*ffea6052Smseidel 1414cdf0e10cSrcweir // Create a new style from the document's factory 1415*ffea6052Smseidel XStyle xStyle = (XStyle) UnoRuntime.queryInterface( 1416*ffea6052Smseidel XStyle.class, mxDocFactory.createInstance( 1417cdf0e10cSrcweir "com.sun.star.style.ParagraphStyle" ) ); 1418*ffea6052Smseidel 1419cdf0e10cSrcweir // Access the XPropertySet interface of the new style 1420cdf0e10cSrcweir XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface( 1421cdf0e10cSrcweir XPropertySet.class, xStyle ); 1422*ffea6052Smseidel 1423cdf0e10cSrcweir // Give the new style a light blue background 1424cdf0e10cSrcweir xStyleProps.setPropertyValue ( "ParaBackColor", new Integer (13421823)); 1425*ffea6052Smseidel 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 1432*ffea6052Smseidel XNameAccess xFamilies = ( XNameAccess ) UnoRuntime.queryInterface ( 1433cdf0e10cSrcweir XNameAccess.class, xSupplier.getStyleFamilies() ); 1434cdf0e10cSrcweir 1435cdf0e10cSrcweir // Access the 'ParagraphStyles' Family 1436*ffea6052Smseidel 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 1444*ffea6052Smseidel 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 ); 1461*ffea6052Smseidel 1462cdf0e10cSrcweir // Select the last paragraph in the document 1463cdf0e10cSrcweir xParaCursor.gotoNextParagraph ( true ); 1464*ffea6052Smseidel 1465*ffea6052Smseidel // And reset its style to 'Standard' (the programmatic name for 1466cdf0e10cSrcweir // the default style) 1467cdf0e10cSrcweir xCursorProps.setPropertyValue ( "ParaStyleName", "Standard" ); 1468cdf0e10cSrcweir } 1469*ffea6052Smseidel catch (Exception e) 1470cdf0e10cSrcweir { 1471cdf0e10cSrcweir e.printStackTrace(); 1472cdf0e10cSrcweir } 1473cdf0e10cSrcweir } 1474*ffea6052Smseidel 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 1485*ffea6052Smseidel XRelativeTextContentInsert xRelative = 1486cdf0e10cSrcweir (XRelativeTextContentInsert ) UnoRuntime.queryInterface ( 1487cdf0e10cSrcweir XRelativeTextContentInsert.class, mxDocText ); 1488cdf0e10cSrcweir 1489cdf0e10cSrcweir // Use the document's factory to create the NumberingRules service, 1490*ffea6052Smseidel // and get its 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" ) ); 1508*ffea6052Smseidel 1509cdf0e10cSrcweir // Get the XPropertySet interface of the new paragraph and put 1510cdf0e10cSrcweir // it in our array 1511*ffea6052Smseidel 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 { 1530*ffea6052Smseidel // Once we find it, set its value to a new type, 1531cdf0e10cSrcweir // dependent on which numbering level we're currently on 1532cdf0e10cSrcweir switch ( i ) 1533cdf0e10cSrcweir { 1534*ffea6052Smseidel case 0 : aProps[j].Value = 1535cdf0e10cSrcweir new Short(NumberingType.ROMAN_UPPER); 1536cdf0e10cSrcweir break; 1537*ffea6052Smseidel case 1 : aProps[j].Value = 1538cdf0e10cSrcweir new Short(NumberingType.CHARS_UPPER_LETTER); 1539cdf0e10cSrcweir break; 1540*ffea6052Smseidel 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 1546*ffea6052Smseidel xReplace.replaceByIndex ( i, aProps ); 1547cdf0e10cSrcweir break; 1548cdf0e10cSrcweir } 1549cdf0e10cSrcweir } 1550cdf0e10cSrcweir } 1551*ffea6052Smseidel // Get the XParagraphCursor interface of our text cursor 1552*ffea6052Smseidel 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 ); 1559*ffea6052Smseidel 1560cdf0e10cSrcweir // Get the XPropertySet of the cursor's currently selected text 1561cdf0e10cSrcweir XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface( 1562cdf0e10cSrcweir XPropertySet.class, mxDocCursor ); 1563*ffea6052Smseidel 1564cdf0e10cSrcweir // Set the updated Numbering rules to the cursor's property set 1565cdf0e10cSrcweir xCursorProps.setPropertyValue ( "NumberingRules", xNum ); 1566cdf0e10cSrcweir mxDocCursor.gotoEnd( false ); 1567*ffea6052Smseidel 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 } 1580*ffea6052Smseidel catch (Exception e) 1581cdf0e10cSrcweir { 1582cdf0e10cSrcweir e.printStackTrace(); 1583cdf0e10cSrcweir } 1584cdf0e10cSrcweir } 1585*ffea6052Smseidel 1586*ffea6052Smseidel /** 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 1595*ffea6052Smseidel mxDocText.insertControlCharacter ( 1596cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1597*ffea6052Smseidel mxDocText.insertControlCharacter ( 1598cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, true ); 1599*ffea6052Smseidel 1600cdf0e10cSrcweir // Create a new TextSection from the document factory and access 1601*ffea6052Smseidel // its 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" ); 1607*ffea6052Smseidel 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 ); 1620*ffea6052Smseidel 1621cdf0e10cSrcweir // Insert a string into the Child_Section 1622cdf0e10cSrcweir mxDocText.insertString ( mxDocCursor, "This is a test", false ); 1623*ffea6052Smseidel 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 ); 1632*ffea6052Smseidel 1633*ffea6052Smseidel // Create another text section and access its 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" ); 1639*ffea6052Smseidel 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 ); 1645*ffea6052Smseidel 1646cdf0e10cSrcweir // Go to the end of the document 1647cdf0e10cSrcweir mxDocCursor.gotoEnd ( false ); 1648cdf0e10cSrcweir // Insert a new paragraph 1649*ffea6052Smseidel mxDocText.insertControlCharacter ( 1650cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1651*ffea6052Smseidel // And select the new paragraph 1652cdf0e10cSrcweir xParaCursor.gotoPreviousParagraph ( true ); 1653*ffea6052Smseidel 1654*ffea6052Smseidel // Create a new Text Section and access its 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" ); 1659*ffea6052Smseidel 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 ); 1665*ffea6052Smseidel 1666cdf0e10cSrcweir // Access the Linked_Section's XPropertySet interface 1667*ffea6052Smseidel 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" ); 1671*ffea6052Smseidel 1672cdf0e10cSrcweir // Access the XPropertySet interface of the Child_Section 1673cdf0e10cSrcweir XPropertySet xChildProps = (XPropertySet) UnoRuntime.queryInterface( 1674cdf0e10cSrcweir XPropertySet.class, xChildNamed ); 1675*ffea6052Smseidel // Set the Child_Section's background color to blue 1676cdf0e10cSrcweir xChildProps.setPropertyValue( "BackColor", new Integer(13421823)); 1677*ffea6052Smseidel 1678cdf0e10cSrcweir // Refresh the document, so the linked section matches the Child_Section 1679*ffea6052Smseidel XRefreshable xRefresh = (XRefreshable) UnoRuntime.queryInterface( 1680cdf0e10cSrcweir XRefreshable.class, mxDoc ); 1681cdf0e10cSrcweir xRefresh.refresh(); 1682cdf0e10cSrcweir } 1683*ffea6052Smseidel catch (Exception e) 1684cdf0e10cSrcweir { 1685cdf0e10cSrcweir e.printStackTrace(); 1686cdf0e10cSrcweir } 1687cdf0e10cSrcweir } 1688*ffea6052Smseidel 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 { 1696*ffea6052Smseidel // Go to the end of the document 1697cdf0e10cSrcweir mxDocCursor.gotoEnd( false ); 1698cdf0e10cSrcweir // insert a new paragraph 1699*ffea6052Smseidel mxDocText.insertControlCharacter ( 1700cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1701*ffea6052Smseidel 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 1708*ffea6052Smseidel mxDocText.insertControlCharacter ( 1709cdf0e10cSrcweir mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 1710*ffea6052Smseidel 1711cdf0e10cSrcweir // Get the XParagraphCursor interface of our text cursor 1712*ffea6052Smseidel 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 ); 1717*ffea6052Smseidel 1718cdf0e10cSrcweir // Insert a string at the beginning of the block of text 1719cdf0e10cSrcweir mxDocText.insertString ( mxDocCursor, "Fish section begins:", false ); 1720*ffea6052Smseidel 1721cdf0e10cSrcweir // Then select all of the text 1722cdf0e10cSrcweir xParaCursor.gotoNextParagraph ( true ); 1723cdf0e10cSrcweir xParaCursor.gotoNextParagraph ( true ); 1724*ffea6052Smseidel 1725*ffea6052Smseidel // Create a new text section and get its XNamed interface 1726cdf0e10cSrcweir XNamed xSectionNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class, 1727cdf0e10cSrcweir mxDocFactory.createInstance("com.sun.star.text.TextSection")); 1728*ffea6052Smseidel 1729a893be29SPedro Giffuni // Set the name of our new section (appropriately) to 'Fish' 1730cdf0e10cSrcweir xSectionNamed.setName ( "Fish" ); 1731cdf0e10cSrcweir 1732*ffea6052Smseidel // Create the TextColumns service and get its 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 ); 1739*ffea6052Smseidel 1740cdf0e10cSrcweir // Get the TextColumns, and make the middle one narrow with a larger 1741cdf0e10cSrcweir // margin on the left than the right 1742*ffea6052Smseidel 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 ); 1748*ffea6052Smseidel 1749cdf0e10cSrcweir // Get the property set interface of our 'Fish' section 1750cdf0e10cSrcweir XPropertySet xSectionProps = (XPropertySet) UnoRuntime.queryInterface( 1751cdf0e10cSrcweir XPropertySet.class, xSectionNamed ); 1752*ffea6052Smseidel 1753cdf0e10cSrcweir // Set the columns to the Text Section 1754cdf0e10cSrcweir xSectionProps.setPropertyValue ( "TextColumns", xColumns ); 1755*ffea6052Smseidel 1756cdf0e10cSrcweir // Get the XTextContent interface of our 'Fish' section 1757cdf0e10cSrcweir mxFishSection = (XTextContent) UnoRuntime.queryInterface( 1758cdf0e10cSrcweir XTextContent.class, xSectionNamed ); 1759*ffea6052Smseidel 1760cdf0e10cSrcweir // Insert the 'Fish' section over the currently selected text 1761cdf0e10cSrcweir mxDocText.insertTextContent ( mxDocCursor, mxFishSection, true ); 1762*ffea6052Smseidel 1763cdf0e10cSrcweir // Get the wonderful XRelativeTextContentInsert interface 1764cdf0e10cSrcweir XRelativeTextContentInsert xRelative = (XRelativeTextContentInsert ) 1765cdf0e10cSrcweir UnoRuntime.queryInterface ( 1766cdf0e10cSrcweir XRelativeTextContentInsert.class, mxDocText ); 1767*ffea6052Smseidel 1768*ffea6052Smseidel // Create a new empty paragraph and get its 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 } 1776*ffea6052Smseidel catch (Exception e) 1777cdf0e10cSrcweir { 1778cdf0e10cSrcweir e.printStackTrace(); 1779cdf0e10cSrcweir } 1780*ffea6052Smseidel } 1781cdf0e10cSrcweir } 1782