How to export cell range to PDF file?

本秂侑毒 提交于 2020-01-01 06:57:12

问题


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?


回答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


来源:https://stackoverflow.com/questions/30509532/how-to-export-cell-range-to-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!