[Solved] Accessing page header/footer of Writer docs

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

[Solved] Accessing page header/footer of Writer docs

Post by _savage »

Hello, is it possible to access the content (e.g. its paragraphs) of the header and footer of each page of a loaded document in Writer?

I found the HeaderFooterContent service but I’m not quite sure how to get from the loaded TextDocument to its page headers/footers…

I’ll keep poking and MRI’ing around, but would appreciate if somebody would be able to point me into the right direction…

Much thanks!
Jens
Last edited by _savage on Wed May 11, 2022 11:55 am, edited 2 times in total.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Programmatically accessing page header/footer of Writer

Post by JeJe »

Assuming there is a header its text is part of the page style.

Code: Select all

mri thiscomponent.stylefamilies.getbyname("PageStyles")
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Programmatically accessing page header/footer of Writer

Post by _savage »

JeJe wrote:Assuming there is a header its text is part of the page style.
Thanks JeJe! I think I found it…

So, the 'PageStyles' provides a StyleFamily service to access styles for 'Standard', 'First Page', 'Left Page', 'Right Page', … which each provide the PageProperties service, which in turn has the HeaderBla and FooterBla properties for these page styles. I think that’s what I was looking for :super:
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Accessing page header/footer of Writer docs

Post by _savage »

Hmm :? Is it possible that a header/foot is protected against modification?

I can iterate over the TextRange objects in the header’s/footer’s paragraph, but when I call setString() on any one range it has no effect? Interestingly, if I call getString() on the owning paragraph then that string contains the modification — it’s just not displayed in the document or saved (screenshot shows MRI editing the component):
soffice.jpg
soffice.jpg (12.17 KiB) Viewed 1855 times
Last edited by _savage on Thu May 12, 2022 11:51 am, edited 1 time in total.
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Accessing page header/footer of Writer docs

Post by JeJe »

Works for me,

Code: Select all

txt= thiscomponent.stylefamilies.getbyname("PageStyles").getbyindex(0).headertext
txt.setstring "whatever"
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Accessing page header/footer of Writer docs

Post by _savage »

JeJe wrote: Thu May 12, 2022 11:33 amWorks for me, […]
And when you save the file, has the header text changed?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Accessing page header/footer of Writer docs

Post by JeJe »

Yes the change is saved. Testing on LO on Windows 8
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Re: Accessing page header/footer of Writer docs

Post by _savage »

Which LO version are you on, and is that an ODT or DOCX? I wonder if I have a funky DOCX version…
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Accessing page header/footer of Writer docs

Post by JeJe »

7.2.2.2 Works for me saving in both.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply