代码之家  ›  专栏  ›  技术社区  ›  ojreadmore

OpenOffice::ooDoc将段落中的文本格式化

  •  3
  • ojreadmore  · 技术社区  · 16 年前

    我有一个简单的任务,添加一个段落,其中包含一些格式化的文本。我想不出如何使这篇文章风格化。

    示例输出:John Smith 200大街 单一的

    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(元素[,样式]) 更改现有元素的样式。我必须先添加文本才能设置其样式吗?

    1 回复  |  直到 16 年前
        1
  •  3
  •   Inshallah    16 年前

    请看 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;