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