3

I have a Libre Office Macro and I need to crop an image, but I have been unable to find any helpful documentation or an example. Anyone have a tip how to do it?

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())

*** edit

Here is what I am now using in case it helps someone else. I had a picture with a known size in pixels that needed to be cropped. Not entirely sure the calculation is completely accurate, but it is working so far.

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
Dan Littlejohn
  • 1,329
  • 4
  • 16
  • 30

2 Answers2

2

TextGraphicObject has a struct called GraphicCrop.

The following code was adapted from 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
Jim K
  • 12,824
  • 2
  • 22
  • 51
-1

Search the net for "DannyB" (in combination with OpenOffice) to find his very useful libraries on StarBasic macros for the Drawcomponent.

I'm pretty sure he has an example.

Other resource to look into: Andrew Pitonyak's "OpenOffice Macros Explained", might have an example as well.

ngulam
  • 995
  • 1
  • 6
  • 11
  • I searched for DannyB's libraries and found https://sourceforge.net/projects/ooomacros/files/Danny_s%20Draw%20Power%20Tools/ and https://wiki.openoffice.org/wiki/Danny%27s_Python_Modules, but did not find anything for cropping. Nor was Andrew's document helpful in this case. Perhaps you could provide a specific link. – Jim K Aug 18 '17 at 00:25
  • No, Jim. If I could have I would have done it. Last time I read DannyB's examples was about 10 years ago in oooforum.org and don't remember any specific – ngulam Aug 18 '17 at 20:25