创建文字、表格和绘图形状

From Apache OpenOffice Wiki
Jump to: navigation, search



上述三种 manipulateXXX 方法将文字、表格和形状对象用作参数并对其进行了更改。以下方法说明如何在不同文档类型中创建此类对象。请注意,所有文档都具有自己的服务工厂,以创建要插入文 档的对象。除此以外,如何进行操作,很大程度上依赖文档类型。本节仅介绍各个过程,具体解释 可参阅文字、电子表格和绘图文档的相关各章。


首先,使用一个简便的方法来创建新文档。

  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);    
  }


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.

useWriter 方法可创建 Writer 文档并对其文字进行处理,然后使用文档的内部服务管理器来实例化文字表格和形状,将其插入并处理表格和形状。如果需要更多信息,请参阅 文本文档

  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;
          }
 
  }


Calc 中的文字、表格和绘图

useCalc 方法可创建 Calc 文档,使用其文档工厂来创建形状,并处理单元格文字、表格和形状。电子表格文档 一章中处理了电子表格的所有方面。

  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;
          }
 
  }



Draw 中的绘图和文字

useDraw 方法可创建绘图文档,使用其文档工厂来实例化和添加形状,然后处理形状。绘图和演示文稿章节进一步介绍了绘图和演示文稿。

  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
In other languages