如何将单元格范围导出到 PDF 文件?

How to export cell range to PDF file?

我有以下代码可以将 sheet 导出到 PDF 文件:

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

它工作正常。我有以下问题:

uno 服务的创建不是问题。但是你应该避免使用调度员。这就是为什么用 openoffice 录制宏不是很有用。你不得不为 API.

烦恼

本教程展示了 PDF 导出的一般工作原理:https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export

要进行自定义,需要 MediaDescriptorhttps://www.openoffice.org/api/docs/common/ref/com/sun/star/document/MediaDescriptor.html

此服务可能由 ::com::sun::star::beans::PropertyValue[] 表示。此类型具有 NameValue 属性。

这个MediaDescriptor至少需要一个FilterName。可以在此处找到一个不太实际的 FilterNames 列表:https://wiki.openoffice.org/wiki/Framework/Article/Filter/FilterList_OOo_3_0

要进一步自定义 FilterData 是可能的。对于那些阅读:https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#General_properties

https://wiki.openoffice.org/wiki/API/Tutorials/PDF_export#Filter_data_demo 中的过滤数据演示开始,FilterDataValue 也使用了 PropertyValue 数组。

所以我们得到:

sub storeCellRangeToPDF()

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

 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