2

I have following code to export a sheet to a PDF file:

Option Explicit

Sub exportToPdf

    Dim document As Object
    Dim dispatcher As Object

    document=ThisComponent.CurrentController.Frame
    dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")

    Dim args1(1) as new com.sun.star.beans.PropertyValue

    args1(0).Name = "URL"
    args1(0).Value = "file:///home/someuser/Desktop/exported.pdf"
    args1(1).Name = "FilterName"
    args1(1).Value = "calc_pdf_Export"

    dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())

End Sub

It is working properly. I have following questions:

  • Is it possible to export a PDF without creating unoService? (And how to do it?)

  • How to export a range of cells instead of the whole sheet?

Community
  • 1
  • 1
Maciej
  • 9,355
  • 2
  • 15
  • 18

1 Answers1

1

The creating of uno services are not the problem. But the dispatcher you should avoid. Thats why recording a macro with openoffice is not very helpful. You have to bothering yourself with the API.

This tutorial shows how PDF export works in general: https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export

For the customizing a MediaDescriptor is needed. https://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html

This service may be represented by a ::com::sun::star::beans::PropertyValue[]. This type has Name and Value properties.

This MediaDescriptor needs at least a FilterName. A not very actual list of FilterNames can be found here: https://wiki.openoffice.org/wiki/Framework/Article/Filter/FilterList_OOo_3_0

For further customizing a FilterData is possible. For those read: https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#General_properties

As of the Filter data demo in https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#Filter_data_demo for the Value of the FilterData also a array of PropertyValues is used.

So we get:

sub storeCellRangeToPDF()

 oDoc   = ThisComponent
 oController = oDoc.getCurrentController()
 oSheet = oController.getActiveSheet()
 oCellRange = oSheet.getCellRangeByName("$A$1:$B$3")

 dim aFilterData(0) as new com.sun.star.beans.PropertyValue
 aFilterData(0).Name = "Selection"
 aFilterData(0).Value = oCellRange

 dim aMediaDescriptor(1) as new com.sun.star.beans.PropertyValue
 aMediaDescriptor(0).Name = "FilterName"
 aMediaDescriptor(0).Value = "calc_pdf_Export"
 aMediaDescriptor(1).Name = "FilterData"
 aMediaDescriptor(1).Value = aFilterData()

 oDoc.storeToURL("file:///home/axel/Dokumente/test.pdf", aMediaDescriptor())

end sub
Axel Richter
  • 56,077
  • 6
  • 60
  • 87
  • 1
    Can you elaborate why dispatcher should be avoided? – Maciej May 31 '15 at 18:56
  • 1
    See: https://wiki.openoffice.org/wiki/The_OpenOffice.org_recorder_and_UNO_dispatch_calls "The recorder produces a series of UNO dispatch calls, which are not particularly useful for learning the OpenOffice.org API model. I personally find the code that the recorder produces to be a little hard to follow." ... "The arguments of each dispatch command are not documented." – Axel Richter Jun 01 '15 at 05:02
  • This code will not run with `Option Explicit`. Variables `oDoc, oController, oSheet, oCellRange` are not declared. – Maciej Jun 23 '15 at 22:41