I already found the solution, so I post it here, if someone also needs it in 
future. 

The simply solution is to set the background for all page styles, and not only 
for the Standard page style. So all what is needed is a loop.

String[] styles = xPageStyleCollection.getElementNames();
for(String style : styles) 
{
        XPropertySet xPropertySet = (XPropertySet) 
UnoRuntime.queryInterface(XPropertySet.class, 
xPageStyleCollection.getByName(style) );
        xPropertySet.setPropertyValue("BackGraphicURL", 
createUNOFileURL("watermark.jpg", xRemoteContext));
}


Greetings
Robert

-----Ursprüngliche Nachricht-----
Von: Thullner, Robert 
Gesendet: Mittwoch, 08. August 2007 16:13
An: '[email protected]'
Betreff: Add background image to file

Hello
 
I use open office to convert MS Word files to PDF documents. This is basically 
done with the methods, I found in the Code Snippet Base
http://codesnippets.services.openoffice.org/Office/Office.ConvertDocuments.snip
 
Now I also want to add a watermark to the pdf file, to indicate that it is a 
copy of another document. My idea was just to add a background image to the MS 
Word file, before converting it to pdf. I do this with the following method 
(also mainly taken out of the Code Snippet Base).
 
private static void addWatermark(XComponent xDocument, XComponentContext 
xRemoteContext) throws Exception {
    XTextDocument oDoc = (XTextDocument) 
UnoRuntime.queryInterface(XTextDocument.class, xDocument);

    // create a supplier to get the Style family collection
    XStyleFamiliesSupplier xSupplier = ( XStyleFamiliesSupplier ) 
UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, oDoc);

    // get the NameAccess interface from the Style family collection
    XNameAccess xNameAccess = xSupplier.getStyleFamilies();

    XNameContainer xPageStyleCollection = (XNameContainer) 
UnoRuntime.queryInterface(XNameContainer.class, xNameAccess.getByName( 
"PageStyles" ));

    // create a PropertySet to set the properties for the new Pagestyle
    XPropertySet xPropertySet = (XPropertySet) 
UnoRuntime.queryInterface(XPropertySet.class, 
xPageStyleCollection.getByName("Standard") );

    xPropertySet.setPropertyValue("BackGraphicURL", 
createUNOFileURL("watermark.jpg", xRemoteContext));

}


But this method does not always add a background image to the file. I did not 
find any pattern, when an image is put on the page and when not.
Also if the file is longer than one page, there is never a background image on 
the first page, but only on the following pages. 

Thanks for any help that can solve these problems
Robert

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to