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