1'*************************************************************************
2'
3' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4'
5' Copyright 2000, 2010 Oracle and/or its affiliates.
6'
7' OpenOffice.org - a multi-platform office productivity suite
8'
9' This file is part of OpenOffice.org.
10'
11' OpenOffice.org is free software: you can redistribute it and/or modify
12' it under the terms of the GNU Lesser General Public License version 3
13' only, as published by the Free Software Foundation.
14'
15' OpenOffice.org is distributed in the hope that it will be useful,
16' but WITHOUT ANY WARRANTY; without even the implied warranty of
17' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18' GNU Lesser General Public License version 3 for more details
19' (a copy is included in the LICENSE file that accompanied this code).
20'
21' You should have received a copy of the GNU Lesser General Public License
22' version 3 along with OpenOffice.org.  If not, see
23' <http://www.openoffice.org/license.html>
24' for a copy of the LGPLv3 License.
25'
26'*************************************************************************
27
28Option Explicit On
29Option Strict On
30
31imports System
32imports System.Collections
33imports Microsoft.VisualBasic
34imports unoidl.com.sun.star.lang
35imports unoidl.com.sun.star.uno
36imports unoidl.com.sun.star.bridge
37imports uno.util
38
39Module WriterDemo
40
41Sub Main( ByVal args() As String)
42'    If args.Length <> 2 Then
43'        Console.WriteLine("WriterDemo takes two arguments. A file url to the office" & _
44'        "program directory and a connection string.")
45'    End If
46'Connect to a running office
47'--------------------------------------------------
48
49'Create a service manager of the remote office
50'Dim ht As Hashtable = New Hashtable()
51'ht.Add("SYSBINDIR", args(0))
52Dim xContext As XComponentContext
53'xLocalContext = Bootstrap.defaultBootstrap_InitialComponentContext( _
54'    args(0) & "/uno.ini", ht.GetEnumerator())
55
56xContext = Bootstrap.bootstrap()
57
58'Dim xURLResolver As XUnoUrlResolver
59'xURLResolver = DirectCast(xLocalContext.getServiceManager(). _
60'    createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", _
61'	    xLocalContext), XUnoUrlResolver)
62
63'Dim xRemoteContext As XComponentContext
64'xRemoteContext = DirectCast(xURLResolver.resolve( _
65'	"uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext"), _
66'        XComponentContext)
67
68Dim xFactory As XMultiServiceFactory
69xFactory = DirectCast(xContext.getServiceManager(), _
70    XMultiServiceFactory)
71
72'Create the Desktop
73Dim xDesktop As unoidl.com.sun.star.frame.XDesktop
74xDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), _
75    unoidl.com.sun.star.frame.XDesktop)
76
77'Open a new empty writer document
78Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader
79xComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)
80Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = _
81    New unoidl.com.sun.star.beans.PropertyValue(){}
82Dim xComponent As unoidl.com.sun.star.lang.XComponent
83    xComponent = xComponentLoader.loadComponentFromURL( _
84   "private:factory/swriter", "_blank", 0, arProps)
85Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument
86xTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)
87
88'Create a text object
89Dim xText As unoidl.com.sun.star.text.XText
90xText = xTextDocument.getText()
91
92Dim xSimpleText As unoidl.com.sun.star.text.XSimpleText
93xSimpleText = DirectCast(xText, unoidl.com.sun.star.text.XSimpleText)
94
95'Create a cursor object
96Dim xCursor As unoidl.com.sun.star.text.XTextCursor
97xCursor = xSimpleText.createTextCursor()
98
99'Inserting some Text
100xText.insertString(xCursor, "The first line in the newly created text document." _
101    & vbLf, false)
102
103'Create instance of a text table with 4 columns and 4 rows
104Dim objTextTable As Object
105objTextTable= DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory). _
106    createInstance("com.sun.star.text.TextTable")
107Dim xTextTable As unoidl.com.sun.star.text.XTextTable
108xTextTable = DirectCast(objTextTable, unoidl.com.sun.star.text.XTextTable)
109xTextTable.initialize(4, 4)
110xText.insertTextContent(xCursor, xTextTable, false)
111
112'Set the table background color
113Dim xPropertySetTable As unoidl.com.sun.star.beans.XPropertySet
114xPropertySetTable = DirectCast(objTextTable, unoidl.com.sun.star.beans.XPropertySet)
115xPropertySetTable.setPropertyValue("BackTransparent", New uno.Any(False))
116xPropertySetTable.setPropertyValue("BackColor", New uno.Any(&Hccccff))
117
118'Get first row
119Dim xTableRows As unoidl.com.sun.star.table.XTableRows
120xTableRows = xTextTable.getRows()
121Dim anyRow As uno.Any
122anyRow = DirectCast(xTableRows, unoidl.com.sun.star.container.XIndexAccess).getByIndex( 0)
123
124'Set a different background color for the first row
125Dim xPropertySetFirstRow As unoidl.com.sun.star.beans.XPropertySet
126xPropertySetFirstRow = DirectCast(anyRow.Value, unoidl.com.sun.star.beans.XPropertySet)
127xPropertySetFirstRow.setPropertyValue("BackTransparent", New uno.Any(False))
128xPropertySetFirstRow.setPropertyValue("BackColor", New uno.Any(&H6666AA))
129
130'Fill the first table row
131insertIntoCell("A1","FirstColumn", xTextTable)
132insertIntoCell("B1","SecondColumn", xTextTable)
133insertIntoCell("C1","ThirdColumn", xTextTable)
134insertIntoCell("D1","SUM", xTextTable)
135
136'Fill the remaining rows
137xTextTable.getCellByName("A2").setValue(22.5)
138xTextTable.getCellByName("B2").setValue(5615.3)
139xTextTable.getCellByName("C2").setValue(-2315.7)
140xTextTable.getCellByName("D2").setFormula("sum <A2:C2>")
141
142xTextTable.getCellByName("A3").setValue(21.5)
143xTextTable.getCellByName("B3").setValue (615.3)
144xTextTable.getCellByName("C3").setValue( -315.7)
145xTextTable.getCellByName("D3").setFormula( "sum <A3:C3>")
146
147xTextTable.getCellByName("A4").setValue( 121.5)
148xTextTable.getCellByName("B4").setValue( -615.3)
149xTextTable.getCellByName("C4").setValue( 415.7)
150xTextTable.getCellByName("D4").setFormula( "sum <A4:C4>")
151
152'Change the CharColor and add a Shadow
153Dim xPropertySetCursor As unoidl.com.sun.star.beans.XPropertySet
154xPropertySetCursor = DirectCast(xCursor, unoidl.com.sun.star.beans.XPropertySet)
155xPropertySetCursor.setPropertyValue("CharColor", New uno.Any(255))
156xPropertySetCursor.setPropertyValue("CharShadowed", New uno.Any(true))
157
158'Create a paragraph break
159xSimpleText.insertControlCharacter(xCursor, _
160    unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
161
162'Inserting colored Text.
163xSimpleText.insertString(xCursor," This is a colored Text - blue with shadow" & vbLf, _
164    False)
165
166'Create a paragraph break
167xSimpleText.insertControlCharacter(xCursor, _
168    unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
169
170'Create a TextFrame.
171Dim objTextFrame As Object
172objTextFrame = DirectCast(xTextDocument, unoidl.com.sun.star.lang.XMultiServiceFactory). _
173    createInstance("com.sun.star.text.TextFrame")
174
175Dim xTextFrame As unoidl.com.sun.star.text.XTextFrame = _
176    DirectCast(objTextFrame, unoidl.com.sun.star.text.XTextFrame)
177
178'Set the size of the frame
179Dim aSize As unoidl.com.sun.star.awt.Size = _
180    New unoidl.com.sun.star.awt.Size(15000, 400)
181DirectCast(xTextFrame, unoidl.com.sun.star.drawing.XShape).setSize(aSize)
182
183'Set anchortype
184Dim xPropertySetFrame As unoidl.com.sun.star.beans.XPropertySet
185xPropertySetFrame = DirectCast(xTextFrame, unoidl.com.sun.star.beans.XPropertySet)
186xPropertySetFrame.setPropertyValue("AnchorType", New uno.Any( _
187    GetType(unoidl.com.sun.star.text.TextContentAnchorType), _
188    unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER))
189
190'insert the frame
191xText.insertTextContent(xCursor, xTextFrame, False)
192
193'Get the text object of the frame
194
195Dim xFrameText As unoidl.com.sun.star.text.XText
196xFrameText = xTextFrame.getText()
197
198Dim xFrameSimpleText As unoidl.com.sun.star.text.XSimpleText
199xFrameSimpleText = DirectCast(xFrameText, unoidl.com.sun.star.text.XSimpleText)
200
201'Create a cursor object
202Dim xFrameCursor As unoidl.com.sun.star.text.XTextCursor
203xFrameCursor = xFrameSimpleText.createTextCursor()
204
205'Inserting some Text
206xFrameSimpleText.insertString(xFrameCursor, _
207    "The first line in the newly created text frame.", False)
208xFrameSimpleText.insertString(xFrameCursor, _
209    vbLf & "With this second line the height of the frame raises.", False)
210
211'Create a paragraph break
212xSimpleText.insertControlCharacter(xFrameCursor, _
213    unoidl.com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
214
215'Change the CharColor and add a Shadow
216xPropertySetCursor.setPropertyValue("CharColor", New uno.Any(65536))
217xPropertySetCursor.setPropertyValue("CharShadowed", New uno.Any(False))
218
219'Insert another string
220xText.insertString(xCursor, vbLf + " That's all for now !!", False)
221
222End Sub
223
224Sub insertIntoCell(sCellName As String,sText As String, _
225     xTable As unoidl.com.sun.star.text.XTextTable)
226    Dim xCell As unoidl.com.sun.star.table.XCell
227    xCell = xTable.getCellByName(sCellName)
228
229    Dim xSimpleTextCell As unoidl.com.sun.star.text.XSimpleText
230    xSimpleTextCell = DirectCast(xCell, unoidl.com.sun.star.text.XSimpleText)
231
232    Dim xCursor As unoidl.com.sun.star.text.XTextCursor
233    xCursor = xSimpleTextCell.createTextCursor()
234
235    Dim xPropertySetCursor As unoidl.com.sun.star.beans.XPropertySet
236    xPropertySetCursor = DirectCast(xCursor, unoidl.com.sun.star.beans.XPropertySet)
237
238    xPropertySetCursor.setPropertyValue("CharColor", New uno.Any(&Hffffff))
239    xSimpleTextCell.insertString(xCursor, sText, False)
240End Sub
241
242End Module
243