Page 1 of 1
[Solved] Insert and Crop an Image in Writer
Posted: Wed Aug 16, 2017 7:27 pm
by dlittlejohn
I have a Libre Office Macro and I need to crop an image, but I have been unable to figure it out. Anyone have a tip how to do it?
Code: Select all
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())
Re: Insert and Crop an Image in Writer
Posted: Wed Aug 16, 2017 8:03 pm
by FJCC
Starting from the last functioning part of your code, I would write
Code: Select all
selection = ThisComponent.CurrentSelection
If selection.ImplementationName <> "SwXTextGraphicObject" Then
Exit Sub
End If
GrphCrop = selection.GraphicCrop
GrphCrop.TOP = 2500
GrphCrop.BOTTOM = 2500
GrphCrop.LEFT = 2500
GrphCrop.RIGHT = 2500
selection.GraphicCrop = GrphCrop
The units of the cropping seem to be 0.01 mm.
Re: Insert and Crop an Image in Writer
Posted: Wed Aug 16, 2017 8:39 pm
by dlittlejohn
Interesting, thanks! It works and is is removing parts of the image, but it is not preserving the aspect ratio or changing the image size.
Is there a way to keep the aspect ratio when croping with a macro?
Re: Insert and Crop an Image in Writer
Posted: Wed Aug 16, 2017 9:37 pm
by musikai
You have to compensate the cropped parts by changing the image size. I can't remember the exact code but you can find out quite a lot by investigating the code of my macro called pictool:
https://extensions.openoffice.org/en/pr ... er-pictool
Re: Insert and Crop an Image in Writer
Posted: Thu Aug 17, 2017 8:31 am
by hubert lambert
Hello,
dlittlejohn wrote:Is there a way to keep the aspect ratio when croping with a macro?
You may either, depending on your real need:
- add these two lines after FJCC's snippet:
Code: Select all
selection.Height = selection.Height - (GrphCrop.Bottom + GrphCrop.Top)
selection.Width = selection.Width - (GrphCrop.Left + GrphCrop.Right)
- change the crop values with negative ones.
Re: Insert and Crop an Image in Writer
Posted: Fri Aug 25, 2017 8:46 pm
by dlittlejohn
Appreciate the help. For posterity here is what I came up with. I had a known picture size in pixels that needed to be cropped. Not sure if my calculation is entirely accurate, but it is working so far.
Code: Select all
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