In this example, three new documents (Writer, SpreadSheet
and Drawing) are created.
On the Writer document:
On the SpreadSheet document:
On the Drawing document:
All three files are saved at the /tmp/ directory.
Click here to see the Writer document created by this example.
Click here to see the SpreadSheet document created by this example.
Click here to see the Drawing document created by this example.
<HTML>
<BODY>
<?php
$data=date("r");
echo " <BR>Started at: $data<BR>";
flush(NULL);
useWriter();
useCalc();
useDraw();
$data=date("r");
echo " <BR>Finished at: $data<BR>";
flush(NULL);
?>
<?php
function useWriter()
{
// create new writer document and get text, then manipulate text
$x_writer_component=newDocComponent("swriter");
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$td=$x_writer_component;
$x_text=$td->getText();
manipulateText($x_text);
// get internal service factory of the document
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_writer_factory=$x_writer_component;
// insert TextTable and get cell text, then manipulate text in cell
$table=$x_writer_factory->createInstance("com.sun.star.text.TextTable");
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_text_content_table=$table;
$x_text->insertTextContent($x_text->getEnd(),$x_text_content_table,false);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cell_range=$table;
$x_cell=$x_cell_range->getCellByPosition(0,1);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cell_text=$x_cell;
manipulateText($x_cell_text);
manipulateTable($x_cell_range);
// insert RectangleShape and get shape text, then manipulate text
$writer_shape=$x_writer_factory->createInstance("com.sun.star.drawing.RectangleShape");
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_writer_shape=$writer_shape;
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$shape_size=create_struct("com.sun.star.awt.Size");
$shape_size->Width=10000;
$shape_size->Height=10000;
$x_writer_shape->setSize($shape_size);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_text_content_shape=$writer_shape;
$x_text->insertTextContent($x_text->getEnd(),$x_text_content_shape,false);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_shape_props=$writer_shape;
//wrap text inside shape
$x_shape_props->setPropertyValue("TextContourFrame",true);
$x_shape_text=$writer_shape;
manipulateText($x_shape_text);
manipulateShape($x_writer_shape);
/* more code snippets used in the manual: */
$bookmark= $x_writer_factory->createInstance("com.sun.star.text.Bookmark");
//name the bookmark
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_named=$bookmark;
$x_named->setName("MyUniqueBookmarkName");
$x_text_content=$bookmark;
//$mx_doc_text->insertTextContent($mx_doc_text->getEnd(),$x_text_content,false);
$x_text->insertTextContent($x_text->getEnd(),$x_text_content,false);
//query BookmarksSupplier
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_bookmarks_supplier=$x_writer_component;
$x_named_bookmarks= $x_bookmarks_supplier->getBookmarks();
$found_bookmark=$x_named_bookmarks->getByName("MyUniqueBookmarkName");
$x_found_bookmark=$found_bookmark;
$x_found=$x_found_bookmark->getAnchor();
$x_found->setString(" The throat mike, glued to her neck, "
."looked as much as possible like an analgesic dermadisk.");
// first query the XTextTablesSupplier interface from our document
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_tables_supplier= $x_writer_component;
//get the tables collection
$x_named_tables= $x_tables_supplier->getTextTables();
//now query the XIndexAccess from the tables collection
$x_indexed_tables=$x_named_tables;
//we need properties
$x_table_props=NULL;
//get the tables
for($i=0; $i<$x_indexed_tables->getCount();$i++)
{
$table=$x_indexed_tables->getByIndex($i);
$x_table_props=$table;
$x_table_props->setPropertyValue("BackColor",0xC8FFB9);
}
$x_writer_component->storeToURL( "file:///tmp/example02.sxw", array() );
$x_writer_component->dispose();
}
function useCalc()
{
// create new calc document and manipulate cell text
$x_calc_component=newDocComponent("scalc");
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_spreadsheet_document = $x_calc_component;
$sheets=$x_spreadsheet_document->getSheets();
$x_indexed_sheets=$sheets;
//var_dump($x_indexed_sheets->getCount());
$sheet = $x_indexed_sheets->getByIndex(0);
//get cell A2 in first sheet
$x_spreadsheet_cells =$sheet;
$x_cell = $x_spreadsheet_cells->getCellByPosition(0,1);
$x_cell_props=$x_cell;
$x_cell_props->setPropertyValue("IsTextWrapped",true);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cell_text= $x_cell;
manipulateText($x_cell_text);
manipulateTable($x_spreadsheet_cells);
//get internal service factory of the document
$x_calc_factory = $x_calc_component;
// get Drawpage
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_draw_page_supplier = $sheet;
$x_draw_page = $x_draw_page_supplier->getDrawPage();
// create and insert RectangleShape and get shape text, then manipulate text
$calc_shape = $x_calc_factory->createInstance("com.sun.star.drawing.RectangleShape");
$x_calc_shape = $calc_shape;
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$size=create_struct("com.sun.star.awt.Size");
$size->Width=10000;
$size->Height=10000;
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$point=create_struct("com.sun.star.awt.Point");
$point->X=7000;
$point->Y=3000;
$x_calc_shape->setSize($size);
$x_calc_shape->setPosition($point);
$x_draw_page->add($x_calc_shape);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_shape_props = $calc_shape;
//wrap the text inside shape
$x_shape_props->setPropertyValue( "TextContourFrame",true );
$x_shape_text = $calc_shape;
manipulateText($x_shape_text);
manipulateShape($x_calc_shape);
$x_calc_component->storeToURL( "file:///tmp/example02.sxc", array() );
$x_calc_component->dispose();
}
function useDraw()
{
//create new draw document and insert rectangle shape
$x_draw_component= newDocComponent("sdraw");
$x_draw_pages_supplier=$x_draw_component;
$draw_pages=$x_draw_pages_supplier->getDrawPages();
$x_indexed_draw_pages=$draw_pages;
$draw_page=$x_indexed_draw_pages->getByIndex(0);
$x_draw_page=$draw_page;
// get internal service factory of the document
$x_draw_factory=$x_draw_component;
$draw_shape=$x_draw_factory->createInstance("com.sun.star.drawing.RectangleShape");
$x_draw_shape=$draw_shape;
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the&nbs
p;API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$size=create_struct("com.sun.star.awt.Size");
$size->Width=10000;
$size->Height=20000;
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$point=create_struct("com.sun.star.awt.Point");
$point->X=5000;
$point->Y=5000;
$x_draw_shape->setSize($size);
$x_draw_shape->setPosition($point);
$draw_page->add($x_draw_shape);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_shape_text = $draw_shape ;
$x_shape_props = $draw_shape;
//wrap text inside shape
$x_shape_props->setPropertyValue("TextContourFrame",true);
manipulateText($x_shape_text);
manipulateShape($x_draw_shape);
$x_draw_component->storeToURL( "file:///tmp/example02.sxd", array() );
$x_draw_component->dispose();
}
function manipulateText($x_text)
{
// simply set whole text as one string
$x_text->setString("He lay flat on the brown, pine-needled flor of the forest, "
."his chin on his folden arms, and high overhead the wind blew in the tops "
."of the pine trees.");
// create text cursor for selecting and formatting
$x_text_cursor=$x_text->createTextCursor();
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cursor_props=$x_text_cursor;
// use cursor to select "He lay" and apply bold italic
$x_text_cursor->gotoStart(false);
&nb
sp; $x_text_cursor->goRight(6,true);
// from CharacterProperties
$x_cursor_props->setPropertyValue("CharWeight",FontWeight_BOLD);
// add more text at the end of the text using insertString
$x_text_cursor->gotoEnd(false);
$x_text->insertString($x_text_cursor," 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
$x_text->insertString($x_text_cursor,"\n \"Is that the mill?\" he asked." , false);
}
function manipulateTable($x_cell_range)
{
$back_color_property_name = "";
$x_table_props = null;
// enter column titles and a cell value
$x_cell=$x_cell_range->getCellByPosition(0,0);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cell_text=$x_cell;
$x_cell_text->setString("Quotation");
$x_cell=$x_cell_range->getCellByPosition(1,0);
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_cell_text=$x_cell;
$x_cell_text->setString("Year");
$x_cell=$x_cell_range->getCellByPosition(1,1);
$x_cell->setValue(1940);
$x_selected_cells=$x_cell_range->getCellRangeByName("A1:B1");
$x_cell_props=$x_selected_cells;
// format table headers and table borders
//&nb
sp;we need to distinguish text and sheet tables:
// property name for cell colors is different in text and sheet cells
// we want to apply TableBorder to whole text table, but only to sheet cells with content
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_service_info=$x_cell_range;
if($x_service_info->supportsService("com.sun.star.sheet.Spreadsheet"))
{
$back_color_property_name="CellBackColor";
$x_selected_cells=$x_cell_range->getCellRangeByName("A1:B2");
$x_table_props=$x_selected_cells;
}
else if($x_service_info->supportsService("com.sun.star.text.TextTable"))
{
$back_color_property_name="BackColor";
$x_table_props=$x_cell_range;
}
// set cell background color
$x_cell_props->setPropertyValue($back_color_property_name, 0x99CCFF);
// set table borders
// create description for blue line, width 10
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$theLine=create_struct("com.sun.star.table.BorderLine");
$theLine->Color = 0x000099;
$theLine->OuterLineWidth = 10;
// apply line description to all border lines and make them valid
// set table borders
// create description for blue line, width 10
//structs are created using the auxiliary function create_struct
//it takes one parameter which is the full struct name as defined at the API docs
//struct attibutes' names ARE CASE SENSITIVE
//they must be typed according to the API
$bord=create_struct("com.sun.star.table.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;
$x_table_props->setPropertyValue("TableBorder",$bord);
$bord = $x_table_props->getPropertyValue("TableBorder");
$theLine = $bord->TopLine;
$col = $theLine->Color;
print("<BR>Color is: $col.<BR>");
}
function manipulateShape($x_shape)
{
//no need to query interfaces here ..
//just adjusting variable's name to meet
//the Programers Guide Example
$x_shape_props=$x_shape;
$x_shape_props->setPropertyValue("FillColor",0x99CCFF);
$x_shape_props->setPropertyValue("LineColor",0x000099);
$x_shape_props->setPropertyValue("RotateAngle",3000);
$x_shape_props->setPropertyValue("TextLeftDistance",0);
$x_shape_props->setPropertyValue("TextRightDistance",0);
$x_shape_props->setPropertyValue("TextUpperDistance",0);
$x_shape_props->setPropertyValue("TextLowerDistance",0);
}
function newDocComponent($doc_type)
{
$load_url="private:factory/".$doc_type;
$x_component_loader=get_remote_xcomponent("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager","com.sun.star.frame.Desktop");
$load_props=array();
$x_component=$x_component_loader->loadComponentFromURL($load_url,"_blank",0,$load_props);
return $x_component;
}
?>
</BODY>
</HTML>