Watermark generation using Pyuno

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sherryFA
Posts: 3
Joined: Wed May 31, 2017 11:45 am

Watermark generation using Pyuno

Post by sherryFA »

I am trying to add watermark in a document using python but I am unable to find the code or any kind of help in documentation. Any suggestions or samples please !!
P.S: I have seen some examples in java but as I am new to python then I wasn't sure how to convert them into python as I still don't understand how to read python documentation.
LibreOffice 5.3.3.2 on Linux 16.10
FJCC
Moderator
Posts: 9560
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: watermark generation using Pyuno

Post by FJCC »

If you post the java code, or a link to it, we can more easily suggest equivalent Python code. Which programming language is used is not as important as knowing what API calls to use.
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.
sherryFA
Posts: 3
Joined: Wed May 31, 2017 11:45 am

Re: Watermark generation using Pyuno

Post by sherryFA »

Actually I was looking at this question viewtopic.php?f=45&t=2586 and I found some related info here in java.
LibreOffice 5.3.3.2 on Linux 16.10
FJCC
Moderator
Posts: 9560
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Watermark generation using Pyuno

Post by FJCC »

It looks like the code you found adds the watermark as part of the process of exporting the document to PDF. Is that what you want to do or do you want to add the watermark as you would with the menu Format -> Page -> Background?
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.
sherryFA
Posts: 3
Joined: Wed May 31, 2017 11:45 am

Re: Watermark generation using Pyuno

Post by sherryFA »

Actually I want to add the watermark while creation of PDF file. so you can say menu Format -> Page -> Background.
LibreOffice 5.3.3.2 on Linux 16.10
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Watermark generation using Pyuno

Post by Villeroy »

[Tutorial] Introduction into object inspection with MRI

menu:Tools>Add-Ons>Mri
StyleFamilies
method getByName(PageStyles)
method getByName(...)
various properties...
mri-menu:Tools>Code>Python
Ctrl+H
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
FJCC
Moderator
Posts: 9560
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Watermark generation using Pyuno

Post by FJCC »

This pair of Python functions adds a watermark to a Writer file and export it as a PDF. The SVG file used as an image was provided by acknak here. He posted it as a txt file but it is really an svg, you just need to change the extension from txt to svg. I also tried adding a watermark in the same way the Java code did, using the PropertyValue named Watermark but that did not do anything. I commented out that code but you can see how I did it. I never use Python, so this code could probably be written more elegantly.

Code: Select all

import uno
from com.sun.star.beans import PropertyValue

def AddBkgrd():
  doc = XSCRIPTCONTEXT.getDocument()
  oStyleFamilies = doc.getStyleFamilies()
  PageStyles = oStyleFamilies.getByName("PageStyles")
  DefaultStyle = PageStyles.getByName("Standard")
  DefaultStyle.BackGraphicURL =uno.systemPathToFileUrl('c:\\users\\fjcc\\desktop\\watermark.svg')
  


def exportPDF():
  storeUrl = uno.systemPathToFileUrl('c:\\users\\fjcc\\desktop\\PDFversion.pdf')
  doc = XSCRIPTCONTEXT.getDocument()
  
  #filterProps = PropertyValue()
  #filterProps.Name = "Watermark"
  #filterProps.Value = "MyMark" 

  storeProps0 = PropertyValue()
  storeProps0.Name = "FilterName"
  storeProps0.Value = "writer_pdf_Export"
  storeProps1 = PropertyValue()
  storeProps1.Name = "Overwrite "
  storeProps1.Value = True
  #storeProps2 = PropertyValue()
  #storeProps2.Name = "FilterData"
  #storeProps2.Value = (filterProps,)
  doc.storeToURL(storeUrl, (storeProps0, storeProps1))
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
karolus
Volunteer
Posts: 1230
Joined: Sat Jul 02, 2011 9:47 am

Re: Watermark generation using Pyuno

Post by karolus »

Hallo

AFAIK there is no need for this double backslash-plaque on Windows with python, use single slashes instead…

Code: Select all

    …uno.systemPathToFileUrl('c:/users/fjcc/desktop/watermark.svg')
Karolus
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
Post Reply