Jump to content

Macros/ScriptForge/RedefinePDFExportOptions

From The Document Foundation Wiki


How to (re)define PDF export options

Authored by Jean-Pierre Ledure.

The current PDF export settings defined in the PDF Options dialogue, which can be accessed by choosing File - Export as - Export as PDF. Export options set with the PDF Options dialogue are kept until they are redefined with the dialogue or by code. In the example, all the options are displayed and afterwards one of them is modified.

All the PDF Export options are displayed, and afterwards one of them is modified.
PDF Export options.

How to run it in BASIC

Create a new document.

  1. Open the Basic IDE
  2. Select the first available blank module
  3. Copy and paste the Basic code
  4. Verify line 14
  5. Run Main()

How to run it in Python

Create a new document.

  1. Run APSO
  2. Create a new module, call it 'Module1'
  3. Copy and paste the Python code below
  4. Verify line 15
  5. Save and run the Main() method

Code

REM		How to (re)define the PDF options
REM		Minimal required version: LibreOffice 7.6
REM 	Used service(s)	Dictionary, Session

Sub PdfOptions()
'	Read the PDF options and modify one of them

Dim session As Object
Dim dict1 As Object, dict2 As Object
Dim key As String
Dim message As String

	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

	Set session = CreateScriptService("Session")

	'	Read the actual PDF export options ?
	Set dict1 = session.GetPDFExportOptions()

	With dict1
		'	Display the actual options
		message = ""
		For Each key in .Keys
			message = message & key & " = " & .Item(key) & ", "
		Next key
		.Dispose()
	End With
	MsgBox message

	'	Change one or more options
	'	We could have re-used the same dictionary, we choose to make a new one
	Set dict2 = CreateScriptService("Dictionary", True)		'	True = case sensitive
	dict2.Add("ViewPDFAfterExport", True)

	'	Rewrite the PDF options
	session.SetPDFExportOptions(dict2)	
	dict2.Dispose()
	
	'	Check by running Export As PDF on any open document, now or later
	'	In the General tab of the dialog, the View PDF after export checkbox is selected.

End Sub
# coding: utf-8
from __future__ import unicode_literals

from scriptforge import CreateScriptService
basic = CreateScriptService('Basic')

###		How to (re)define the PDF options
###		Minimal required version: LibreOffice 7.6
### 	Used service(s)	Dictionary, Session

def pdfoptions():
	#	Read the PDF options and modify one of them

	session = CreateScriptService('Session')

	#	Read the actual PDF export options
	dict1 = session.GetPDFExportOptions()

	#	Display the actual options
	message = ''
	for key, item in dict1.items():
		message = message + key + ' = ' + str(item) + ', '
	basic.MsgBox(message)

	#	Change one or more options
	#	We could have re-used the same dictionary, we choose to make a new one
	dict2 = CreateScriptService('Dictionary')
	dict2['ViewPDFAfterExport'] = True


	#	Rewrite the PDF options
	session.SetPDFExportOptions(dict2)

	#	Check by running Export As PDF on any open document, now or later
	#	In the General tab of the dialog, the View PDF after export checkbox is selected.


g_exportedScripts = (pdfoptions,)

See also