Fremmedobjekt i teksten

From Apache OpenOffice Wiki
< NO/Documentation/BASIC Guide
Revision as of 05:12, 16 December 2009 by Ketil3 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Tekstdokument kan ha mer enn ren tekst i avsnitt. Fremmedobjekt som tabeller, tegninger, tekstfelter og annet kan forankres eller flyte rundt i dokumentet.

TextContent

Forankring styres via tjenesten com.sun.star.text.TextContent som alle objekt støtter. Egenskaper:

AnchorType (Enum)
Hvordan objektet skal forankres (com.sun.star.text.TextContentAnchorType).
AnchorTypes (sequence of Enum)
Alle typer forankring for objektet.
TextWrap (Enum)
Hvordan tekst skal "omslutte" objektet (com.sun.star.text.WrapTextMode).

Metoder som dokumentet tilbyr:

  • createInstance() lager et nytt objekt som støtter TextContent.
  • insertTextContent() legger et objekt inn i teksten.
  • removeTextContent() fjerner et objekt fra teksten.

Tabeller

Eksemplet viser innsetting av en ny tabell:

Dim Kalender As Object
Dim Cursor As Object
Cursor = ThisComponent.Text.createTextCursor()
Kalender = ThisComponent.createInstance("com.sun.star.text.TextTable")
Kalender.initialize(12,31)
ThisComponent.Text.insertTextContent(Cursor, Kalender, False)

En ser at en ved innsetting også skal angi posisjon (cursor) og om det utvalgte skal erstattes (True) eller beholdes (False).

Å vise alle tabeller gjøres som under:

alleTabeller = ThisComponent.getTextTables()
For I = 0 to alleTabeller.count - 1
   tabell = TextTables(I)
   ... 
Next I

En tabell har rader og celler, men ingen eksplisitte kolonner.

Tabeller støtter com.sun.star.text.TextTable og har egenskaper som:

BackColor (Long)
background color of table.
BottomMargin (Long)
bottom margin in 100ths of a millimeter.
LeftMargin (Long)
left margin in 100ths of a millimeter.
RightMargin (Long)
right margin in 100ths of a millimeter.
TopMargin (Long)
top margin in 100ths of a millimeter.
RepeatHeadline (Boolean)
table header is repeated on every page.
Width (Long)
absolute width of the table in 100ths of a millimeter.

Tabellens rader nås som følger:

Dim Doc As Object
Dim Table As Object
Dim Cursor As Object
Dim Rows As Object
Dim Row As Object
Dim I As Integer
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
 
Table = Doc.createInstance("com.sun.star.text.TextTable")
Table.initialize(5, 4)
 
Doc.Text.insertTextContent(Cursor, Table, False)
Rows = Table.getRows
For I = 0 To Rows.getCount() - 1
   Row = Rows.getByIndex(I)
   Row.BackColor = &HFF00FF
Next

Liste over alle rader

Tabellen tilbyr getRows som gir en liste med alle rader. Denne listen støtter com.sun.star.table.XTableRows, et interface som tilbyr:

getByIndex(Integer)
returns a row object for the specified index.
getCount()
returns the number of row objects.
insertByIndex(Index, Count)
inserts Count rows in the table as of the Index position.
removeByIndex(Index, Count)
deletes Count rows from the table as of the Index position.

OBS: insert og remove kun hvis tabellen er fri for "merged cells".

Enkeltrad

Hver rad støtter com.sun.star.text.TextTableRow service og tilbyr følgende:

BackColor (Long)
background color of row.
Height (Long)
height of line in 100ths of a millimeter.
IsAutoHeight (Boolean)
table height is dynamically adapted to the content.
VertOrient (const)
vertical orientation of the text frame — details on vertical orientation of the text within the table (values in accordance with com.sun.star.text.VertOrientation)

Columns

Columns are accessed in the same way as rows, using the getByIndex, getCount, insertByIndex, and removeByIndex methods on the Column object, which is reached through getColumns. They can, however, only be used in tables that do not contain merged table cells. Cells cannot be formatted by column in Apache OpenOffice Basic. To do so, the method of formatting individual table cells must be used.

Cells

Each cell of a Apache OpenOffice document has a unique name. If the cursor of Apache OpenOffice is in a cell, then the name of that cell can be seen in the status bar. The top left cell is usually called A1 and the bottom right row is usually called Xn, where X stands for the letters of the top column and n for the numbers of the last row. The cell objects are available through the getCellByName() method of the table object. The following example shows a loop that passes through all the cells of a table and enters the corresponding row and column numbers into the cells.

Dim Doc As Object
Dim Table As Object
Dim Cursor As Object
Dim Rows As Object
Dim RowIndex As Integer
Dim Cols As Object
Dim ColIndex As Integer
Dim CellName As String
Dim Cell As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
 
Table = Doc.createInstance("com.sun.star.text.TextTable")
Table.initialize(5, 4)
 
Doc.Text.insertTextContent(Cursor, Table, False)
 
Rows = Table.getRows
Cols = Table.getColumns
 
For RowIndex = 1 To Rows.getCount()
   For ColIndex = 1 To Cols.getCount()
      CellName = Chr(Asc("A") - 1 + ColIndex) & RowIndex
      Cell = Table.getCellByName(CellName)
      Cell.String = "row: " & CStr(RowIndex) + ", column: " & CStr(ColIndex)
   Next
Next

A table cell is comparable with a standard text. It supports the createTextCursor interface for creating an associated TextCursor object.

CellCursor = Cell.createTextCursor()

All formatting options for individual characters and paragraphs are therefore automatically available.

The following example searches through all tables of a text document and applies the right-align format to all cells with numerical values by means of the corresponding paragraph property.

Dim Doc As Object
Dim TextTables As Object
Dim Table As Object
Dim CellNames
Dim Cell As Object
Dim CellCursor As Object
Dim I As Integer
Dim J As Integer
 
Doc = ThisComponent
TextTables = Doc.getTextTables()
 
For I = 0 to TextTables.count - 1
   Table = TextTables(I)
   CellNames = Table.getCellNames()
 
   For J = 0 to UBound(CellNames)
      Cell = Table.getCellByName(CellNames(J))
      If IsNumeric(Cell.String) Then
         CellCursor = Cell.createTextCursor()
         CellCursor.paraAdjust = com.sun.star.style.ParagraphAdjust.RIGHT
      End If
   Next
Next

The example creates a TextTables list containing all tables of a text that are traversed in a loop. Apache OpenOffice then creates a list of the associated cell names for each of these tables. There are passed through in turn in a loop. If a cell contains a numerical value, then the example changes the formatting correspondingly. To do this, it first creates a TextCursor object which makes reference to the content of the table cell and then adapts the paragraph properties of the table cell.

Text Frames

Text frames are considered to be TextContent objects, just like tables and graphs. They may essentially consist of standard text, but can be placed at any position on a page and are not included in the text flow.

As with all TextContent objects, a distinction is also made with text frames between the actual creation and insertion in the document.

Dim Doc As Object
Dim TextTables As Object
Dim Cursor As Object
Dim Frame As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
Frame = Doc.createInstance("com.sun.star.text.TextFrame")
Doc.Text.insertTextContent(Cursor, Frame, False)

The text frame is created using the createInstance method of the document object. The text frame created in this way can then be inserted in the document using the insertTextContent method of the Text object. In so doing, the name of the proper com.sun.star.text.TextFrame service should be specified.

The text frame's insert position is determined by a Cursor object, which is also executed when inserted.

Documentation note.png VBA : Text frames are Apache OpenOffice's counterpart to the position frame used in Word. Whereas VBA uses the Document.Frames.Add method for this purpose, creation in Apache OpenOffice Basic is performed using the previous procedure with the aid of a TextCursor as well as the createInstance method of the document object.


Text frame objects provide a range of properties with which the position and behavior of the frame can be influenced. The majority of these properties are defined in the com.sun.star.text.BaseFrameProperties service, which is also supported by each TextFrame service. The central properties are:

BackColor (Long)
background color of the text frame.
BottomMargin (Long)
bottom margin in 100ths of a millimeter.
LeftMargin (Long)
left margin in 100ths of a millimeter.
RightMargin (Long)
right margin in 100ths of a millimeter.
TopMargin (Long)
top margin in 100ths of a millimeter.
Height (Long)
height of text frame in 100ths of a millimeter.
Width (Long)
width of text frame in 100ths of a millimeter.
HoriOrient (const)
horizontal orientation of text frame (in accordance with com.sun.star.text.HoriOrientation).
VertOrient (const)
vertical orientation of text frame (in accordance with com.sun.star.text.VertOrientation).

The following example creates a text frame using the properties described previously:

Dim Doc As Object
Dim TextTables As Object
Dim Cursor As Object
Dim Frame As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
Cursor.gotoNextWord(False)
Frame = Doc.createInstance("com.sun.star.text.TextFrame")
 
Frame.Width = 3000
Frame.Height = 1000
Frame.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
Frame.TopMargin = 0
Frame.BottomMargin = 0
Frame.LeftMargin = 0
Frame.RightMargin = 0
Frame.BorderDistance = 0
Frame.HoriOrient = com.sun.star.text.HoriOrientation.NONE
Frame.VertOrient = com.sun.star.text.VertOrientation.LINE_TOP
 
Doc.Text.insertTextContent(Cursor, Frame, False)

The example creates a TextCursor as the insertion mark for the text frame. This is positioned between the first and second word of the text. The text frame is created using Doc.createInstance. The properties of the text frame objects are set to the starting values required.

The interaction between the AnchorType (from the TextContent Service) and VertOrient (from the BaseFrameProperties Service) properties should be noted here. AnchorType receives the AS_CHARACTER value. The text frame is therefore inserted directly in the text flow and behaves like a character. It can, for example, be moved into the next line if a line break occurs. The LINE_TOP value of the VertOrient property ensures that the upper edge of the text frame is at the same height as the upper edge of the character.

Once initialization is complete, the text frame is finally inserted in the text document using a call from insertTextContent.

To edit the content of a text frame, the user uses the TextCursor, which has already been mentioned numerous times and is also available for text frames.

Dim Doc As Object
Dim TextTables As Object
Dim Cursor As Object
Dim Frame As Object
Dim FrameCursor As Object
 
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
Frame = Doc.createInstance("com.sun.star.text.TextFrame")
 
Frame.Width = 3000
Frame.Height = 1000
 
Doc.Text.insertTextContent(Cursor, Frame, False)
 
FrameCursor = Frame.createTextCursor()
FrameCursor.charWeight = com.sun.star.awt.FontWeight.BOLD
FrameCursor.paraAdjust = com.sun.star.style.ParagraphAdjust.CENTER
FrameCursor.String = "This is a small Test!"

The example creates a text frame, inserts this in the current document and opens a TextCursor for the text frame. This cursor is used to set the frame font to bold type and to set the paragraph orientation to centered. The text frame is finally assigned the “This is a small test!” string.

Text Fields

Text fields are TextContent objects because they provide additional logic extending beyond pure text. Text fields can be inserted in a text document using the same methods as those used for other TextContent objects:

Dim Doc As Object
Dim DateTimeField As Object
Dim Cursor As Object
Doc = ThisComponent
Cursor = Doc.Text.createTextCursor()
 
DateTimeField = Doc.createInstance("com.sun.star.text.textfield.DateTime")
DateTimeField.IsFixed = False
DateTimeField.IsDate = True
Doc.Text.insertTextContent(Cursor, DateTimeField, False)

The example inserts a text field with the current date at the start of the current text document. The True value of the IsDate property results in only the date and not time being displayed. The False value for IsFixed ensures that the date is automatically updated when the document is opened.

Documentation note.png VBA : While the type of a field in VBA is specified by a parameter of the Document.Fields.Add method, the name of the service that is responsible for the field type in question defines it in Apache OpenOffice Basic.


In the past, text fields were accessed using a whole range of methods that Apache OpenOffice made available in the old Selection object (for example InsertField, DeleteUserField, SetCurField).

In Apache OpenOffice 2.x, the fields are administered using an object-oriented concept. To create a text field, a text field of the type required should first be created and initialized using the properties required. The text field is then inserted in the document using the insertTextContent method. A corresponding source text can be seen in the previous example. The most important field types and their properties are described in the following sections.

In addition to inserting text fields, searching a document for the fields can also be an important task. The following example shows how all text fields of a text document can be traversed in a loop and checked for their relevant type.

Dim Doc As Object
Dim TextFieldEnum As Object
Dim TextField As Object
Dim I As Integer
 
Doc = ThisComponent
 
TextFieldEnum = Doc.getTextFields.createEnumeration
 
While TextFieldEnum.hasMoreElements()
 
   TextField = TextFieldEnum.nextElement()
 
   If TextField.supportsService("com.sun.star.text.textfield.DateTime") Then
      MsgBox "Date/time"
   ElseIf TextField.supportsService("com.sun.star.text.textfield.Annotation") Then
      MsgBox "Annotation"
   Else
      MsgBox "unknown"
   End If
 
Wend

The starting point for establishing the text fields present is the TextFields list of the document object. The example creates an Enumeration object on the basis of this list, with which all text fields can be queried in turn in a loop. The text fields found are checked for the service supported using the supportsService method. If the field proves to be a date/time field or an annotation, then the corresponding field type is displayed in an information box. If on the other hand, the example encounters another field, then it displays the information “unknown”.

Below, you will find a list of the most important text fields and their associated properties. A complete list of all text fields is provided in the API reference in the com.sun.star.text.textfield module. (When listing the service name of a text field, uppercase and lowercase characters should be used in Apache OpenOffice Basic, as in the previous example.)

Number of Pages, Words and Characters

The text fields

return the number of pages, words, or characters of a text. They support the following property:

NumberingType (const)
numbering format (guidelines in accordance with constants from com.sun.star.style.NumberingType ).

Current Page

The number of the current page can be inserted in a document using the com.sun.star.text.textfield.PageNumber text field. The following properties can be specified:

NumberingType (const)
number format (guidelines in accordance with constants from com.sun.star.style.NumberingType).
Offset (short)
offset added to the number of pages (negative specification also possible).

The following example shows how the number of pages can be inserted into the footer of a document.

Dim Doc As Object
Dim DateTimeField As Object
Dim PageStyles As Object
Dim StdPage As Object
Dim FooterCursor As Object
Dim PageNumber As Object
 
Doc = ThisComponent
 
PageNumber = Doc.createInstance("com.sun.star.text.textfield.PageNumber")
PageNumber.NumberingType = com.sun.star.style.NumberingType.ARABIC
 
PageStyles = Doc.StyleFamilies.getByName("PageStyles")
 
StdPage = PageStyles("Default")
StdPage.FooterIsOn = True
 
FooterCursor = StdPage.FooterTextLeft.Text.createTextCursor()
StdPage.FooterTextLeft.Text.insertTextContent(FooterCursor, PageNumber, False)

The example first creates a text field which supports the com.sun.star.text.textfield.PageNumber service. Since the header and footer lines are defined as part of the page templates of Apache OpenOffice, this is initially established using the list of all PageStyles.

To ensure that the footer line is visible, the FooterIsOn property is set to True. The text field is then inserted in the document using the associated text object of the left-hand footer line.

Annotations

Annotation fields (com.sun.star.text.textfield.Annotation) can be seen by means of a small yellow symbol in the text. Clicking on this symbol opens a text field, in which a comment on the current point in the text can be recorded. An annotation field has the following properties.

Author (String)
name of author.
Content (String)
comment text.
Date (Date)
date on which annotation is written.

Date / Time

A date / time field (com.sun.star.text.textfield.DateTime) represents the current date or the current time. It supports the following properties:

IsFixed (Boolean)
if True, the time details of the insertion remain unchanged, if False, these are updated each time the document is opened.
IsDate (Boolean)
if True, the field displays the current date, otherwise the current time.
DateTimeValue (struct)
current content of field (com.sun.star.util.DateTime structure)
NumberFormat (const)
format in which the time or date is depicted.

Chapter Name / Number

The name of the current chapter is available through a text field of the com.sun.star.text.textfield.Chapter type. The form can be defined using two properties.

ChapterFormat (const)
determines whether the chapter name or the chapter number is depicted (in accordance with com.sun.star.text.ChapterFormat)
Level (Integer)
determines the chapter level whose name and/or chapter number is to be displayed. The value 0 stands for highest level available.

Bookmarks

Bookmarks (Service com.sun.star.text.Bookmark) are TextContent objects. Bookmarks are created and inserted using the concept already described previously:

Dim Doc As Object
Dim Bookmark As Object
Dim Cursor As Object
 
Doc = ThisComponent
 
Cursor = Doc.Text.createTextCursor()
 
Bookmark = Doc.createInstance("com.sun.star.text.Bookmark")
Bookmark.Name = "My bookmarks"
Doc.Text.insertTextContent(Cursor, Bookmark, True)

The example creates a Cursor, which marks the insert position of the bookmark and then the actual bookmark object (Bookmark). The bookmark is then assigned a name and is inserted in the document through insertTextContent at the cursor position.

The bookmarks of a text are accessed through a list called Bookmarks. The bookmarks can either be accessed by their number or their name.

The following example shows how a bookmark can be found within a text, and a text inserted at its position.

Dim Doc As Object
Dim Bookmark As Object
Dim Cursor As Object
 
Doc = ThisComponent
 
Bookmark = Doc.Bookmarks.getByName("My bookmarks")
 
Cursor = Doc.Text.createTextCursorByRange(Bookmark.Anchor)
Cursor.String = "Here is the bookmark"

In this example, the getByName method is used to find the bookmark required by means of its name. The createTextCursorByRange call then creates a Cursor, which is positioned at the anchor position of the bookmark. The cursor then inserts the text required at this point.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools