About News Downloads Installation Documentation FAQ Links Contact

Advanced Example

In this example, a new Writer document is created, then:

Click here to see the PDF document created by this example.


PHP Code

<?
    
/* *
    * This example creates a PDF file listing the functions and constants
    * loaded at the current running PHP5 module/program
    *
    * Details: Page Numbering is ON
    */
    
    
$data=date("r");
    echo
" <BR>Started at: $data<BR>";
    
flush(NULL);

    
//get extensions loaded
    
$php_extensions=get_loaded_extensions();
    
//for each extension, get it's functions
    
$extensions_functions=array();
    foreach(
$php_extensions as $extension)
    {
        
$extensions_functions["$extension"]=get_extension_funcs($extension);
    }
    
//get the defined constants
    
$defined_constants=get_defined_constants();
    
    
// create new writer document and get text
    
$x_writer_component=newDocComponent("swriter");
    
//no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_text_document=$x_writer_component;
    
$x_text=$x_text_document->getText();
    
    
//no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    // get internal service factory of the document
    
$x_writer_factory=$x_writer_component;
    
    
    
// create text cursor for selecting and formatting
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_text_cursor=$x_text->createTextCursor();
    
$x_cursor_props=$x_text_cursor;
    
    
//get text cursor's XParagraphCursor
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_para_cursor=$x_text_cursor;
    
//get paragraph cursor's properties
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_para_props=$x_text_cursor;
        
    
//create the NumberingRules service for all functions and access it's XIndexAccess Interface
    
$x_numbering_rules=$x_writer_factory->createInstance("com.sun.star.text.NumberingRules");  
    
    
//no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_index_replace=$x_numbering_rules;
    
    
//$numbering_level0=$x_index_replace->getByIndex(0);
    //var_dump($numbering_level0);
        
    //for each PHP5 extension loaded
    
foreach($php_extensions as $module_name)
    {
        
//create a Topic for every module and it's functions
        
insert_module_name($module_name,$x_text);
    
        
        foreach(
$extensions_functions["$module_name"] as $function)
        {
            
insert_function_description($function, $x_text,$x_writer_factory);
        }
        
        
//move to the end of the document
        
$x_text_cursor->gotoEnd(false);
        
//insert 3 line breaks
        
        
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_LINE_BREAK,false);
        
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_LINE_BREAK,false);
        
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_LINE_BREAK,false);

    }

    
    
//turn on page numbering
    
insert_page_numbers($x_text,$x_writer_component,$x_writer_factory);
        
    
//display a table with some of the defined constants
    
insert_constants_table($x_text,$x_writer_factory);
        
    
//create a Footnote
    
create_footnotes($x_text,$x_writer_component);
    
    
//insert an index
    
insert_index($x_text,$x_writer_factory);
    
    
//store the report as PDF (/tmp/example03.pdf)
    
store_as_pdf($x_writer_component,"file:///tmp/example03.pdf");

    
$x_writer_component->dispose();
    $data=date("r");
    echo
" <BR>Finished at: $data<BR>";
    
flush(NULL);

?>
<?
function store_as_pdf($x_component,$store_url)
{
    
//no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_storable=$x_component;
    
$store_props=array();
    
//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
    //"com.sun.star.beans.PropertyValue" in this case
    
$store_props[0]=create_struct("com.sun.star.beans.PropertyValue");
    
    
//struct attibutes' names ARE CASE SENSITIVE    
    //they must be typed according to the API
    
$store_props[0]->Name="FilterName";
    
$store_props[0]->Value = "writer_pdf_Export";
    
$x_storable->storeToURL($store_url, $store_props);  
}

function
insert_page_numbers($x_text,$x_writer_component,$x_writer_factory)
{
    
// 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;
    
$current_page_style_name=$x_cursor_props->getPropertyValue("PageStyleName");
    
$styles=$x_writer_component;
    
$x_named_styles=$styles->getStyleFamilies();
    
    
$page_styles=$x_named_styles->getByName("PageStyles");
    
$current_style=$page_styles->getByName($current_page_style_name);
    
    
//get current style properties
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$props=$current_style;
    
    
//headers or footers must be switched on
    
$props->setPropertyValue("FooterIsOn",true);
    
    
//get an instance of the service com.sun.star.text.Text
    //which contain the Footer Text of the Layout
    
$footer_text=$props->getPropertyValue("FooterText");
    
//$footer_text=$x_writer_factory->createInstance("com.sun.star.text.Text");
    
$footer_x_text=$footer_text;
    
    
//set an instance of the service com.sun.star.text.textfield.PageNumber
    
$page_number=$x_writer_factory->createInstance("com.sun.star.text.TextField.PageNumber");
    
//get it's XTextField interface
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_page_number=$page_number;
    
//get XTextField properties
    
$textfield_props=$x_page_number;
    
//set the "Numbering Type" to Arabic (1,2,3,4, etc).
    
$textfield_props->setPropertyValue("NumberingType",NumberingType_ARABIC);
    
    
//insert it into the Footer
    
$footer_x_text->insertTextContent($footer_x_text->getEnd(),$x_page_number,false);
    
    
$footer_x_text_cursor=$footer_x_text->createTextCursor();
    
$footer_x_text_cursor->gotoEnd(false);
    
$footer_x_text_cursor->gotoStart(true);
    
//get footer properties
    
$footer_props=$footer_x_text_cursor;
    
    
//set it's justification to Right
    
$footer_props->setPropertyValue("ParaAdjust",ParagraphAdjust_RIGHT);

    
    
    
}

function
insert_index($x_text,$x_writer_factory)
{
    
// 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;
    
    
//insert a paragraph
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
    
    
//get text cursor's XParagraphCursor
    
$x_para_cursor=$x_text_cursor;
    
//get paragraph cursor's properties access
    
$x_para_props=$x_text_cursor;
    
//move to the end of the paragraph
    
$x_para_cursor->gotoEndOfParagraph(false);
    
//insert a Page Break after the paragraph
    
$x_para_props->setPropertyValue("BreakType",BreakType_PAGE_AFTER);
    
    
//create an index
    
$index=$x_writer_factory->createInstance("com.sun.star.text.ContentIndex");
    
$x_index_content=$index;
    
$x_text_cursor->gotoStart(false);
    
$x_text->insertTextContent($x_text_cursor,$x_index_content,false);
    
    
//get the XDocumentIndex Interface from the service
    
$x_document_index=$index;
    
    
//get it's properties access interface
    
$x_document_index_props=$x_document_index;
    
    
//set CreateFromOutline to TRUE
    
$x_document_index_props->setPropertyValue("CreateFromOutline",true);
    
//update index content
    
$x_document_index->update();
}


function
insert_module_name($module_name,$x_text)
{

    global
$x_numbering_rules;

    
// create text cursor for selecting and formatting
    
$x_text_cursor=$x_text->createTextCursor();
    
//get this cursor properties access
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_cursor_props=$x_text_cursor;
    
    
//get text cursor's XParagraphCursor
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_para_cursor=$x_text_cursor;
    
    
//get paragraph cursor's properties access
    
$x_para_props=$x_text_cursor;


    
//move to the End of the document
    
$x_text_cursor->gotoEnd(false);
        
    
//insert new paragraph    
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
        
    
//insert text
    
$x_text->insertString($x_text_cursor,"Module $module_name",false);
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
        
    
//select the recently added paragraph
    
$x_para_cursor->gotoPreviousParagraph(false);
    
$x_para_cursor->gotoEndOfParagraph(true);
    
    
$x_para_props->setPropertyValue("ParaStyleName","Heading 1");
    
//set selected text to be bold
    
$x_para_props->setPropertyValue("CharWeight",FontWeight_BOLD);
    
//set selected text posture to NONE
    
$x_para_props->setPropertyValue("CharPosture",FontSlant_NONE);    
    
/* For more formatting properties, see the documention of
     * the following services:
     * com.sun.star.style.CharacterProperties,
     * com.sun.star.style.CharacterPropertiesAsian and
     * com.sun.star.style.CharacterPropertiesComplex.
     **/
        
    //set the Left Margin to 0 centimeters
    
$x_para_props->setPropertyValue("ParaLeftMargin",0);
    
//Back Color = Black
    
$x_para_props->setPropertyValue("ParaBackColor",0x000000);
    
//Font Color = white
    
$x_para_props->setPropertyValue("CharColor",0xFFFFFF);
    
//Font Size = 20
    
$x_para_props->setPropertyValue("CharHeight",20);
    
//Font Name = Tahoma, Arial, Times New Roman
    
$x_para_props->setPropertyValue("CharFontName","Tahoma,Arial,Times New Roman");
    
    
//using numbering (Arabic)
    
$x_para_props->setPropertyValue("NumberingRules",$x_numbering_rules);        
    
$x_para_props->setPropertyValue("NumberingLevel",1);    
    

    
    
}

function
insert_function_description($function_name, $x_text,$x_factory)
{

    
// create text cursor for selecting and formatting
    
$x_text_cursor=$x_text->createTextCursor();
    
//get this cursor properties
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_cursor_props=$x_text_cursor;
    
    
//get text cursor's XParagraphCursor
    
$x_para_cursor=$x_text_cursor;
    
    
//get paragraph cursor's properties
    
$x_para_props=$x_text_cursor;
    
    
//"de-select" the text and move to the end of the document
    
$x_text_cursor->gotoEnd(false);
    
//insert new paragraph    
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
    
//insert Function Name
    
$x_text->insertString($x_text_cursor,"Function $function_name",false);
    
//select the recently added function name
    
$x_para_cursor->gotoStartOfParagraph(true);
    
//set the Left Margin to 1 centimeter
    
$x_para_props->setPropertyValue("ParaLeftMargin",1000);
    
//set it's properties so that text posture is Italic
    
$x_para_props->setPropertyValue("CharPosture",FontSlant_ITALIC);
    
    
//already using numbering. Set numbering level to 1 (bullets)
    
$x_para_props->setPropertyValue("NumberingLevel",1);    
    
    
//"de-select" the text and move to the end of the document
    
$x_text_cursor->gotoEnd(false);
    
    
}

/**
* this function create a Table with all defined constants names and values
*/
function insert_constants_table($x_text,$x_factory)
{
    
//get defined constants
    
$defined_constants=get_defined_constants();
    
    
//get the "numbering service" globally defined
    
global $x_numbering_rules;        
    
    
// create text cursor for selecting and formatting
    
$x_text_cursor=$x_text->createTextCursor();
    
//get this cursor properties
    
$x_cursor_props=$x_text_cursor;
    
    
//"de-select" any possibly defined text and move to the end
    //of the document
    
$x_text_cursor->gotoEnd(false);

    
//insert new paragraph    
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
    
    
//insert a "Chapter Title"
    
$x_text->insertString($x_text_cursor,"Defined Constants",false);
    
    
//insert a new paragraph, in order to apply the "chapter style"
    //only to the "Defined Constants" text
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_PARAGRAPH_BREAK,false);
    
//insert a line breaks before the table
    
$x_text->insertControlCharacter($x_text_cursor,ControlCharacter_LINE_BREAK,false);
    
    
    
//get text cursor's XParagraphCursor
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_para_cursor=$x_text_cursor;
    
    
//get paragraph cursor's properties access
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_para_props=$x_text_cursor;
    
//move to the End of the document
    
$x_text_cursor->gotoEnd(false);
        
    
//select the recently added "Defined Constants" text
    
$x_para_cursor->gotoPreviousParagraph(false);
    
$x_para_cursor->gotoEndOfParagraph(true);
    
$x_para_props->setPropertyValue("ParaStyleName","Heading 1");
    
//set selected text to be bold
    
$x_para_props->setPropertyValue("CharWeight",FontWeight_BOLD);
    
//set selected text posture to NONE
    
$x_para_props->setPropertyValue("CharPosture",FontSlant_NONE);    
    
//set the Left Margin to 0 centimeters
    
$x_para_props->setPropertyValue("ParaLeftMargin",0);
    
//Back Color = Black
    
$x_para_props->setPropertyValue("ParaBackColor",0x000000);
    
//Font Color = white
    
$x_para_props->setPropertyValue("CharColor",0xFFFFFF);
    
//Font Size = 20
    
$x_para_props->setPropertyValue("CharHeight",20);
    
//Font Name = Tahoma, Arial, Times New Roman
    
$x_para_props->setPropertyValue("CharFontName","Tahoma,Arial,Times New Roman");
    
//insert a Page Break before this paragraph
    
$x_para_props->setPropertyValue("BreakType",BreakType_PAGE_BEFORE);
    
//using numbering (Arabic)
    
$x_para_props->setPropertyValue("NumberingRules",$x_numbering_rules);        
    
    
//
    //TABLE
    // create a new TextTable using the components factory
    
$table=$x_factory->createInstance("com.sun.star.text.TextTable");
    
    
//get the XTextTable interface from the service
    //no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_table=$table;
    
//set lines to count($function_arguments)+1 and columns to 3
    
$x_table->initialize((count($defined_constants)/10)+2,2);
    
    
//no need to query interfaces here ..
    //just adjusting variable's name to meet
    //the Programers Guide Example
    
$x_text_content_table=$table;
    
    
//insert the table into the document (at the END)
    
$x_text->insertTextContent($x_text->getEnd(),$x_text_content_table,false);
    
    
//create a new XCellRange (to manipulate table's cells)
    
$x_cell_range=$table;
    
    
//set first cell to "Constant Name"
    
$x_cell=$x_cell_range->getCellByPosition(0,0);
    
$x_cell_text=$x_cell;
    
$x_cell_text->setString("Constant Name");
        
    
//set second cell to "Value"
    
$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("Value");
    
//select first row
    
$x_selected_cells=$x_cell_range->getCellRangeByName("A1:B1");
    
//get selected cells properties access
    
$x_cells_props=$x_selected_cells;        
    
    
//set selected cells style to:
    //Back Color = Black
    
$x_cells_props->setPropertyValue("BackColor",0x000000);
    
//Font Color = white
    
$x_cells_props->setPropertyValue("CharColor",0xFFFFFF);
    
//Font Size = 20
    
$x_cells_props->setPropertyValue("CharHeight",16);
    
//Font Name = Arial, Times New Roman
    
$x_cells_props->setPropertyValue("CharFontName","Arial,Times New Roman");
    
    
//fill the table with constants' names and values
    
$row=1;
    
$keys=array_keys($defined_constants);
    for(
$i=0; $i<(count($defined_constants)/10);$i++)
    {
        
$key=$keys[$i];
        
//if this is an  odd row, define it's background color to blue or
        //gray otherwise
        
$back_color=($row%2==0)?0x99CCFF:0xEEEEEE;
        
        
//set the first column value to $key (constant name)
        
$x_cell=$x_cell_range->getCellByPosition(0,$row);
        
$x_cell_text=$x_cell;
        
$x_cell_text->setString("$key");
        
        
//get cell Text Cursor
        
$x_cell_text_cursor=$x_cell_text->createTextCursor();
        
$x_cell_text_cursor->gotoEnd(true);
        
//get text properties interface
        
$x_cell_props=$x_cell_text_cursor;
        
//set Font to Bookman, Bold and Italic
        
$x_cell_props->setPropertyValue("CharFontName","tahoma,times new roman,arial");
        
$x_cell_props->setPropertyValue("CharWeight",FontWeight_BOLD);
        
$x_cell_props->setPropertyValue("CharPosture",FontSlant_ITALIC);
        
        
//get first column cell properties access
        
$x_cell_props=$x_cell;
        
//set the background color
        
$x_cell_props->setPropertyValue("BackColor",$back_color);
        
        
        
        
//SECOND COLUMN
        //set the second column value to $value (constant value)
        
$x_cell=$x_cell_range->getCellByPosition(1,$row);
        
$x_cell_text=$x_cell;
        
$value=$defined_constants["$key"];
        
$x_cell_text->setString("$value");
        
        
//get second column Text Cursor
        
$x_cell_text_cursor=$x_cell_text->createTextCursor();
        
$x_cell_text_cursor->gotoEnd(true);
        
        
//get constant value's text properties interface
        
$x_cell_props=$x_cell_text_cursor;
        
//set Font to Arial
        
$x_cell_props->setPropertyValue("CharFontName","Arial");
        
//set justification to Right
        
$x_cell_props->setPropertyValue("ParaAdjust",ParagraphAdjust_RIGHT);
        
        
//get second column cell properties access
        
$x_cell_props=$x_cell;
        
//set the background color
        
$x_cell_props->setPropertyValue("BackColor",$back_color);
        
        
//increment row counter
        
$row=$row+1;
    }
//end foreach
    
    
    // set table borders
    // create description for black line, width 50
    //structs must be created using the auxiliary function
    //create_struct
    
$theLine = create_struct("com.sun.star.table.BorderLine");
    
$theLine->Color = 0x000000;
    
$theLine->OuterLineWidth = 15;
    
// apply line description to all border lines and make them valid
    //structs must be created using the auxiliary function
    //create_struct
    
$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;

    
//get the table's properties access interface
    
$x_table_props=$x_cell_range;
    
//set the property
    
$x_table_props->setPropertyValue("TableBorder",$bord);
        
}


function
create_footnotes($x_text,$x_writer_component)
{

    
// get internal service factory of the document
    
$x_writer_factory=$x_writer_component;
    
    
// create text cursor for selecting and formatting
    
$x_text_cursor=$x_text->createTextCursor();
    
$x_cursor_props=$x_text_cursor;
    
    
// Create a new footnote from the document factory and get it's XFootnote interface
    
$x_footnote=$x_writer_factory->createInstance("com.sun.star.text.Footnote");    
    
//set the footnote label to 'Description'
    
$x_footnote->setLabel("Footnote");
    
        
//get the Footnote text content reference
    
$x_footnote_text_content=$x_footnote;
        
    
//insert the Footnote into the Document
    
$x_text->insertTextContent($x_text_cursor,$x_footnote_text_content,false);
    
    
    
//get the XSimpleText interface from the footnote
    
$x_footnote_simple_text=$x_footnote;
    
    
//create TextCursor for the Footnote
    
$x_footnote_cursor=$x_footnote_simple_text->createTextCursor();
    
    
// set text as Footnote
    
$x_footnote_simple_text->insertString($x_footnote_cursor,"This example creates a PDF file listing functions and constants loaded at the current running PHP5 module/program",false);
    
}

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;
}
?>

SourceForge.net Logo