[Solved] Add Header Footer using java code in OpenOffice

Discussions about using 3rd party extension with OpenOffice.org
Post Reply
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

[Solved] Add Header Footer using java code in OpenOffice

Post by Rushna »

Hi All,

I want to add header footer in current open document using Openoffice plugin.
there is XHeaderFooter class but I am not getting how it will work.

please help me out .. :(
Last edited by Rushna on Fri Sep 22, 2017 8:37 am, edited 1 time in total.
OpenOffice 4.3.1 on Windows 7
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Add Header Footer using java code in OpenOffice

Post by FJCC »

I don't know Java but I recorded this using the MRI extension. I hope it helps.

Code: Select all

import com.sun.star.beans.PropertyVetoException;
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import com.sun.star.uno.RuntimeException;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XStyleFamiliesSupplier xStyleFamiliesSupplier = UnoRuntime.queryInterface(
			XStyleFamiliesSupplier.class, oInitialTarget);
		XNameAccess xNameAccess = xStyleFamiliesSupplier.getStyleFamilies();
		
		XNameContainer xNameContainer = UnoRuntime.queryInterface(
			XNameContainer.class, xNameAccess.getByName("PageStyles"));
		
		XNameAccess xNameAccess2 = UnoRuntime.queryInterface(
			XNameAccess.class, xNameContainer);
		XStyle xStyle = UnoRuntime.queryInterface(
			XStyle.class, xNameAccess2.getByName("Standard"));
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xStyle);
		xPropSet.setPropertyValue("FooterIsOn", true);
		
	}
	catch (IllegalArgumentException e1)
	{
		// setPropertyValue
		e1.printStackTrace();
	}
	catch (PropertyVetoException e2)
	{
		// setPropertyValue
		e2.printStackTrace();
	}
	catch (WrappedTargetException e3)
	{
		// getByName, setPropertyValue
		e3.printStackTrace();
	}
	catch (UnknownPropertyException e4)
	{
		// setPropertyValue
		e4.printStackTrace();
	}
	catch (NoSuchElementException e5)
	{
		// getByName
		e5.printStackTrace();
	}
	catch (RuntimeException e6)
	{
		// getByName
		e6.printStackTrace();
	}
}
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Add Header Footer using java code in OpenOffice

Post by RoryOF »

Here is a note on how to turn off Headers/Footers in OpenOffice using Java. Turning them on should be a simple change to the code.
https://stackoverflow.com/questions/374 ... nd-footers
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: Add Header Footer using java code in OpenOffice

Post by Rushna »

I am looking for these two packages but I don't have idea how to implement.
Please suggest:

1) "com.sun.star.text.XHeaderFooter" and
2) "com.sun.star.text.XHeaderFooterPageStyle"
OpenOffice 4.3.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Add Header Footer using java code in OpenOffice

Post by RoryOF »

Rushna wrote:I am looking for these two packages but I don't have idea how to implement.
Please suggest:

1) "com.sun.star.text.XHeaderFooter" and
2) "com.sun.star.text.XHeaderFooterPageStyle"
https://www.openoffice.org/api/docs/com ... le-ix.html

Both now deprecated.

Your project - you must do the work.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
UnklDonald418
Volunteer
Posts: 1544
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Add Header Footer using java code in OpenOffice

Post by UnklDonald418 »

Have you look at Andrew Davison's book titled "Java LibreOffice Programming"?
http://fivedots.coe.psu.ac.th/~ad/jlop/
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: Add Header Footer using java code in OpenOffice

Post by Rushna »

Added Header/Footer properly but Right/Left Header/Footer still not working.
I am using "HeaderTextRight" and "HeaderTextLeft".


please help...
OpenOffice 4.3.1 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Add Header Footer using java code in OpenOffice

Post by Villeroy »

First you have to do it manually so you understand the concept of page styles. Then you may be able to do it programatically or drop this approach and work with templates.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: Add Header Footer using java code in OpenOffice

Post by Rushna »

Hi..,
I am using this code but i am getting error in HeaderText.insertString(xTextCursor, s1, true); this line.look into this code and
please help me out.

TabStop[] tabStop=new TabStop[1];
tabStop[0]=new TabStop();
tabStop[0].Position=5001;
tabStop[0].Alignment=TabAlign.CENTER;


XTextCursor xTextCursor = (XTextCursor)UnoRuntime.queryInterface(XTextCursor.class,tabStop[0]);

XText HeaderText = (XText) UnoRuntime.queryInterface(XText.class, xStyleProps.getPropertyValue("HeaderText"));
HeaderText.insertString(xTextCursor, s1, true);
OpenOffice 4.3.1 on Windows 7
User avatar
robleyd
Moderator
Posts: 5055
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Add Header Footer using java code in OpenOffice

Post by robleyd »

The actual error will help anyone trying to work out the problem.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.1.2; SlackBuild for 24.2.1 by Eric Hameleers
Post Reply