Page 1 of 1
Watermark generation using Pyuno
Posted: Wed May 31, 2017 11:51 am
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.
Re: watermark generation using Pyuno
Posted: Wed May 31, 2017 3:44 pm
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.
Re: Watermark generation using Pyuno
Posted: Wed May 31, 2017 4:20 pm
by sherryFA
Actually I was looking at this question
viewtopic.php?f=45&t=2586 and I found some related info here in java.
Re: Watermark generation using Pyuno
Posted: Wed May 31, 2017 6:08 pm
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?
Re: Watermark generation using Pyuno
Posted: Wed May 31, 2017 10:52 pm
by sherryFA
Actually I want to add the watermark while creation of PDF file. so you can say menu Format -> Page -> Background.
Re: Watermark generation using Pyuno
Posted: Thu Jun 01, 2017 12:07 am
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
Re: Watermark generation using Pyuno
Posted: Thu Jun 01, 2017 5:58 am
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))
Re: Watermark generation using Pyuno
Posted: Mon Jun 05, 2017 11:57 am
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