[Solved] Insert and Crop an Image in Writer

Creating a macro - Writing a Script - Using the API

[Solved] Insert and Crop an Image in Writer

Postby dlittlejohn » Wed Aug 16, 2017 7:27 pm

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   Expand viewCollapse view
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
dlittlejohn
 
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm

Re: Insert and Crop an Image in Writer

Postby FJCC » Wed Aug 16, 2017 8:03 pm

Starting from the last functioning part of your code, I would write
Code: Select all   Expand viewCollapse view
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.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7314
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Insert and Crop an Image in Writer

Postby dlittlejohn » Wed Aug 16, 2017 8:39 pm

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
dlittlejohn
 
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm

Re: Insert and Crop an Image in Writer

Postby musikai » Wed Aug 16, 2017 9:37 pm

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/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 247
Joined: Wed Nov 11, 2015 12:19 am

Re: Insert and Crop an Image in Writer

Postby hubert lambert » Thu Aug 17, 2017 8:31 am

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   Expand viewCollapse view
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
hubert lambert
 
Posts: 132
Joined: Mon Jun 13, 2016 10:50 am

Re: Insert and Crop an Image in Writer

Postby dlittlejohn » Fri Aug 25, 2017 8:46 pm

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   Expand viewCollapse view
   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
dlittlejohn
 
Posts: 3
Joined: Wed Aug 16, 2017 7:19 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests