[Solved] Insert and Crop an Image in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
dlittlejohn
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm

[Solved] Insert and Crop an Image in Writer

Post 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())
Last edited by Hagar Delest on Mon Sep 04, 2017 10:32 pm, edited 1 time in total.
Reason: tagged [Solved].
Open Office 4 on Redhat 7
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Insert and Crop an Image in Writer

Post 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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
dlittlejohn
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm

Re: Insert and Crop an Image in Writer

Post 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?
Open Office 4 on Redhat 7
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Insert and Crop an Image in Writer

Post 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
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
hubert lambert
Posts: 145
Joined: Mon Jun 13, 2016 10:50 am

Re: Insert and Crop an Image in Writer

Post 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.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
dlittlejohn
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm

Re: Insert and Crop an Image in Writer

Post 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
Open Office 4 on Redhat 7
Post Reply