You are viewing a plain text version of this content. The canonical link for it is here.
Posted to odf-users@incubator.apache.org by "Joseph D. Wagner" <jo...@josephdwagner.info> on 2018/01/06 10:49:47 UTC

Add Page X of Y Footer

I wrote this code:

         // Adds Page X of Y to the footer
         Font fontsmaller = new Font("Arial", 
StyleTypeDefinitions.FontStyle.REGULAR, 8);
         Footer footer = doc.getFooter();
         Section footersection = footer.appendSection("Page Numbers");
         Paragraph footerparagraph = 
footersection.addParagraph("Page\u00A0");
         footerparagraph.setFont(fontsmaller); // Doesn't work here either
Fields.createCurrentPageNumberField(footerparagraph.getOdfElement());
         footerparagraph.appendTextContent("\u00A0of\u00A0");
Fields.createPageCountField(footerparagraph.getOdfElement());
         footerparagraph.setFont(fontsmaller); // Doesn't work here either

It seems to work EXCEPT the part of changing the font size.  In Word, 
the font ends up being the default size of 12.  (Works as expected in 
LibreOffice.)

I also wrote this variant, just in case setFont needs to be executed in 
some kind of order:

         // Adds Page X of Y to the footer
         Font fontsmaller = new Font("Arial", 
StyleTypeDefinitions.FontStyle.REGULAR, 8);
         Footer footer = doc.getFooter();
         Section footersection = footer.appendSection("Page Numbers");
         Paragraph footerparagraph = footersection.addParagraph("");
         footerparagraph.setFont(fontsmaller); // Doesn't work here either
         footerparagraph.appendTextContent("Page\u00A0");
Fields.createCurrentPageNumberField(footerparagraph.getOdfElement());
         footerparagraph.appendTextContent("\u00A0of\u00A0");
Fields.createPageCountField(footerparagraph.getOdfElement());
         footerparagraph.setFont(fontsmaller); // Doesn't work here

Here's a counterexample of when it works in both LiberOffice and Word, 
but only for the main body.

         // Special handling for first line
         Paragraph p = doc.getParagraphByIndex(0, false);
         p.setFont(font);
         infile.readLine();
         p.appendTextContent(infile.readLine());

         // Loops over all other lines
         String inline = new String();
         while ((inline = infile.readLine()) != null) {
             p.appendTextContent("\r\n" + inline.replaceAll("\n", "\r\n"));
         }
         infile.close();

Notice how I have to keep appending to an existing paragraph. Once I try 
to add a new paragraph, setFont doesn't set the size correctly, in Word.

Any help with this would be appreciated.

Joseph D. Wagner

P.S.  I had to add a non-breaking space to get it to correct add "Page X 
of Y".  Otherwise, in Word, it would say "Page Xof Y"


Re: Add Page X of Y Footer

Posted by "Joseph D. Wagner" <jo...@josephdwagner.info>.
Interesting follow-up.  It works when I set the font larger, like 16, 
but it doesn't work when I set the font smaller, like 10 or 8.

Joseph D. Wagner


On 01/06/2018 02:49 AM, Joseph D. Wagner wrote:
> I wrote this code:
>
>         // Adds Page X of Y to the footer
>         Font fontsmaller = new Font("Arial", 
> StyleTypeDefinitions.FontStyle.REGULAR, 8);
>         Footer footer = doc.getFooter();
>         Section footersection = footer.appendSection("Page Numbers");
>         Paragraph footerparagraph = 
> footersection.addParagraph("Page\u00A0");
>         footerparagraph.setFont(fontsmaller); // Doesn't work here either
> Fields.createCurrentPageNumberField(footerparagraph.getOdfElement());
>         footerparagraph.appendTextContent("\u00A0of\u00A0");
> Fields.createPageCountField(footerparagraph.getOdfElement());
>         footerparagraph.setFont(fontsmaller); // Doesn't work here either
>
> It seems to work EXCEPT the part of changing the font size.  In Word, 
> the font ends up being the default size of 12.  (Works as expected in 
> LibreOffice.)
>
> I also wrote this variant, just in case setFont needs to be executed 
> in some kind of order:
>
>         // Adds Page X of Y to the footer
>         Font fontsmaller = new Font("Arial", 
> StyleTypeDefinitions.FontStyle.REGULAR, 8);
>         Footer footer = doc.getFooter();
>         Section footersection = footer.appendSection("Page Numbers");
>         Paragraph footerparagraph = footersection.addParagraph("");
>         footerparagraph.setFont(fontsmaller); // Doesn't work here either
>         footerparagraph.appendTextContent("Page\u00A0");
> Fields.createCurrentPageNumberField(footerparagraph.getOdfElement());
>         footerparagraph.appendTextContent("\u00A0of\u00A0");
> Fields.createPageCountField(footerparagraph.getOdfElement());
>         footerparagraph.setFont(fontsmaller); // Doesn't work here
>
> Here's a counterexample of when it works in both LiberOffice and Word, 
> but only for the main body.
>
>         // Special handling for first line
>         Paragraph p = doc.getParagraphByIndex(0, false);
>         p.setFont(font);
>         infile.readLine();
>         p.appendTextContent(infile.readLine());
>
>         // Loops over all other lines
>         String inline = new String();
>         while ((inline = infile.readLine()) != null) {
>             p.appendTextContent("\r\n" + inline.replaceAll("\n", 
> "\r\n"));
>         }
>         infile.close();
>
> Notice how I have to keep appending to an existing paragraph. Once I 
> try to add a new paragraph, setFont doesn't set the size correctly, in 
> Word.
>
> Any help with this would be appreciated.
>
> Joseph D. Wagner
>
> P.S.  I had to add a non-breaking space to get it to correct add "Page 
> X of Y".  Otherwise, in Word, it would say "Page Xof Y"
>