【问题标题】:How to export cell range to PDF file?如何将单元格范围导出为 PDF 文件?
【发布时间】:2015-05-28 14:30:33
【问题描述】:

我有以下代码可以将工作表导出为 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

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

  • 是否可以在不创建 unoService 的情况下导出 PDF? (以及怎么做?)

  • 如何导出一系列单元格而不是整个工作表?

【问题讨论】:

    标签: libreoffice-calc export-to-pdf libreoffice-basic


    【解决方案1】:

    创建 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。可以在此处找到不太实际的 FilterName 列表: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 也使用了 PropertyValues 数组。

    所以我们得到:

    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
    

    【讨论】:

    • 您能否详细说明为什么应避免使用调度程序?
    • 见:wiki.openoffice.org/wiki/… "记录器产生一系列UNO调度调用,对于学习OpenOffice.org API模型并不是特别有用。我个人觉得记录器产生的代码是有点难跟上。” ...“没有记录每个调度命令的参数。”
    • 此代码不会与Option Explicit 一起运行。变量oDoc, oController, oSheet, oCellRange 未声明。
    猜你喜欢
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2016-02-29
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多