3

我有一个简单的任务,即添加一个包含一些格式化文本的段落。我无法弄清楚如何对文本进行风格化。

示例输出:John Smith 200 Main Street 单曲

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

我一直在阅读有关 CPAN 的文档http://search.cpan.org/~jmgdoc/OpenOffice-OODoc/ 我看到我可以使用textStyle(element [, style])来更改现有元素的样式。我必须先添加文本才能对其进行样式设置吗?

4

1 回答 1

3

请参阅文档中的extendText()setSpan()

这是一个可以满足您要求的示例:

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;
于 2009-07-26T02:00:50.207 回答