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

Creating a macro - Writing a Script - Using the API

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

Postby murielgm » Fri Jul 05, 2019 10:36 pm

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
LibreOffice 6.2.4.2 (x64) ON Windows 10.0
murielgm
 
Posts: 3
Joined: Fri Jul 05, 2019 10:01 pm

Re: Watermark/Background - writer - PDFTK - Macro

Postby musikai » Sat Jul 06, 2019 11:32 am

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/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 247
Joined: Wed Nov 11, 2015 12:19 am

Re: Watermark/Background - writer - PDFTK - Macro

Postby murielgm » Sat Jul 06, 2019 7:29 pm

Thanks for your tip! Google had told me that you also uses PDFTK, but know I will look for your project more carefully!
LibreOffice 6.2.4.2 (x64) ON Windows 10.0
murielgm
 
Posts: 3
Joined: Fri Jul 05, 2019 10:01 pm

Re: Watermark/Background - Writer - PDFTK - Macro

Postby musikai » Sun Jul 07, 2019 9:43 pm

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"
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/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 247
Joined: Wed Nov 11, 2015 12:19 am

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

Postby murielgm » Mon Jul 08, 2019 2:22 pm

Wow, you are a genius!!!!!
Thanks!
LibreOffice 6.2.4.2 (x64) ON Windows 10.0
murielgm
 
Posts: 3
Joined: Fri Jul 05, 2019 10:01 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: Google [Bot] and 7 guests