/************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * *************************************************************/ @GrabResolver(name = 'guno', root = 'https://repository.apache.org/content/repositories/orgapacheopenoffice-1016/') @Grab("org.openoffice:bootstrap-connector:0.1.1") @Grab("org.openoffice:juh:4.1.2") @Grab("org.openoffice:ridl:4.1.2") @Grab("org.openoffice:unoil:4.1.2") @Grab("org.openoffice:jurt:4.1.2") @Grab("org.openoffice:guno-extension:0.1.4") // needed the following to fix // Caught: BUG! exception in phase 'conversion' in source unit @GrabExclude('org.codehaus.groovy:groovy-all') // @GrabConfig(systemClassLoader=true) import com.sun.star.lang.XComponent import com.sun.star.uno.XComponentContext import com.sun.star.uno.UnoRuntime import com.sun.star.uno.AnyConverter import com.sun.star.bridge.XUnoUrlResolver import com.sun.star.frame.XComponentLoader import com.sun.star.lang.XMultiComponentFactory import com.sun.star.lang.XMultiServiceFactory import com.sun.star.lang.XServiceInfo import com.sun.star.beans.XPropertySet import com.sun.star.beans.XPropertySetInfo import com.sun.star.beans.PropertyValue import com.sun.star.beans.UnknownPropertyException import com.sun.star.beans.PropertyVetoException import com.sun.star.text.XTextDocument import com.sun.star.text.XText import com.sun.star.text.XTextCursor import com.sun.star.text.XWordCursor import com.sun.star.text.XTextContent import com.sun.star.text.XTextTable; import com.sun.star.text.XTextTableCursor import com.sun.star.table.XTableRows import com.sun.star.table.XCellRange import com.sun.star.table.XCell import com.sun.star.table.XCellCursor import com.sun.star.table.TableBorder import com.sun.star.table.BorderLine import com.sun.star.drawing.XShape import com.sun.star.awt.Size; import com.sun.star.awt.Point; import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.sheet.XSpreadsheet import com.sun.star.sheet.XSheetCellCursor import com.sun.star.container.XIndexAccess import com.sun.star.drawing.XDrawPagesSupplier import com.sun.star.drawing.XDrawPageSupplier import com.sun.star.drawing.XDrawPage import com.sun.star.text.XTextTablesSupplier import com.sun.star.container.XNameAccess import com.sun.star.container.XNamed import com.sun.star.text.XBookmarksSupplier import com.sun.star.text.XTextRange import ooo.connector.BootstrapSocketConnector /* * HelloTextTableShape.groovy is based on HelloTextTableShape.java by author dschulten * provided with the Apache OpenOffice SDK examples. * Changes were made to highlight the use Apache Groovy, the Groovy UNO Extension (guno-extension), * and the bootstrap-connector. */ /** * * @author Carl B. Marcum */ public class HelloTextTableShape { // EDIT FOR LOCATION OF SOFFICE private String oooExeFolder = "/opt/openoffice4/program/" // private String oooExeFolder = "C:/Program Files (x86)/OpenOffice 4/program" private XComponentContext xRemoteContext = null private XMultiComponentFactory xRemoteServiceManager = null /** Creates a new instance of HelloTextTableShape */ public HelloTextTableShape() { } /** * @param args the command line arguments */ public static void main(String[] args) { HelloTextTableShape helloTextTableShape1 = new HelloTextTableShape() try { helloTextTableShape1.useDocuments() } catch (java.lang.Exception e) { System.err.println(e.getMessage()) e.printStackTrace() } finally { System.exit(0) } } protected void useDocuments() throws java.lang.Exception { useWriter() useCalc() useDraw() } protected void useWriter() throws java.lang.Exception { try { // create new writer document and get text, then manipulate text XComponent xWriterComponent = newDocComponent("swriter") XTextDocument xTextDocument = xWriterComponent.guno(XTextDocument.class) XText xText = xTextDocument.getText() manipulateText(xText) // get internal service factory of the document XMultiServiceFactory xWriterFactory = xWriterComponent.guno(XMultiServiceFactory.class) // insert TextTable and get cell text, then manipulate text in cell Object table = xWriterFactory.createInstance("com.sun.star.text.TextTable") XTextContent xTextContentTable = table.guno(XTextContent.class) xText.insertTextContent(xText.getEnd(), xTextContentTable, false) XCellRange xCellRange = table.guno(XCellRange.class) XCell xCell = xCellRange.getCellByPosition(0, 1) XText xCellText = xCell.guno(XText.class) manipulateText(xCellText) manipulateTable(xCellRange) // insert RectangleShape and get shape text, then manipulate text Object writerShape = xWriterFactory.createInstance("com.sun.star.drawing.RectangleShape") XShape xWriterShape = writerShape.guno(XShape.class) xWriterShape.setSize(new Size(10000, 10000)) XTextContent xTextContentShape = writerShape.guno(XTextContent.class) xText.insertTextContent(xText.getEnd(), xTextContentShape, false) XPropertySet xShapeProps = writerShape.guno(XPropertySet.class) // wrap text inside shape xShapeProps.setPropertyValue("TextContourFrame", true) XText xShapeText = writerShape.guno(XText.class) manipulateText(xShapeText) manipulateShape(xWriterShape) // more code snippets used in the manual: Object bookmark = xWriterFactory.createInstance("com.sun.star.text.Bookmark") // name the bookmark XNamed xNamed = bookmark.guno(XNamed.class) xNamed.setName("MyUniqueBookmarkName") // get XTextContent interface and insert it at the end of the document XTextContent xTextContent = bookmark.guno(XTextContent.class) //mxDocText.insertTextContent ( mxDocText.getEnd(), xTextContent, false ); xText.insertTextContent(xText.getEnd(), xTextContent, false) //query BookmarksSupplier XBookmarksSupplier xBookmarksSupplier = xWriterComponent.guno(XBookmarksSupplier.class) XNameAccess xNamedBookmarks = xBookmarksSupplier.bookmarks Object foundBookmark = xNamedBookmarks.getByName("MyUniqueBookmarkName") XTextContent xFoundBookmark = foundBookmark.guno(XTextContent.class) XTextRange xFound = xFoundBookmark.anchor xFound.setString(" The throat mike, glued to her neck, " + "looked as much as possible like an analgesic dermadisk.") // first query the XTextTablesSupplier interface from our document XTextTablesSupplier xTablesSupplier = xWriterComponent.guno(XTextTablesSupplier.class) // get the tables collection XNameAccess xNamedTables = xTablesSupplier.textTables // now query the XIndexAccess from the tables collection XIndexAccess xIndexedTables = xNamedTables.guno(XIndexAccess.class) // we need properties XPropertySet xTableProps = null // get the tables for (int i = 0; i < xIndexedTables.getCount(); i++) { //Object table = xIndexedTables.getByIndex(i); table = xIndexedTables.getByIndex(i) xTableProps = table.guno(XPropertySet.class) xTableProps.setPropertyValue("BackColor", 0xC8FFB9) } // ended here } catch (com.sun.star.lang.DisposedException e) { //works from Patch 1 xRemoteContext = null throw e } } protected void useCalc() throws java.lang.Exception { try { // create new calc document and manipulate cell text XComponent xCalcComponent = newDocComponent("scalc") XSpreadsheetDocument xSpreadsheetDocument = xCalcComponent.guno(XSpreadsheetDocument.class) Object sheets = xSpreadsheetDocument.sheets XIndexAccess xIndexedSheets = sheets.guno(XIndexAccess.class) Object sheet = xIndexedSheets.getByIndex(0) //get cell A2 in first sheet XCellRange xSpreadsheetCells = sheet.guno(XCellRange.class) XCell xCell = xSpreadsheetCells.getCellByPosition(0, 1) XPropertySet xCellProps = xCell.guno(XPropertySet.class) xCellProps.setPropertyValue("IsTextWrapped", true) XText xCellText = xCell.guno(XText.class) manipulateText(xCellText) manipulateTable(xSpreadsheetCells) // get internal service factory of the document XMultiServiceFactory xCalcFactory = xCalcComponent.guno(XMultiServiceFactory.class) // get Drawpage XDrawPageSupplier xDrawPageSupplier = sheet.guno(XDrawPageSupplier.class) XDrawPage xDrawPage = xDrawPageSupplier.drawPage // create and insert RectangleShape and get shape text, then manipulate text Object calcShape = xCalcFactory.createInstance("com.sun.star.drawing.RectangleShape") XShape xCalcShape = calcShape.guno(XShape.class) xCalcShape.setSize(new Size(10000, 10000)) xCalcShape.setPosition(new Point(7000, 3000)) xDrawPage.add(xCalcShape) XPropertySet xShapeProps = calcShape.guno(XPropertySet.class) // wrap text inside shape xShapeProps.setPropertyValue("TextContourFrame", true) XText xShapeText = calcShape.guno(XText.class) manipulateText(xShapeText) manipulateShape(xCalcShape) } catch (com.sun.star.lang.DisposedException e) { //works from Patch 1 xRemoteContext = null throw e } } protected void useDraw() throws java.lang.Exception { try { //create new draw document and insert rectangle shape XComponent xDrawComponent = newDocComponent("sdraw") XDrawPagesSupplier xDrawPagesSupplier = xDrawComponent.guno(XDrawPagesSupplier.class) Object drawPages = xDrawPagesSupplier.drawPages XIndexAccess xIndexedDrawPages = drawPages.guno(XIndexAccess.class) Object drawPage = xIndexedDrawPages.getByIndex(0) XDrawPage xDrawPage = drawPage.guno(XDrawPage.class) // get internal service factory of the document XMultiServiceFactory xDrawFactory = xDrawComponent.guno(XMultiServiceFactory.class) Object drawShape = xDrawFactory.createInstance("com.sun.star.drawing.RectangleShape") XShape xDrawShape = drawShape.guno(XShape.class) xDrawShape.setSize(new Size(10000, 20000)) xDrawShape.setPosition(new Point(5000, 5000)) xDrawPage.add(xDrawShape) XText xShapeText = drawShape.guno(XText.class) XPropertySet xShapeProps = drawShape.guno(XPropertySet.class) // wrap text inside shape xShapeProps.setPropertyValue("TextContourFrame", true) manipulateText(xShapeText) manipulateShape(xDrawShape) } catch (com.sun.star.lang.DisposedException e) { //works from Patch 1 xRemoteContext = null throw e } } protected void manipulateText(XText xText) throws com.sun.star.uno.Exception { // simply set whole text as one string xText.setString("He lay flat on the brown, pine-needled floor of the forest, " + "his chin on his folded arms, and high overhead the wind blew in the tops " + "of the pine trees.") // create text cursor for selecting and formatting XTextCursor xTextCursor = xText.createTextCursor() XPropertySet xCursorProps = xTextCursor.guno(XPropertySet.class) // use cursor to select "He lay" and apply bold italic xTextCursor.gotoStart(false) xTextCursor.goRight((short) 6, true) // from CharacterProperties xCursorProps.putAt("CharPosture", com.sun.star.awt.FontSlant.ITALIC) xCursorProps.putAt("CharWeight", com.sun.star.awt.FontWeight.BOLD) // add more text at the end of the text using insertString xTextCursor.gotoEnd(false) xText.insertString(xTextCursor, " The mountainside sloped gently where he lay; " + "but below it was steep and he could see the dark of the oiled road " + "winding through the pass. There was a stream alongside the road " + "and far down the pass he saw a mill beside the stream and the falling water " + "of the dam, white in the summer sunlight.", false); // after insertString the cursor is behind the inserted text, insert more text xText.insertString(xTextCursor, "\n \"Is that the mill?\" he asked.", false) } protected void manipulateTable(XCellRange xCellRange) throws com.sun.star.uno.Exception { String backColorPropertyName = "" XPropertySet xTableProps = null // enter column titles and a cell value XCell xCell = xCellRange.getCellByPosition(0, 0) XText xCellText = xCell.guno(XText.class) xCellText.setString("Quotation") xCell = xCellRange.getCellByPosition(1, 0) xCellText = xCell.guno(XText.class) xCellText.setString("Year") xCell = xCellRange.getCellByPosition(1, 1) xCell.setValue(1940) XCellRange xSelectedCells = xCellRange.getCellRangeByName("A1:B1") XPropertySet xCellProps = xSelectedCells.guno(XPropertySet.class) // format table headers and table borders // we need to distinguish text and sheet tables: // property name for cell colors is different in text and sheet cells // we want to apply TableBorder to whole text table, but only to sheet cells with content XServiceInfo xServiceInfo = xCellRange.guno(XServiceInfo.class) if (xServiceInfo.supportsService("com.sun.star.sheet.Spreadsheet")) { backColorPropertyName = "CellBackColor" xSelectedCells = xCellRange.getCellRangeByName("A1:B2") xTableProps = xSelectedCells.guno(XPropertySet.class) } else if (xServiceInfo.supportsService("com.sun.star.text.TextTable")) { backColorPropertyName = "BackColor" xTableProps = xCellRange.guno(XPropertySet.class) } // set cell background color xCellProps.setPropertyValue(backColorPropertyName, 0x99CCFF) // set table borders // create description for blue line, width 10 BorderLine theLine = new BorderLine() theLine.Color = 0x000099 theLine.OuterLineWidth = 10 // apply line description to all border lines and make them valid TableBorder bord = new TableBorder() bord.VerticalLine = theLine bord.HorizontalLine = theLine bord.LeftLine = theLine bord.RightLine = theLine bord.TopLine = theLine bord.BottomLine = theLine bord.IsVerticalLineValid = true bord.IsHorizontalLineValid = true bord.IsLeftLineValid = true bord.IsRightLineValid = true bord.IsTopLineValid = true bord.IsBottomLineValid = true xTableProps.setPropertyValue("TableBorder", bord) bord = xTableProps.getAt("TableBorder") theLine = bord.TopLine int col = theLine.Color println(col) } protected void manipulateShape(XShape xShape) throws com.sun.star.uno.Exception { XPropertySet xShapeProps = xShape.guno(XPropertySet.class) xShapeProps.putAt("FillColor", 0x99CCFF) xShapeProps.putAt("LineColor", 0x000099) xShapeProps.putAt("RotateAngle", 3000) xShapeProps.putAt("TextLeftDistance", 0) xShapeProps.putAt("TextRightDistance", 0) xShapeProps.putAt("TextUpperDistance", 0) xShapeProps.putAt("TextLowerDistance", 0) } protected XComponent newDocComponent(String docType) throws java.lang.Exception { String loadUrl = "private:factory/" + docType xRemoteServiceManager = this.remoteServiceManager Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext) XComponentLoader xComponentLoader = desktop.guno(XComponentLoader.class) PropertyValue[] loadProps = new PropertyValue[0] return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps) } protected XMultiComponentFactory getRemoteServiceManager() throws java.lang.Exception { if (xRemoteContext == null && xRemoteServiceManager == null) { try { // First step: get the remote office component context // xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); // instead we use the bootstrapconnector for test xRemoteContext = BootstrapSocketConnector.bootstrap(oooExeFolder) println "Connected to a running office ..." xRemoteServiceManager = xRemoteContext.serviceManager } catch (Exception e) { e.printStackTrace() System.exit(1) } } return xRemoteServiceManager } }