I've been trying to process some XML files to create OpenOffice text documents and have been mostly successful so far. However, I would like to add page numbers in the footer, which should appear on all pages but the first couple of them where the title is supposed to appear.
So far, I think I've figured out that the way to go about achieving this is through master/layout pages but I have been unable to accomplish the required task. Of course, it would be easier to simply produce the basic document and then edit it through LibreOffice to get what I need but... you know how it is! I've got to know now!
So, the sample code below produces what I expect: An .odt document with the title on the first page without a page number in the footer and the text appears in the following pages with a page number in the footer. However, the problem seems to be that every page contains one paragraph only preceded by a page break.
I guess the problem lies somewhere in the way I connect master/layout pages to the paragraph styles... or something... but I couldn't find any concrete examples on line to help me fix my mistake(s). I would appreciate any pointers because this is almost driving me crazy.
Here's the sample code:
#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use Win32;
use Encode;
use OpenOffice::OODoc;
use Data::Dumper::AutoEncode;
ooLocalEncoding('utf-8');
my $curfolder = Win32::GetCwd();
my $outfolder = $curfolder . '\\out';
my $outfile = $outfolder . '\\test.odt';
my $doc = odfDocument(
file => $outfile,
create => 'text',
opendocument => 0,
);
my $styles = odfDocument(
container => $doc,
part => "styles",
);
my $centerfooter = $styles->createStyle(
'centerfooter',
family => "paragraph",
properties => {
"fo:margin-top" => "0.5cm",
"fo:text-align" => 'center',
},
replace => 1,
);
my $headerstyle = $styles->createStyle(
"header",
family => "paragraph",
parent => "Standard",
properties => {
"fo:margin-top" => "8cm",
"fo:text-align" => "center",
"fo:break-after" => "page",
},
replace => 1,
);
$styles->styleProperties(
$headerstyle,
-area => "text",
"fo:font-size" => "200%",
"fo:font-weight" => "bold",
);
$styles->setAttributes(
$headerstyle,
"master-page-name" => 'header',
);
my $regularstyle = $styles->createStyle(
"regular",
family => "paragraph",
parent => "Standard",
replace => 1,
);
$styles->setAttributes(
$regularstyle,
"master-page-name" => 'pagenumbers',
);
my $pagelayout = $styles->pageLayout("Standard");
my $titlepage = $styles->createMasterPage(
'titlepage',
layout => $pagelayout,
);
my $pnpage = $styles->createMasterPage(
'pagenumbers',
layout => $pagelayout,
);
my $pn = $styles->createParagraph( '', 'centerfooter' );
my $pg = $styles->textField( 'page-number', style => 'centerfooter' );
$styles->appendElement( $pn, $pg );
$styles->masterPageFooter( 'pagenumbers', $pn );
my $wordlist = $doc->appendParagraph(
text => '',
style => 'header',
);
$doc->extendText( $wordlist, uc 'Main Title', 'header' );
my $regular = $doc->appendParagraph(
text => 'Some text.',
style => 'regular',
);
$doc->appendParagraph(
text => 'More text.',
style => 'regular',
);
$doc->appendParagraph(
text => 'And some more.',
style => 'regular',
);
$doc->save;