[SOLVED] Watermark/Background - Writer - PDFTK - Macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
murielgm
Posts: 6
Joined: Fri Jul 05, 2019 10:01 pm

[SOLVED] Watermark/Background - Writer - PDFTK - Macro

Post by murielgm »

Hi, sorry my bad English. I know almost nothing about macros, I only know how to find stuff on google.

I was looking for a solution to use a watermark image in a writer document, without having to bother about margins.

So, I found this answer: :D

viewtopic.php?f=7&t=93590&hilit=PDFTK#p445098
1.: Create a document without background picture, and export it to PDF format. Then create an one page document with zero margins, and with a picture on the page. Export it to PDF format. Then use a third party PDF manager software (like the free PDFTK) put a watermark (background picture) into each page of the first pdf file, by usage of the second pdf file as "watermark file".
I was able to do it, using the PDFTK command: "pdftk in.pdf background back.pdf output out.pdf"

So, is it possible to create a MACRO, to do the following: :?: :?:

1) Export the current document ODT file to a TEMPORARY PDF FILE;
2) Get this TEMPORARY PDF FILE and merge it with another PDF FILE (the watermark pdf) using PDFTK (using the parameter "BACKGROUND"), to create the FINAL PDF.
3) Delete the TEMPORARY PDF FILE.

I have tried to understand the SHELL function, but it is too too too far far away from my abilities to make all the steps to happen :D

Thx in advance.

MGM
Curitiba/Brazil
Last edited by robleyd on Mon Jul 08, 2019 2:51 pm, edited 2 times in total.
Reason: Add green tick
Version: 7.5.9.2 (X86_64) / LibreOffice Community
Build ID: cdeefe45c17511d326101eed8008ac4092f278a9
CPU threads: 8; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: pt-BR (pt_BR); UI: pt-BR
Calc: threaded
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Watermark/Background - writer - PDFTK - Macro

Post by musikai »

I don't have the time to create such a specific macro for you but you can have a look into the macro code of my free project LOSA (in the signature) which also has this functionality built-in.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
murielgm
Posts: 6
Joined: Fri Jul 05, 2019 10:01 pm

Re: Watermark/Background - writer - PDFTK - Macro

Post by murielgm »

Thanks for your tip! Google had told me that you also uses PDFTK, but know I will look for your project more carefully!
Version: 7.5.9.2 (X86_64) / LibreOffice Community
Build ID: cdeefe45c17511d326101eed8008ac4092f278a9
CPU threads: 8; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: pt-BR (pt_BR); UI: pt-BR
Calc: threaded
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Watermark/Background - Writer - PDFTK - Macro

Post by musikai »

Yes my project can make usage of a lot of functions from pdftk.

Here I wrote together the background function. (actually I use the multibackground function. If you don't want that then change the word "multibackground" into "background" in the shell command right at the bottom)

Please edit the path to your Background PDF at the beginning.

Running this macro will export your current document to a temp.pdf in the system_temp_directory, add the background.pdf and export the result.pdf into the same folder as the original.odt. It will be named like the original.odt but as pdf: eg. original.pdf

After that the temp.pdf will be deleted

Code: Select all

Sub export_with_background_PDF
BackgroundPDF = "C:\Users\yourname\Desktop\background.pdf"
pdftkapp = "pdftk"
oDoc=ThisComponent
if len(oDoc.getURL())=0 then
 msgbox "Save document first!"
 exit sub
end if

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
 workdirfull = DirectoryNameoutofPath(oDoc.getURL(),"/")
 docname = GetFileNameWithoutExtension(oDoc.getURL(),"/")

 ResultPDF =  workdirfull & "/" & docname & ".pdf"

oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
SysTempDir = oPathSettings.Temp_writable & "/"

TempPDF = SysTempDir & "temp.pdf"

Dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "FilterName"
args3(0).Value = "writer_pdf_Export"
oDoc.storeToURL(TempPDF,args3())

Shell(pdftkapp,0, """" & ConvertFromURL(TempPDF) & """" & " multibackground " & """" & ConvertFromURL(BackgroundPDF) & """" & " output " & """" & ConvertFromURL(ResultPDF) & """", true)
kill(ConvertFromURL(TempPDF))

End Sub
Last edited by musikai on Tue Sep 10, 2019 10:55 am, edited 1 time in total.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
murielgm
Posts: 6
Joined: Fri Jul 05, 2019 10:01 pm

Re: Watermark/Background - Writer - PDFTK - Macro [SOLVED]

Post by murielgm »

Wow, you are a genius!!!!!
Thanks!
Version: 7.5.9.2 (X86_64) / LibreOffice Community
Build ID: cdeefe45c17511d326101eed8008ac4092f278a9
CPU threads: 8; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: pt-BR (pt_BR); UI: pt-BR
Calc: threaded
Post Reply