Thread

Posted on Fri Sep 15 15:49:11 2006 by littleslinux
new version to create empty odt file and modify style properties
Thanks for jmgdoc jeunice and all other guys on this forum, after study all your guys suggestion and google, I made a new version, it works but still need improvement The complete source code as:
#!/usr/bin/perl use lib "/home/littles/.perl/module/lib/perl5/site_perl/5.8.5"; use OpenOffice::OODoc; use OpenOffice::OODoc::File; use OpenOffice::OODoc::Styles; use OpenOffice::OODoc::Document; use OpenOffice::OODoc::Text; use OpenOffice::OODoc::Manifest; use OpenOffice::OODoc::Meta; use OpenOffice::OODoc::Image; use OpenOffice::OODoc::XPath; use Data::Dumper; my $document; my $content; my @styles; my $style; # I seperate document, content and style to make code more readable $document = ooDocument ( file => 'test.odt', create => 'text' ) or print " can not create $filename! \n"; # connect a style-focused document interface $style = ooDocument ( file => 'test.odt', member => 'styles', ) or print " can not open test.odt styles ! \n"; # before we use font, declare it $style->importFontDeclaration ( '<style:font-face '.'style:name="Comic Sans MS" '.'svg:font-family="Comic Sans MS"/>' ); $style->importFontDeclaration ( '<style:font-face '. 'style:name="Nimbus Roman No9 L" '. 'svg:font-family="Nimbus Roman No9 L +" />' ); $style->importFontDeclaration ( '<style:font-face '.'style:name="Courier" '.'svg:font-family="Courier" />' ); # chinese support $style->importFontDeclaration ( '<style:font-face '.'style:name="AR PL SungtiL GB" '.'svg:font-family="AR PL SungtiL GB" />' ); # update 'Text body' paragraph and text properties $style->updateStyle ( "Text_20_body", family=>'paragraph', parent => "Standard", properties => { 'area' => 'paragraph', 'fo:margin-left'=>"0in", 'fo:margin-right'=>"0in", 'fo:margin-top'=>"0.05in", 'fo:margin-bottom'=>"0.05in", 'fo:line-height'=>"0.1575in", 'fo:text-align'=>"justify", 'fo:text-indent'=>"0in", 'style:justify-single-word'=>"true", 'style:auto-text-indent'=>"false", 'style:shadow' => "none" } ) or print " can createStyle ! $LINE\n"; $style->updateStyle ( "Text_20_body", properties => { 'area' => 'text', 'fo:font-size' => "10pt", 'style:font-name' => 'Nimbus Roman No9 L', 'fo:font-size-asian' => "10pt", 'style:font-name-asian' => 'AR PL SungtiL GB' } ) ############################################################ # Quotations ############################################################ $style->createStyle ( 'Quotations', family=>'paragraph', parent => "Standard", properties => { 'area' => 'paragraph', 'fo:margin-left'=>"0.2in", 'fo:margin-right'=>"0.2in", 'fo:margin-top'=>"0in", 'fo:margin-bottom'=>"0in", 'fo:padding'=>"0.0201in", 'fo:border'=>"0.0008in solid #000000", 'fo:line-height'=>"0.12in", 'fo:text-align'=>"justify", 'fo:text-indent'=>"0in", 'style:auto-text-indent'=>"false", 'style:auto-text-indent'=>"false", 'fo:background-color'=>"#e6e6e6", 'style:join-border'=>"true", # merge border 'style:justify-single-word'=>"true", 'style:shadow' => "none" } ) or print " can createStyle ! \n"; $style->updateStyle ( 'Quotations', properties => { 'area' => 'text', 'fo:font-size' => "9pt", 'style:font-name' => 'Courier', 'fo:font-size-asian' => "9pt", 'style:font-name-asian' => 'AR PL SungtiL GB' } ) or print " can updateStyle $LINE!\n" ; $style->save("test.odt"); ################################################################### # connect a content-focused document interface $content = ooDocument ( file => 'test.odt', member => 'content' ) or print " can not open test.odt content ! \n"; $content->appendParagraph ( text => 'Text body content Text body content Text body content Text body content Text body cont +ent Text body content Text body content Text body content Text body content Text body content Text + body content Text body content Text body content Text body content Text body content Text body co +ntent Text body content Text body content Text body content Text body content Text body content', style => 'Text body' ); $content->appendParagraph ( text => 'Quotations body content Quotations body content Quotations body content Quotations bod +y content Quotations body content Quotations body content Quotations body content Quotations body +content Quotations body content Quotations body content Quotations body content Quotations body co +ntent Quotations body content Quotations body content Quotations body content Quotations body cont +ent ', style => 'Quotations' ); $content->save("test.odt");
Inside source code, I update 'Text body' or 'Text_20_body', but for Quotations, I cant update it, I can't find it's display name, So I just create it myself, I mainly use this style for my source code in report. Need your suggestion, I will check OO specification to find Quotations' display name to improvement this little script file.
Direct Responses: 3042 | 3043 | Write a response
Posted on Fri Sep 15 17:31:16 2006 by jeunice in response to 3039
Re: new version to create empty odt file and modify style properties

Inside source code, I update 'Text body' or 'Text_20_body', but for Quotations, I cant update it, I can't find it's display name, So I just create it myself, I mainly use this style for my source code in report. Need your suggestion, I will check OO specification to find Quotations' display name to improvement this little script file.

Don't look too long, as it's not there. Quotations seems to be a "phantom" style--one that the OpenOffice code knows about, and will record it in the style component of documents that use it--but one that is not written to a document unless it is used.

With OpenOffice::OODoc, you are not using the OpenOffice code base and machinery to manipulate OpenOffice files. Rather, you are using Perl modules that operate directly on the ZIP archives and XML contents of ODF files. So not all the functionality you find in OpenOffice will be available. This happens to include default definitions for a number of styles.

Write a response
Posted on Fri Sep 15 17:46:15 2006 by jmgdoc in response to 3039
Re: new version to create empty odt file and modify style properties

I've several suggestions, the first is minor, the others more important.

First, the sequence of "use OpenOffice::OODoc::Xyz" is useless. As soon as the main module (OpenOffice::OODoc) is loaded, all the submodules are loaded, so you could remove 8 lines from "use OpenOffice::OODoc::File" to "use OpenOffice::OODoc::XPath".

Second: the really displayed name of the "Quotations" style through OpenOffice.org is out of our control, because "Quotations" is the name of a predefined style, and it's translated according to your local installation. You should try another style name; for example, "Kuotations" is displayed as is.

Third: In the appendParagraph() instruction, the style option is set with "Text body" (the display name), but "Text_20_body" (the internal name) should be used instead.

Last: In your script, the physical file (test.odt) is loaded separately through $content and $style. As a consequence, the last instruction
$content->save("test.odt")
overwrites the changes previously committed by
$style->save("test.odt")
The script works nevertheless, because the file is saved and reopened after every intermediate update, but it generates a lot of useless I/O traffic and XML parsing (remember that, with a complex document, ooDocument() is a very costly operation).
You should explicitly link the two instances in the beginning of the program this way:
$content = ooDocument ( file => "test.odt, member => "content", create => "text ); $style = ooDocument ( file => $content, member => "styles" );
Then you should call the save() method, at the very end, from either $content or $style (at your choice, but only once).
Direct Responses: 3088 | Write a response
Posted on Tue Sep 19 14:14:42 2006 by littleslinux in response to 3043
Re: new version to create empty odt file and modify style properties
thanks jeunice and jmgdoc. For Quotations, I have checked a styles.xml from a odt file create by OpenOffice, its name is 'Quotations', but no display-name avaiable. From jeunice, it seems OODoc can't control 'Quotations'. From jmgdoc, it seems can but must confirm correct name. Then how? I have updated my perl code, use document and style explicit link. Thanks again.
Write a response