Page 1 of 1

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

PostPosted: Fri Jul 05, 2019 10:36 pm
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


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.

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.


Re: Watermark/Background - writer - PDFTK - Macro

PostPosted: Sat Jul 06, 2019 11:32 am
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.

Re: Watermark/Background - writer - PDFTK - Macro

PostPosted: Sat Jul 06, 2019 7:29 pm
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!

Re: Watermark/Background - Writer - PDFTK - Macro

PostPosted: Sun Jul 07, 2019 9:43 pm
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   Expand viewCollapse view
Sub export_with_background_PDF
BackgroundPDF = "C:\Users\yourname\Desktop\background.pdf"
pdftkapp = "pdftk"
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( "" )
SysTempDir = oPathSettings.Temp_writable & "/"

TempPDF = SysTempDir & "temp.pdf"

Dim args3(0) as new
args3(0).Name = "FilterName"
args3(0).Value = "writer_pdf_Export"

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

End Sub

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

PostPosted: Mon Jul 08, 2019 2:22 pm
by murielgm
Wow, you are a genius!!!!!