Skip to content

Instantly share code, notes, and snippets.

@non7top
Created September 5, 2019 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non7top/2e62a322214d30f7db9a76bf49ba46cf to your computer and use it in GitHub Desktop.
Save non7top/2e62a322214d30f7db9a76bf49ba46cf to your computer and use it in GitHub Desktop.
LibreOffice Calc macros to print selected pages into pdf
sub print_to_pdf2
if len(ThisComponent.getURL())=0 then
msgbox "Save document first!"
exit sub
end if
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
sPath = convertfromurl(DirectoryNameoutofPath(ThisComponent.getURL(),"/"))
docname = convertfromurl(GetFileNameWithoutExtension(ThisComponent.getURL(),"/"))
rem ####
Pages="2,9,17,19,64,66,67,70,77"
dim aPages() as String
aPages=Split(Pages, ",")
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim argsF(1) as new com.sun.star.beans.PropertyValue
argsF(0).Name = "PageRange"
dim alen as Integer
alen=UBound(aPages)-LBound(aPages)
dim iCount as Integer
dim args1(2) as new com.sun.star.beans.PropertyValue
For iCount = 0 To alen
pdfname=converttourl(sPath & getpathseparator & docname & " - " & aPages(iCount) & ".pdf")
argsF(0).Name = "PageRange"
argsF(0).Value = aPages(iCount)
'Export to PDF
args1(0).Name = "FilterName"
args1(0).Value = "calc_pdf_Export"
args1(1).Name = "FilterData"
args1(1).Value = argsF()
args1(2).Name = "URL"
args1(2).Value = pdfname
dispatcher.executeDispatch(document, ".uno:ExportToPDF", "", 0, args1())
Next iCount
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment