Creating Text, Tables and Drawing Shapes

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 13:22, 1 November 2007 by Cc208500 (Talk)

Jump to: navigation, search



The three manipulateXXX methods above took text, table and shape objects as parameters and altered them. The following methods show how to create such objects in the various document types. Note that all documents have their own service factory to create objects to be inserted into the document. Aside from that it depends very much on the document type how you proceed. This section only demonstrates the different procedures, the explanation can be found in the chapters about Text, Spreadsheet and Drawing Documents.

First, a small convenience method is used to create new documents.

  protected XComponent newDocComponent(String docType) throws java.lang.Exception {
          String loadUrl = "private:factory/" + docType;
          xRemoteServiceManager = this.getRemoteServiceManager(unoUrl);
          Object desktop = xRemoteServiceManager.createInstanceWithContext(
              "com.sun.star.frame.Desktop", xRemoteContext);
          XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
              XComponentLoader.class, desktop);
          PropertyValue[] loadProps = new PropertyValue[0];
          return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);    
  }

Text, Tables and Drawings in Writer

The method useWriter creates a writer document and manipulates its text, then uses the document's internal service manager to instantiate a text table and a shape, inserts them and manipulates the table and shape. Refer to Text Documents for more detailed information.

  protected void useWriter() throws java.lang.Exception {
          try {
              // create new writer document and get text, then manipulate text
              XComponent xWriterComponent = newDocComponent("swriter");
              XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(
                  XTextDocument.class, xWriterComponent);
              XText xText = xTextDocument.getText();
 
              manipulateText(xText);
 
              // get internal service factory of the document
              XMultiServiceFactory xWriterFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
                  XMultiServiceFactory.class, xWriterComponent);
 
              // insert TextTable and get cell text, then manipulate text in cell
              Object table = xWriterFactory.createInstance("com.sun.star.text.TextTable");
              XTextContent xTextContentTable = (XTextContent)UnoRuntime.queryInterface(
                  XTextContent.class, table);
 
              xText.insertTextContent(xText.getEnd(), xTextContentTable, false);
 
              XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface(
                  XCellRange.class, table);
              XCell xCell = xCellRange.getCellByPosition(0, 1);
              XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
 
              manipulateText(xCellText);
              manipulateTable(xCellRange);
 
              // insert RectangleShape and get shape text, then manipulate text
              Object writerShape = xWriterFactory.createInstance(
                  "com.sun.star.drawing.RectangleShape");
              XShape xWriterShape = (XShape)UnoRuntime.queryInterface(
                  XShape.class, writerShape);
              xWriterShape.setSize(new Size(10000, 10000));
              XTextContent xTextContentShape = (XTextContent)UnoRuntime.queryInterface(
                  XTextContent.class, writerShape);
 
              xText.insertTextContent(xText.getEnd(), xTextContentShape, false);
 
              XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, writerShape);
              // wrap text inside shape
              xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));
 
 
              XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, writerShape);
 
              manipulateText(xShapeText);
              manipulateShape(xWriterShape); 
          }
          catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
              xRemoteContext = null;
              throw e;
          }
 
  }

Text, Tables and Drawings in Calc

The method useCalc creates a calc document, uses its document factory to create a shape and manipulates the cell text, table and shape. The chapter Spreadsheet Documents treats all aspects of spreadsheets.

  protected void useCalc() throws java.lang.Exception {
          try {
              // create new calc document and manipulate cell text
              XComponent xCalcComponent = newDocComponent("scalc");
              XSpreadsheetDocument  xSpreadsheetDocument  = 
                  (XSpreadsheetDocument)UnoRuntime.queryInterface(
                      XSpreadsheetDocument .class, xCalcComponent);
              Object sheets = xSpreadsheetDocument.getSheets();
              XIndexAccess xIndexedSheets = (XIndexAccess)UnoRuntime.queryInterface(
                  XIndexAccess.class, sheets);
              Object sheet =  xIndexedSheets.getByIndex(0);
 
              //get cell A2 in first sheet
              XCellRange xSpreadsheetCells = (XCellRange)UnoRuntime.queryInterface(
                  XCellRange.class, sheet);
              XCell xCell = xSpreadsheetCells.getCellByPosition(0,1);
              XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, xCell);
              xCellProps.setPropertyValue("IsTextWrapped", new Boolean(true));
 
              XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
 
              manipulateText(xCellText);
              manipulateTable(xSpreadsheetCells);
 
              // get internal service factory of the document
              XMultiServiceFactory xCalcFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
                  XMultiServiceFactory.class, xCalcComponent);
              // get Drawpage
              XDrawPageSupplier xDrawPageSupplier =
         (XDrawPageSupplier)UnoRuntime.queryInterface(XDrawPageSupplier.class, sheet);
              XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage();
 
              // create and insert RectangleShape and get shape text, then manipulate text
              Object calcShape = xCalcFactory.createInstance(
                  "com.sun.star.drawing.RectangleShape");
              XShape xCalcShape = (XShape)UnoRuntime.queryInterface(
                  XShape.class, calcShape);
              xCalcShape.setSize(new Size(10000, 10000));
              xCalcShape.setPosition(new Point(7000, 3000));
 
              xDrawPage.add(xCalcShape);
 
              XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, calcShape);
              // wrap text inside shape
              xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));
 
 
              XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, calcShape);
 
              manipulateText(xShapeText);
              manipulateShape(xCalcShape); 
 
          }
          catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
              xRemoteContext = null;
              throw e;
          }
 
  }

Drawings and Text in Draw

The method useDraw creates a draw document and uses its document factory to instantiate and add a shape, then it manipulates the shape. The chapter Drawing Documents and Presentation Documents casts more light on drawings and presentations.

  protected void useDraw() throws java.lang.Exception {
          try {
              //create new draw document and insert ractangle shape
              XComponent xDrawComponent = newDocComponent("sdraw");
              XDrawPagesSupplier xDrawPagesSupplier = 
                  (XDrawPagesSupplier)UnoRuntime.queryInterface(
                      XDrawPagesSupplier.class, xDrawComponent);
 
              Object drawPages = xDrawPagesSupplier.getDrawPages();
              XIndexAccess xIndexedDrawPages = (XIndexAccess)UnoRuntime.queryInterface(
                  XIndexAccess.class, drawPages);
              Object drawPage = xIndexedDrawPages.getByIndex(0);
              XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawPage);
 
              // get internal service factory of the document
              XMultiServiceFactory xDrawFactory = 
                  (XMultiServiceFactory)UnoRuntime.queryInterface(
                      XMultiServiceFactory.class, xDrawComponent);
 
              Object drawShape = xDrawFactory.createInstance(
                  "com.sun.star.drawing.RectangleShape");
              XShape xDrawShape = (XShape)UnoRuntime.queryInterface(XShape.class, drawShape);
              xDrawShape.setSize(new Size(10000, 20000)); 
              xDrawShape.setPosition(new Point(5000, 5000));
              xDrawPage.add(xDrawShape);
 
              XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, drawShape);
              XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, drawShape);
 
              // wrap text inside shape
              xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true));            
 
              manipulateText(xShapeText);
              manipulateShape(xDrawShape);
          }
          catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
              xRemoteContext = null;
              throw e;
          }
 
 
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools