文字、表格和绘图的通用机制

From Apache OpenOffice Wiki
Jump to: navigation, search



我们想在这里突出共同点,因此先介绍用于处理现有文字、表格和绘图的通用接口和属性。然后, 再介绍在每种文档类型中创建文字、表格和绘图的不同技巧。


用于现有文字、表格和绘图的主要接口和属性如下所示:

对于文字,com.sun.star.text.XText 接口含有用于更改实际文字和其他文字内容的方法。(除了常规文字段落以外,文字内容还包括文字表格、文字字段、图形对象以及其他类似内容,但这些内 容并非在所有上下文中都可用。)我们这里提到的文字都是指所有文字,即所有包含在文本文档、 文字框、页眉和页脚、表格单元格或绘图形状中的文字。XText 是 OpenOffice.org 中所有文字的关键字。


XTextRange 接口

com.sun.star.text.XText 接口可以设置或获得作为单个字符串的文字,并可以找到文字的开始和结束位置。另外,XText 可以在文字中的任意位置插入字符串,并创建文字光标以选择和格式化文字。最后,XText 通过 insertTextContentremoveTextContent 方法来处理文字内容,尽管并 非所有文字都接受除常规文字以外的文字内容。实际上,XText 通过继承 com.sun.star.text.XSimpleText(从 com.sun.star.text.XTextRange 继承而来)而涵盖了所 有的文字内容。


文字格式化通过 com.sun.star.style.ParagraphPropertiescom.sun.star.style.CharacterProperties 服务中描述的属性来实现。


以下示例方法 manipulateText() 将添加文字,然后通过 CharacterProperties 并使用文字光标来选择和格式化字词,之后再插入更多的文字。manipulateText() 方法只含有 XText 最基本的方法,因此它可以用于每个文字对象。它尤其避免使用 insertTextContent(),因为除了常规文字外,其他任何文字内容都不能保证能够插入到所有文字对象中。

  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 = (XPropertySet)UnoRuntime.queryInterface(
              XPropertySet.class, xTextCursor);
 
          // use cursor to select "He lay" and apply bold italic
          xTextCursor.gotoStart(false);
          xTextCursor.goRight((short)6, true);        
          // from CharacterProperties
          xCursorProps.setPropertyValue("CharPosture",    
              com.sun.star.awt.FontSlant.ITALIC);
          xCursorProps.setPropertyValue("CharWeight", 
              new Float(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);   
  }


表格和表格单元格中, com.sun.star.table.XCellRange 接口用于获得单个单元格和单元格的分区域。有了单元格后,可以通过 com.sun.star.table.XCell 接口来使用其公式或数值。


单元格和单元格区域


文字表格中的表格格式与电子表格中的表格格式并不完全相同。文字表格使用 com.sun.star.text.TextTable 中指定的属性,而电子表格使用 com.sun.star.table.CellProperties 中指定的属性。另外,表格光标还可用于选择和格式化单元格区域及其含有的文字。但由于 com.sun.star.text.TextTableCursorcom.sun.star.sheet.SheetCellCursor 的工作方式有很大差别,因此我们将在文本文档和电子表格文档的相关章节中对它们进行介绍。

  protected void manipulateTable(XCellRange xCellRange) throws com.sun.star.uno.Exception {
 
          String backColorPropertyName = "";
          XPropertySet xTableProps = null;
 
          // enter column titles and a cell value
  // Enter "Quotation" in A1, "Year" in B1. We use setString because we want to change the whole
  // cell text at once
          XCell xCell = xCellRange.getCellByPosition(0,0);
          XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
          xCellText.setString("Quotation");
          xCell = xCellRange.getCellByPosition(1,0);
          xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell);
          xCellText.setString("Year");
 
  // cell value
  xCell = xCellRange.getCellByPosition(1,1);
          xCell.setValue(1940);
 
  // select the table headers and get the cell properties
  XCellRange xSelectedCells = xCellRange.getCellRangeByName("A1:B1");
          XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
              XPropertySet.class, xSelectedCells);
 
          // format the color of the table headers and table borders
          // we need to distinguish text and spreadsheet tables:
          // - the property name for cell colors is different in text and sheet cells
          // - the common property for table borders is com.sun.star.table.TableBorder, but 
  //   we must apply the property TableBorder to the whole text table, 
  //   whereas we only want borders for spreadsheet cells with content.
 
        // XServiceInfo allows to distinguish text tables from spreadsheets
  XServiceInfo xServiceInfo = (XServiceInfo)UnoRuntime.queryInterface(
              XServiceInfo.class, xCellRange);
 
  // determine the correct property name for background color and the XPropertySet interface
  // for the cells that should get colored border lines
  if (xServiceInfo.supportsService("com.sun.star.sheet.Spreadsheet")) {
              backColorPropertyName = "CellBackColor";
              // select cells
     xSelectedCells = xCellRange.getCellRangeByName("A1:B2");
     // table properties only for selected cells
              xTableProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, xSelectedCells);
          }
          else if (xServiceInfo.supportsService("com.sun.star.text.TextTable")) {
              backColorPropertyName = "BackColor";
    // table properties for whole table
              xTableProps = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, xCellRange);
          }       
          // set cell background color
          xCellProps.setPropertyValue(backColorPropertyName, new Integer(0x99CCFF));
 
          // set table borders
          // create description for blue line, width 10
          // colors are given in ARGB, comprised of four bytes for alpha-red-green-blue as in 0xAARRGGBB  
  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 = bord.HorizontalLine = 
              bord.LeftLine = bord.RightLine = 
              bord.TopLine = bord.BottomLine = 
                  theLine;
          bord.IsVerticalLineValid = bord.IsHorizontalLineValid = 
              bord.IsLeftLineValid = bord.IsRightLineValid = 
              bord.IsTopLineValid = bord.IsBottomLineValid =
                  true;
 
          xTableProps.setPropertyValue("TableBorder", bord);
 
  }


在绘图形状上 com.sun.star.drawing.XShape 接口用于确定形状的位置和大小。

XShape 接口


剩下的事情就是进行基于属性的格式化,要使用的属性会有很多。OpenOffice.org 附带了十一种不同的形 状作为 GUI(图形用户界面)中绘图工具的基础。其中六种形状具有反映其属性的单独属性。这六 种形状是:



另外五种形状没有单独的属性,它们共享 com.sun.star.drawing.PolyPolygonBezierDescriptor 服务中定义的属性:

所有这十一种形状都使用以下服务中的属性:


留意以下显示这些属性如何工作的示例:

  protected void manipulateShape(XShape xShape) throws com.sun.star.uno.Exception {
          // for usage of setSize and setPosition in interface XShape see method useDraw() below
  XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShape);
  // colors are given in ARGB, comprised of four bytes for alpha-red-green-blue as in 0xAARRGGBB
          xShapeProps.setPropertyValue("FillColor", new Integer(0x99CCFF));
          xShapeProps.setPropertyValue("LineColor", new Integer(0x000099));
          // angles are given in hundredth degrees, rotate by 30 degrees
  xShapeProps.setPropertyValue("RotateAngle", new Integer(3000));
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages