Merge documents, but retain their respective headers/footers

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Merge documents, but retain their respective headers/footers

Post by groverblue »

Can someone help me figure out how to merge two documents, but retain their headers/footers?

Here is an example of what I'm doing:

Code: Select all

    public void append(XTextDocument xTextDocument1, XTextDocument xTextDocument2, String tempDoc) {

            importPageStyle(xTextDocument1, xTextDocument2);

            PropertyValue[] storeProps = createPropertyValueArray(

                    createPropertyValue("FilterName", new Any(Type.STRING, "writer8")),

                    createPropertyValue("CompressionMode", new Any(Type.STRING, "1")),

                    createPropertyValue("Pages", new Any(Type.STRING, "All")),

                    createPropertyValue("Overwrite", new Any(Type.BOOLEAN, Boolean.TRUE)));

            componentExport(xTextDocument2, storeProps, tempDoc);

            XDocumentInsertable xDocI = (XDocumentInsertable) getOOoUnoRuntimeQueryInterface(XDocumentInsertable.class, xTextDocument1.getText().createTextCursor());            

            xDocI.insertDocumentFromURL((new File(tempDoc)).toURI().toString(), new PropertyValue[0]);

    }



    public void importPageStyle(XTextDocument xTextDocument1, XTextDocument xTextDocument2) throws AgoDocumentException {

            XStyle document2Style = getPageStyle(xTextDocument2);

            String styleName = String.valueOf(uniqueStyleId(xTextDocument2));

            XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier) getOOoUnoRuntimeQueryInterface(XStyleFamiliesSupplier.class, xTextDocument1);

            XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();

            XNameContainer xPageStyles = (XNameContainer) getOOoUnoRuntimeQueryInterface(XNameContainer.class, xStyleFamilies.getByName("PageStyles"));

            xPageStyles.insertByName(styleName, style);

            XStyle document1Style = (XStyle) getOOoUnoRuntimeQueryInterface(XStyle.class, xPageStyles.getByName("Standard"));

            XPropertySet xStyleProps = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, xStyle);

            xStyleProps.setPropertyValue("FollowStyle", styleName);

    }



    public XStyle getPageStyle(XTextDocument document) {

            XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier) getOOoUnoRuntimeQueryInterface(XStyleFamiliesSupplier.class, document);

            XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();

            XNameContainer xPageStyles = (XNameContainer) getOOoUnoRuntimeQueryInterface(XNameContainer.class, xStyleFamilies.getByName("PageStyles"));

            XStyle xStyle = (XStyle) getOOoUnoRuntimeQueryInterface(XStyle.class, xPageStyles.getByName("Standard"));

            return xStyle;

    }
I'm unable to get past xPageStyles.insertByName(styleName, style), getting a com.sun.star.lang.IllegalArgumentException:

Code: Select all

com.sun.star.lang.IllegalArgumentException: 

        at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:177)

        at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:143)

        at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:335)

        at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:304)

        at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:91)

        at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:639)

        at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:151)

        at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:133)

        at $Proxy30.insertByName(Unknown Source)
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

FWIW, here's some Basic code.

Doc1 is done using page style "Default" but also contains another page style which is an exact copy of "Default" named "new". Doc2 has text in the header different from the text in Doc1's header. I didn't try to mess with fields in the headers.

Code: Select all

Sub Main
Doc1URL = ConvertToUrl("C:\users\cat\documents\Doc 1.odt")
Doc2URL = ConvertToUrl("C:\users\cat\documents\Doc 2.odt")
MergeDocs(Doc1URL,Doc2URL)
End Sub

Sub MergeDocs(First,Second)
oDoc2 = StarDesktop.loadComponentFromURL(Second,"_blank",0,Array())
ps = oDoc2.getStyleFamilies().getByName("PageStyles").getByName("Default")
HT = ps.HeaderText.String
oDoc2.dispose()
oDoc1 = StarDesktop.loadComponentFromURL(First,"_blank",0,Array())
oVC = oDoc1.CurrentController.getViewCursor
oVC.gotoEnd(false)
ParaBreak = com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK
oDoc1.Text.insertControlCharacter(oVC,ParaBreak,false)
oVC.BreakType = 4
oVC.PageDescName = "new"
oTC = oDoc1.Text.createTextCursorByRange(oVC)
oTC.insertDocumentFromURL(Second,Array()) 'This inserts the doc's text but not its header.
ps = oDoc1.getStyleFamilies().getByName("PageStyles").getByName("new")
ps.HeaderText.String = HT
End Sub
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

Thanks for your reply, John. I tried your suggestion, and here is what I think is the correct code:

Code: Select all

String newPageStyleName = String.valueOf(Math.abs(document.hashcode());

XText xDocText = document.getText();

XTextCursor xOrigDocTextCursor = xDocText.createTextCursor();

xOrigDocTextCursor.gotoEnd(false);

XPropertySet xOrigDocTextCursorProp = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, xOrigDocTextCursor);

xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.PAGE_BEFORE);

xDocText.insertControlCharacter(xOrigDocTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);

xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.NONE);

XParagraphCursor xParaCursor = (XParagraphCursor) getOOoUnoRuntimeQueryInterface(XParagraphCursor.class, xOrigDocTextCursor);

XPropertySet paraProps = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, xParaCursor);

paraProps.setPropertyValue(ParagraphStyleProperties.PageDescName, newPageStyleName);
Though, I get an error on setPropertyValue. ParagraphStyleProperties.PageDescName is just a constant string for "PageDescName"

Code: Select all

com.sun.star.lang.IllegalArgumentException: 
        at com.sun.star.lib.uno.environments.remote.Job.remoteUnoRequestRaisedException(Job.java:177)
        at com.sun.star.lib.uno.environments.remote.Job.execute(Job.java:143)
        at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:335)
        at com.sun.star.lib.uno.environments.remote.JobQueue.enter(JobQueue.java:304)
        at com.sun.star.lib.uno.environments.remote.JavaThreadPool.enter(JavaThreadPool.java:91)
        at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge.sendRequest(java_remote_bridge.java:639)
        at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.request(ProxyFactory.java:151)
        at com.sun.star.lib.uno.bridges.java_remote.ProxyFactory$Handler.invoke(ProxyFactory.java:133)
        at $Proxy20.setPropertyValue(Unknown Source)
The code looks correct, but it obviously isn't. Do you see anything that stands out as wrong?
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

Too late for serious thought but I'll get back tomorrow.

I did note that you appear to only use a text cursor and when I change my code to do so it works fine so that is not an issue. Bet you knew that!

FWIW, the text cursor should be at the start of the document by default. Can you do, "paraProps.setPropertyValue(ParagraphStyleProperties.PageDescName, newPageStyleName);" there so Doc1 has a new page style?

You couldn't buy a cup of coffee for the value of what I remember about programing in Java.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

Given my nonexistent skills with Java it looks to me as though you may have these two lines reversed.

Code: Select all

xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.PAGE_BEFORE);

xDocText.insertControlCharacter(xOrigDocTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
We first want to insert a paragraph break which will move the text cursor to a new line. Then we want to change the break type so a new page is generated and the cursor is at the top of that page.

Would this line effectively cancel what we just did above.

Code: Select all

xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.NONE);

Code: Select all

String newPageStyleName = String.valueOf(Math.abs(document.hashcode());
I'm really lost with this one. It looks to me as though you are creating a String as opposed to actually creating a new page style. If it were me I would fiscally create the new page style in Doc1 and try applying it. Then I would worry about using code to create the new page style.
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

I made the suggested changes, but I have a question about creating a new page style and using. I notice that you reference a page style called "new" via PageDescName, but I don't see where you create an actual page style object prior?

oVC.PageDescName = "new"

At what point does a "new" page style become created and available for usage?
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

The macro does not create the"new" page style. I created it manually in Doc1.

I don't know how to create a new page style but was able to record a macro to do so based on the page style a the current view cursor location.

Code: Select all

sub NewPageStyle 
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Param"
args1(0).Value = "new"
args1(1).Name = "Family"
args1(1).Value = 8
dispatcher.executeDispatch(document, ".uno:StyleNewByExample", "", 0, args1())
end sub
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

I mostly got it working, except for the copying of the actually header/footer text. Whenever I read in a new document, the header and footer text is always applied to the first page (or Default page style), instead of the newly created page style. So, for instance, let's say I have the following documents.

Document 1: Footer = "hello #1"
Document 2: Footer = "hello #2"
Document 3: Footer = "hello #3"
Document 4: Footer = "hello #4"

I first load Document 1, creating a new instance of DocumentWrapper (please note that some code was left out for brevity).
Next, I append Document 2 by calling wrapper.appendDocument("path to document 2"). This inserts document 2 with a page break and section break. A new Page Style is create with the random name generated, but when I copy the header/footer info from Document 2 into the new Page Style, it gets applied to the Default Page Style, or page one.

Next, I append Document 3 by calling wrapper.appendDocument("path to document 3"). This inserts document 3 after document 2 (with proper page breaks), but again the header/footer data is applied to page 1, or Default Page Style.

The same occurs for Document 4.

I've included all the relevant code below. Please note that I had to leave the PAGE BREAK code as is, otherwise the insertion of a new document would not occur on a new page. I may be able to leave out the Section Break, but I for now it shouldn't be a problem. I think I initially used them because some documents had multiple columns, but that is handled with the page styles.

Any additional thoughts?

Code: Select all

public class DocumentWrapper {

    private XTextDocument document;

    public void appendDocument(String tempDoc) throws IllegalArgumentException, java.lang.Exception {

            PropertyValue[] xComponentLoadProps = ... //"AsTemplate", "Hidden"

            DocumentWrapper document22 = new DocumentWrapper(componentImport(tempDoc, xComponentLoadProps));

            String newStyleName = String.valueOf("ps_" + Math.random());

            XStyle document2Style = document2.getStyle(StyleFamilies.PageStyles, PageStyles.Standard);

            document.getText().createTextCursor().gotoEnd(false);            

            XTextCursor cursor = this.insertSectionBreak(newStyleName);

            XDocumentInsertable xDocI = (XDocumentInsertable) getOOoUnoRuntimeQueryInterface(XDocumentInsertable.class, cursor);

            xDocI.insertDocumentFromURL((new File(tempDoc)).toURI().toString(), new PropertyValue[0]);

            Object o = getOOoUnoRuntimeServiceCreateInstance("com.sun.star.style.PageStyle", document);

            XStyle newPageStyle = (XStyle) getOOoUnoRuntimeQueryInterface(XStyle.class, o);

            newPageStyle.setName(newStyleName);

            DocumentWrapper.copyPageStyleProperties(document2Style, newPageStyle);

            this.setStyle(StyleFamilies.PageStyles, newPageStyle);

            XParagraphCursor xParaCursor = (XParagraphCursor) getOOoUnoRuntimeQueryInterface( XParagraphCursor.class, cursor);

            XPropertySet paraProps = (XPropertySet) getOOoUnoRuntimeQueryInterface( XPropertySet.class, xParaCursor);

            paraProps.setPropertyValue(ParagraphStyleProperties.PageDescName, newPageStyleName);

	    paraProps = null;

            xParaCursor = null;

            xDocI = null;

            cursor = null;

            document2Style = null;

            newStyleName = null;

    }

    public XTextCursor insertSectionBreak(String newSectionName) throws UnknownPropertyException, IllegalArgumentException, WrappedTargetException, PropertyVetoException, Exception, java.lang.Exception {

        XText xDocText = this.document.getText();

        XTextCursor xOrigDocTextCursor = xDocText.createTextCursor();

        xOrigDocTextCursor.gotoEnd(false);

        XPropertySet xOrigDocTextCursorProp = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, xOrigDocTextCursor);

        xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.PAGE_BEFORE);

        xDocText.insertControlCharacter(xOrigDocTextCursor, ControlCharacter.PARAGRAPH_BREAK, false);
        xOrigDocTextCursorProp.setPropertyValue("BreakType", BreakType.NONE);

        XParagraphCursor xParaCursor = (XParagraphCursor) getOOoUnoRuntimeQueryInterface( XParagraphCursor.class, xOrigDocTextCursor);

        xParaCursor.gotoPreviousParagraph(false);

        XNamed xChildNamed = (XNamed) getOOoUnoRuntimeQueryInterface(

                XNamed.class,

                getOOoUnoRuntimeServiceCreateInstance("com.sun.star.text.TextSection", this.document));

        xChildNamed.setName(newSectionName);

        XTextContent xChildContent = (XTextContent) getOOoUnoRuntimeQueryInterface(XTextContent.class, xChildNamed);

        xDocText.insertTextContent(xOrigDocTextCursor, xChildContent, false);

        xParaCursor.gotoPreviousParagraph(false);

        xOrigDocTextCursorProp = null;

        xParaCursor = null;

        xDocText = null;

        xChildNamed = null;

        xChildContent = null;

        return xOrigDocTextCursor;

    }

    public static void copyHeaderProperties(XPropertySet fromProps, XPropertySet toProps) throws UnknownPropertyException, WrappedTargetException, PropertyVetoException, IllegalArgumentException {

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackColor));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicFilter, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicFilter));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicLocation, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicLocation));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicURL, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackGraphicURL));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackTransparent, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackTransparent));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBodyDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBodyDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderDynamicSpacing, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderDynamicSpacing));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderIsDynamicHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderIsDynamicHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderLeftBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderLeftBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderLeftBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderLeftBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderLeftMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderLeftMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderRightBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderRightBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderRightBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderRightBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderRightMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderRightMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderShadowFormat, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderShadowFormat));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderTopBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTopBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderTopBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTopBorderDistance));

        Boolean isOn = Boolean.FALSE;

        Boolean isShared = Boolean.FALSE;

        Object objectIsOn = fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderIsOn);

        Object objectIsShared = fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderIsShared);

        if (objectIsOn instanceof Any){

            if (((Any)objectIsOn).getType() == Type.BOOLEAN){

                isOn = (Boolean) ((Any)objectIsOn).getObject();

            }

        } else if (objectIsOn instanceof Boolean){

            isOn = (Boolean) objectIsOn;

        }

        if (objectIsShared instanceof Any){

            if (((Any)objectIsShared).getType() == Type.BOOLEAN){

                isShared = (Boolean) ((Any)objectIsShared).getObject();

            }

        } else if (objectIsShared instanceof Boolean){

            isShared = (Boolean) objectIsShared;

        }

        objectIsOn = null;

        objectIsShared = null;

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderIsOn, isOn);

        toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderIsShared, isShared);

        XText fromXText = null;

        XText toXText = null;

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderText));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderText));

            toXText.setString(fromXText.getString());

        }

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTextLeft));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTextLeft));

            toXText.setString(fromXText.getString());

        }

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTextRight));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderTextRight));

            toXText.setString(fromXText.getString());

        }

        fromXText = null;

        toXText = null;

    }

    public static void copyFooterProperties(XPropertySet fromProps, XPropertySet toProps) throws UnknownPropertyException, WrappedTargetException, PropertyVetoException, IllegalArgumentException {

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBackColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBackColor));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicFilter, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicFilter));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicLocation, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicLocation));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicURL, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBackGraphicURL));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBackTransparent, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBackTransparent));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBodyDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBodyDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBottomBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBottomBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterBottomBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterBottomBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterDynamicSpacing, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterDynamicSpacing));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterIsDynamicHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterIsDynamicHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterLeftBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterLeftBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterLeftBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterLeftBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterLeftMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterLeftMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterRightBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterRightBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterRightBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterRightBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterRightMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterRightMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterShadowFormat, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterShadowFormat));

        Boolean isOn = Boolean.FALSE;

        Boolean isShared = Boolean.FALSE;

        Object objectIsOn = fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterIsOn);

        Object objectIsShared = fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterIsShared);

        if (objectIsOn instanceof Any){

            if (((Any)objectIsOn).getType() == Type.BOOLEAN){

                isOn = (Boolean) ((Any)objectIsOn).getObject();

            }

        } else if (objectIsOn instanceof Boolean){

            isOn = (Boolean) objectIsOn;

        }

        if (objectIsShared instanceof Any){

            if (((Any)objectIsShared).getType() == Type.BOOLEAN){

                isShared = (Boolean) ((Any)objectIsShared).getObject();

            }

        } else if (objectIsShared instanceof Boolean){

            isShared = (Boolean) objectIsShared;

        }

        objectIsOn = null;

        objectIsShared = null;

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterIsOn, isOn);

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterIsShared, isShared);

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterTopBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTopBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FooterTopBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTopBorderDistance));

        XText fromXText;

        XText toXText;

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterText));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.FooterText));

            toXText.setString(fromXText.getString());

        }

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTextLeft));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTextLeft));

            toXText.setString(fromXText.getString());

        }

        fromXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTextRight));

        if (isOn.booleanValue() && isShared.booleanValue() && fromXText != null && fromXText.getString() != null && !fromXText.getString().isEmpty()){

            toXText = (XText) getOOoUnoRuntimeQueryInterface(XText.class, toProps.getPropertyValue(PageStyles.PageStyleProperties.FooterTextRight));

            toXText.setString(fromXText.getString());

        }

        fromXText = null;

        toXText = null;

    }

    public static void copyPageStyleProperties(XStyle fromStyle, XStyle toStyle) throws UnknownPropertyException, WrappedTargetException, PropertyVetoException, IllegalArgumentException {

        XPropertySet fromProps = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, fromStyle);

        XPropertySet toProps = (XPropertySet) getOOoUnoRuntimeQueryInterface(XPropertySet.class, toStyle);

        DocumentWrapper.copyHeaderProperties(fromProps, toProps);

        DocumentWrapper.copyFooterProperties(fromProps, toProps);

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BackColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BackColor));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BackGraphicFilter, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BackGraphicFilter));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BackGraphicLocation, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BackGraphicLocation));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BackGraphicURL, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BackGraphicURL));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BackTransparent, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BackTransparent));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BottomBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BottomBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BottomBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BottomBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.BottomMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.BottomMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineAdjust, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineAdjust));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineColor));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineRelativeWidth, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineRelativeWidth));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineTextDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineTextDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.FootnoteLineWeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.FootnoteLineWeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridBaseHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridBaseHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridBaseWidth, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridBaseWidth));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridColor));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridDisplay, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridDisplay));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridLines, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridLines));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridMode, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridMode));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridPrint, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridPrint));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridRubyHeight, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridRubyHeight));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.GridSnapToChars, fromProps.getPropertyValue(PageStyles.PageStyleProperties.GridSnapToChars));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.Height, fromProps.getPropertyValue(PageStyles.PageStyleProperties.Height));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.IsLandscape, fromProps.getPropertyValue(PageStyles.PageStyleProperties.IsLandscape));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.LeftBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.LeftBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.LeftBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.LeftBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.LeftMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.LeftMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.NumberingType, fromProps.getPropertyValue(PageStyles.PageStyleProperties.NumberingType));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.PageStyleLayout, fromProps.getPropertyValue(PageStyles.PageStyleProperties.PageStyleLayout));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.PrinterPaperTray, fromProps.getPropertyValue(PageStyles.PageStyleProperties.PrinterPaperTray));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.RegisterParagraphStyle, fromProps.getPropertyValue(PageStyles.PageStyleProperties.RegisterParagraphStyle));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.RightBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.RightBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.RightBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.RightBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.RightMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.RightMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.RubyBelow, fromProps.getPropertyValue(PageStyles.PageStyleProperties.RubyBelow));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.ShadowFormat, fromProps.getPropertyValue(PageStyles.PageStyleProperties.ShadowFormat));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.Size, fromProps.getPropertyValue(PageStyles.PageStyleProperties.Size));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.StandardPageMode, fromProps.getPropertyValue(PageStyles.PageStyleProperties.StandardPageMode));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.TextColumns, fromProps.getPropertyValue(PageStyles.PageStyleProperties.TextColumns));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.TopBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.TopBorder));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.TopBorderDistance, fromProps.getPropertyValue(PageStyles.PageStyleProperties.TopBorderDistance));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.TopMargin, fromProps.getPropertyValue(PageStyles.PageStyleProperties.TopMargin));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.UserDefinedAttributes, fromProps.getPropertyValue(PageStyles.PageStyleProperties.UserDefinedAttributes));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.Width, fromProps.getPropertyValue(PageStyles.PageStyleProperties.Width));

        toProps.setPropertyValue(PageStyles.PageStyleProperties.WritingMode, fromProps.getPropertyValue(PageStyles.PageStyleProperties.WritingMode));

        fromProps = null;

        toProps = null;

    }
}
Last edited by groverblue on Thu Mar 10, 2011 12:51 am, edited 1 time in total.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

There is no question that the header, footer and the contents of each are properties of the page style. Thus, if you put something in page style X's footer it should have no effect on page style Y's footer.

For this I'll assume that Doc1 is only one page long. Go to page 2, you should have a colored top border which indicates that there is a page break. With your cursor in the very first paragraph of page 2 do Format > Page > Text Flow tab, in the Breaks section both boxes should be checked and your new page style should show in the middle by the second box.

As you click into the different pages of the document you should see the page style name change in the status bar at the bottom of the page (2nd item from left).

Do you fail any of these "tests"?
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

Format > Page does not have a "Text Flow" tab. It has: Organizer, Page, Background, Header, Footer, Borders, Columns, Footnote. Under the Header/Footer tabs, their checkboxes are enabled. Also, the window title is "Page Style: 1915124614" - which is the expected style name.

Additionally, the second item on the status bar also indicates "1915124614". When I go back to the first page, the style changes to "Default". Finally, there is a green line indicating a page break, and the Text Section is after the break.

Everything appear to in place as expected. I just don't get it. Copying the header/footer text, as indicated above, should not put it in the Default page style. Clearly the page style reference is the new one, and it is inserted.
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

JohnV, thanks again for your help and patience. We will figure this out!
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

Format > Page does not have a "Text Flow" tab.
My error corrected below.

With your cursor in the very first paragraph of page 2 do Format > Paragraph > Text Flow tab, in the Breaks section both boxes should be checked and your new page style should show in the middle by the second box. This still assumes there is a page break indicated here.
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

JohnV, those settings look good.

It looks like I have it figured out, partially! I did the following: I saved, then retrieved, the new page style before copying the properties to it:

Code: Select all

XTextCursor cursor = this.insertSectionBreak(newStyleName);
Object o = getOOoUnoRuntimeServiceCreateInstance("com.sun.star.style.PageStyle", document);
XStyle newPageStyle = (XStyle) getOOoUnoRuntimeQueryInterface(XStyle.class, o);
newPageStyle.setName(newStyleName);
this.setStyle(StyleFamilies.PageStyles, newPageStyle);

newPageStyle = this.getStyle(StyleFamilies.PageStyles, newPageName);  // <--------- NEW

XParagraphCursor xParaCursor = (XParagraphCursor) getOOoUnoRuntimeQueryInterface( XParagraphCursor.class, cursor);
XPropertySet paraProps = (XPropertySet) getOOoUnoRuntimeQueryInterface( XPropertySet.class, xParaCursor);
paraProps.setPropertyValue(ParagraphStyleProperties.PageDescName, newPageStyleName);           
DocumentWrapper.copyPageStyleProperties(document2Style, newPageStyle);
XDocumentInsertable xDocI = (XDocumentInsertable) getOOoUnoRuntimeQueryInterface(XDocumentInsertable.class, cursor);
xDocI.insertDocumentFromURL((new File(tempDoc)).toURI().toString(), new PropertyValue[0]);   


This results in the header/footer being copied, but the text formatting is not retained. Additionally, images in the second page's header are not page bound, instead the image is moved to the first page.

I have a few questions, and I hope you know the answer to one or more. I may need to post a separate thread to get this info.
  • First, how do header/footer styles work? I see that there are the following Paragraph Styles, but do they have to be the ones used? : "Footer", "Footer left", "Footer right", "Header", "Header left", "Header right"
  • Is there a Paragraph Style? Can there be more than one?
  • Are there Character Styles?
  • How can I determine the used Styles within a header/footer? I do have access to the header/footer XText.
  • Will copying the XText also copy any sub components like tables, graphics?
Thanks.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: Merge documents, but retain their respective headers/foo

Post by JohnV »

Additionally, images in the second page's header are not page bound, instead the image is moved to the first page.
My experience indicates that a picture in inserted in the document at the current location of the view cursor. In the code below I get the view cursor into the header and then insert a picture.

OOo uses a different paragraph style for almost everything but you can use any paragraph style you like. The code below change the header's style.

Yes there are character styles. Press F11 to open the Stylist and click the 2nd icon from the left.
How can I determine the used Styles within a header/footer? I do have access to the header/footer XText.
I print it in the code below.
Will copying the XText also copy any sub components like tables, graphics?
I really don't know the answer to this one. My guess is possibly tables but I doubt frames or graphics.

Code: Select all

Sub Main
oDoc = ThisComponent
ps = oDoc.getStyleFamilies.getByName("PageStyles").getByName("Default")
oTC = ps.HeaderText.createTextCursor
Print oTC.ParaStyleName
oTC.ParaStyleName = "Default" 'Change header pargraph style.
oVC = oDoc.CurrentController.getViewCursor
oVC.gotoRange(oTC,false) 'Get the view cursor into the header.
dim document   as object ' The rest is insert a picture anchored in the header.
dim dispatcher as object
document   = oDoc.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FileName"
args1(0).Value = "file:///C:/Users/John/Documents/2by3.jpg"
args1(1).Name = "FilterName"
args1(1).Value = "<All formats>"
args1(2).Name = "AsLink"
args1(2).Value = false
args1(3).Name = "Style"
args1(3).Value = "Graphics"
dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args1()) 
End Sub
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

Hey JohnV,

I can't seem to get this one to work. I think that I need to copy the paragraph styles over, but without knowing for sure it's just a waste of time. Unfortunately, due to deadline restrictions, we had to do a workaround for the header/footers, placing them in the actual body of the document (since they are single page documents, stuff like page number is not an issue). This cripples the capability of the software, however. I hope to get back on this task and resolve it, and I will surely post the solution. Thank you for all your help.

-John
aocana
Posts: 3
Joined: Tue Jul 05, 2011 10:57 am

Re: Merge documents, but retain their respective headers/foo

Post by aocana »

Where are those objects???

1) PageStyles.PageStyleProperties.HeaderBackColor
2) PageStyles.PageStyleProperties.HeaderBottomBorder
etc
etc

in copyHeaderProperties Method for example?????

Sorry for the question but i don't find them!!!
OpenOffice 3 - XP
groverblue
Posts: 40
Joined: Wed Mar 26, 2008 6:17 pm

Re: Merge documents, but retain their respective headers/foo

Post by groverblue »

In my post dated Tue Mar 08, 2011 10:57 am:

Code: Select all

toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBackColor, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBackColor));

toProps.setPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorder, fromProps.getPropertyValue(PageStyles.PageStyleProperties.HeaderBottomBorder));
See the post for all the details. Expand the code section.
aocana
Posts: 3
Joined: Tue Jul 05, 2011 10:57 am

Re: Merge documents, but retain their respective headers/foo

Post by aocana »

I have expanded all the code sections of this post but i don't see them!!!
OpenOffice 3 - XP
User avatar
RoryOF
Moderator
Posts: 34612
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Merge documents, but retain their respective headers/foo

Post by RoryOF »

Have another look! They are in groverblue's post of 08 Mar 2011 at 10.57 am.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
aocana
Posts: 3
Joined: Tue Jul 05, 2011 10:57 am

Re: Merge documents, but retain their respective headers/foo

Post by aocana »

Yes!! i have seen that but i wanted to mean about de declaration of those objects!! They are Strings or what??
OpenOffice 3 - XP
Post Reply