Libre Office 宏以裁剪图像

Posted

技术标签:

【中文标题】Libre Office 宏以裁剪图像【英文标题】:Libre Office Macro to crop image 【发布时间】:2017-08-16 16:44:46 【问题描述】:

我有一个 Libre Office 宏,我需要裁剪图像,但我找不到任何有用的文档或示例。任何人都有提示如何做到这一点?

dim noArgs()
dim emptyDocComponent as object
dim document as object
dim dispatcher as object
emptyDocComponent = StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, noArgs())
frame = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FileName"
args1(0).Value = "file://" & inputPath
args1(1).Name = "FilterName"
args1(1).Value = "<All formats>"
args1(2).Name = "AsLink"
args1(2).Value = false
args1(3).Name = "Style"
args1(3).Value = "Graphics"

dispatcher.executeDispatch(frame, ".uno:InsertGraphic", "", 0, args1())

selection = ThisComponent.CurrentSelection
If selection.ImplementationName <> "SwXTextGraphicObject" Then
   Exit Sub
End If

' this is what the macro recorder captured, but it was "rem" and non-functional
rem dispatcher.executeDispatch(document, ".uno:Crop", "", 0, Array())

***编辑

这是我现在使用的,以防它帮助其他人。我有一张需要裁剪的像素大小已知的图片。不完全确定计算是否完全准确,但到目前为止它是有效的。

dim noArgs()
dim emptyDocComponent as object
dim document as object
dim dispatcher as object
emptyDocComponent = StarDesktop.LoadComponentFromUrl("private:factory/swriter", "_blank", 0, noArgs())
frame = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name = "FileName"
args1(0).Value = "file://" & inputPath
args1(1).Name = "FilterName"
args1(1).Value = "<All formats>"
args1(2).Name = "AsLink"
args1(2).Value = false
args1(3).Name = "Style"
args1(3).Value = "Graphics"

dispatcher.executeDispatch(frame, ".uno:InsertGraphic", "", 0, args1())

selection = ThisComponent.CurrentSelection
If selection.ImplementationName <> "SwXTextGraphicObject" Then
   Exit Sub
End If

' size = (pixels / pixelsPerInch) * mm/in * scaling * actual graphic / displayed graphic
imageWidth = (int(pixelWidth) / int(xPixelsPerInch)) * 25.4 * 110 * (selection.actualSize.Width / selection.Width)
imageHeight = (int(pixelHeight) / int(yPixelsPerInch)) * 25.4 * 110 * (selection.actualSize.Height / selection.Height)

GraphicCrop = selection.GraphicCrop
GraphicCrop.Top = selection.actualSize.Height - imageHeight
GraphicCrop.Bottom = 0
GraphicCrop.Left = 0
GraphicCrop.Right = selection.actualSize.Width - imageWidth
selection.GraphicCrop = GraphicCrop

【问题讨论】:

【参考方案1】:

TextGraphicObject 有一个名为GraphicCrop 的结构。

以下代码改编自https://forum.openoffice.org/en/forum/viewtopic.php?f=25&t=72496。

selection = ThisComponent.CurrentSelection
If selection.ImplementationName <> "SwXTextGraphicObject" Then
   Exit Sub
End If

pxPerInch = 100*25.6
cropFig = selection.GraphicCrop
cropFig.Left = 0.27*pxPerInch
cropFig.Right = 1.34*pxPerInch
cropFig.Top = 0.31*pxPerInch
cropFig.Bottom = 0.18*pxPerInch
selection.GraphicCrop = cropFig

【讨论】:

这是我用的。谢谢!【参考方案2】:

在网上搜索“DannyB”(与OpenOffice 结合使用),在Drawcomponent 的StarBasic 宏上找到他非常有用的库。

我很确定他有一个例子。

要研究的其他资源:Andrew Pitonyak 的“OpenOffice Macros Explained”,也可能有一个示例。

【讨论】:

我搜索了 DannyB 的库,找到了 sourceforge.net/projects/ooomacros/files/… 和 wiki.openoffice.org/wiki/Danny%27s_Python_Modules,但没有找到任何可以裁剪的东西。在这种情况下,安德鲁的文件也没有帮助。也许你可以提供一个具体的链接。 不,吉姆。如果可以的话,我会做到的。我上次在 oooforum.org 上阅读 DannyB 的示例大约是 10 年前,我不记得有任何具体的

以上是关于Libre Office 宏以裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章

使用 MS Office 图片管理器从 JPEG 图像中裁剪颜色

Nathive——libre软件图像编辑器

[Libre|Open]Office 中当前文档的路径

在 Open/Libre/Neo Office 中查找文本选择中的段落的宏

Firebase 存储规则,Libre Office contentTypes

Libre Office 配置文件 bootstrap.ini 损坏