如何设置导出 LibreOffice Writer 页面图像的宏的分辨率?

如何设置导出 LibreOffice Writer 页面图像的宏的分辨率?

之前我问过如何将 LibreOffice Writer 文档的所有页面导出为图像。我得到了回答。

我已经根据自己的需要调整了宏,这是我当前的代码:

Sub StoreEachPageToWebP()
    Dim oDoc As Variant, oViewCursor As Variant
    Dim iPageCount As Long, iPage As Long 
    Dim sOriginalURL As String, sFileURL As String 
    dim args1(1) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "FilterName"
    args1(0).Value = "writer_webp_Export"
    args1(1).Name = "FilterData"
    args1(1).Value = Array(Array("Quality",0,75,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Lossless",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelWidth",0,1000,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelHeight",0,647,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalWidth",0,26455,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalHeight",0,17116,com.sun.star.beans.PropertyState.DIRECT_VALUE))

    oDoc = ThisComponent        
    iPageCount = oDoc.getCurrentController().PageCount
    oViewCursor = oDoc.getCurrentController().getViewCursor()
    sOriginalURL = oDoc.getURL()
    oViewCursor.jumpToFirstPage()
    For iPage = 1 To iPageCount
        sFileURL = Replace(sOriginalURL, ".odt", "-" & Format(iPage, "000") & ".webp")
        oDoc.StoreToURL(sFileURL, args1)
        If Not oViewCursor.jumpToNextPage() Then End
    Next iPage
End Sub

不幸的是,FilterData选项似乎被忽略了。导出的 WebP 图像始终具有相同的分辨率,即页面的标准大小。当我手动导出时,它可以正常工作。我使用录制的宏来获取选项,FilterData因此我可以非常确定它们是正确的。录制的宏如下所示:

sub RecordedPageExportAsWebP
rem ----------------------------------------------------------------------
rem define variables
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")

rem ----------------------------------------------------------------------
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = "file:///home/user/Documents/namecards-all.webp"
args1(1).Name = "FilterName"
args1(1).Value = "writer_webp_Export"
args1(2).Name = "FilterData"
args1(2).Value = Array(Array("Quality",0,75,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("Lossless",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelWidth",0,1000,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("PixelHeight",0,647,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalWidth",0,26455,com.sun.star.beans.PropertyState.DIRECT_VALUE),Array("LogicalHeight",0,17116,com.sun.star.beans.PropertyState.DIRECT_VALUE))
args1(3).Name = "SelectionOnly"
args1(3).Value = false

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


end sub

我在这里遗漏了什么?

我正在使用以下版本的 LibreOffice:

Version: 7.4.0.3 / LibreOffice Community
Build ID: 40(Build:3)
CPU threads: 8; OS: Linux 5.19; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
7.4.0-3
Calc: threaded

答案1

正如我所说,需要几行额外的代码来设置分辨率:

Sub StoreEachPageToPNG()
Dim oDoc As Variant, oViewCursor As Variant
Dim iPageCount As Long, iPage As Long 
Dim sOriginalURL As String, sFileURL As String 
Dim pngFilterData(1) As New com.sun.star.beans.PropertyValue
    pngFilterData(0).Name = "PixelWidth"
    pngFilterData(0).Value = 1634 ' or as much as you need
    pngFilterData(1).Name = "PixelHeight"
    pngFilterData(1).Value = 2114 ' take care to maintain proportions
Dim Args(1) As New com.sun.star.beans.PropertyValue
    Args(0).Name = "FilterName"
    Args(0).Value = "writer_png_Export"
    Args(1).Name = "FilterData"
    Args(1).Value = pngFilterData
    
    oDoc = ThisComponent
    iPageCount = oDoc.getCurrentController().PageCount
    oViewCursor = oDoc.getCurrentController().getViewCursor()
    sOriginalURL = oDoc.getURL()
    oViewCursor.jumpToFirstPage()
    For iPage = 1 To iPageCount
        sFileURL = Replace(sOriginalURL, ".odt", " - " & Format(iPage, "000") & ".png")
        oDoc.StoreToURL(sFileURL, Args)
        If Not oViewCursor.jumpToNextPage() Then End
    Next iPage
End Sub

增加 PixelWidth 和 PixelHeight 的值时要小心,不要贪多——输出文件的大小会快速增加!

当然,这个代码还远远不够完美——比如,这个宏没有检查当前文档是否有名称(文件是否已保存)、是否有权限将文件写入目标文件夹、目标名称的文件是否已经存在且未被锁定等。

相关内容