views:

226

answers:

1

I have a simple task of adding a paragraph that has some formatted text within it. I cannot figure out how to stylize the text.

Example output: John Smith 200 Main Street single

my $doc = odfDocument(file=> 'outputfile.odt',create=> 'text');
$doc->appendParagraph(text => "John Smith 200 Main Street single", style => "optionalParagraphStyle");
$doc->save;

I've been reading the documentation on CPAN http://search.cpan.org/~jmgdoc/OpenOffice-OODoc/ I see that I can use textStyle(element [, style]) to change the style of an existing element. Do I have to first add text in order to style it?

+2  A: 

Please see extendText() and setSpan() in the Documentation.

Here is an example that does what you want:

use OpenOffice::OODoc;
my $doc = odfDocument(file=> 'outputfile.odt',create=> 'text');
$doc->createStyle(
    "strong",
    family     => "text",
    properties => { "fo:font-weight"  => "bold" }
    );
$doc->createStyle(
    "em",
    family     => "text",
    properties => { "fo:font-style"  => "italic" }
    );

my $p = $doc->appendParagraph(text => "", style => "optionalParagraphStyle");
$doc->extendText($p, "John Smith");
$doc->extendText($p, " 200 Main Street", "strong");
$doc->extendText($p, " single", "em");

my $p = $doc->appendParagraph(text => "John Smith 200 Main Street single", style => "optionalParagraphStyle");
$doc->setSpan($p, "200 Main Street", "strong");
$doc->setSpan($p, "single", "em");

$doc->save;
Inshallah
This makes perfect sense when I see it written this way. Many thanks.
ojreadmore
Do you know of a resource that lists all available properties, such as fo:font-weight and fo:font-style?
ojreadmore
Have a look at the specification: http://docs.oasis-open.org/office/v1.1/OS/OpenDocument-v1.1-html/OpenDocument-v1.1.html
Inshallah