[Solved] search macro [DRAW]

Creating a macro - Writing a Script - Using the API

[Solved] search macro [DRAW]

Postby umba » Tue Jun 05, 2018 10:21 am

Hello,
I have pdf document that I open in LibreOffice Draw. There are around 100 pages, almost similar. I need to insert image on page, if there is e.g string called (lets say) "EX". I have macro that insert image, so its not a problem, but I have a problem with insert that image on page that should be here ("EX").
I recorded macro that search for "EX":
Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Sub searchEX

rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue


args1(1).Name = "SearchItem.SearchString"
args1(1).Value = "ex"

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())



End Sub

It's working, selecting that string but when I'm running then macro "insert image" it appears on page 1 (I have called it index=0). Is it possible to get page index where is string "EX"

Or I have second thought, dont know it can works. When I run macro there pops MSGBox "write amount of pages".
Here is variable with amount of pages, lets say 'p_amount'.
So I have:
p_amount=p_amount-1
index=0
for index=0 To p_amount
{macro insertmage}
next index
Its filling all pages with that image. Is it possible to do loop something like that?

p_amount=p_amount-1
index=0
for index=0 To p_amount
"IF search for EX"
then
{macro insertmage}
end IF
next index

I hope you understood it, my english is very bad so its hard to explain what I mean correctly :knock:
Greetings!
Last edited by robleyd on Wed Jun 06, 2018 10:18 am, edited 1 time in total.
Reason: Tagged [Solved] [robleyd, Moderator]
On Windows 10: LibreOffice 5.4.4
umba
 
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: search macro [DRAW]

Postby Lupp » Tue Jun 05, 2018 12:57 pm

In fact I do not understand cleary. This is mainly about what you want to achieve finally.

Are you aware of the fact that the pages of the original pdf are represented as 'Slides' in Draw with standard names ('Slide 1' e.g.)?
In what way should your "ex" identify the correct slide? What if "ex" has multiple occurrences?
How should the position of the inserted image on its page be determined?
Is it ok for you that the new image will always be the logically last one on the slide even if is inserted above other shapes?
What "macro" do you intend to use for the insertion? Please post it.
Please attach an example pdf of the kind you are working with.
Are you aware of the fact that you cannot 'Save As...' the edited document as a pdf?
Do you intend to export it to pdf again?
On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Let's create a powerful UFO: United Free Office!
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1982
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: search macro [DRAW]

Postby umba » Tue Jun 05, 2018 2:30 pm

Thanks fot fast replay and sorry for incomprehensible post. Now I will try to explain this in detail.
For inserting image I use this:
Code: Select all   Expand viewCollapse view
p_amount=p_amount-1

index=0
for index = 0 To  p_amount
Folder = "C:\Users\....\Desktop\"
imagen = "image.png"
ImagenURL = convertToURL(Folder & imagen)
oImagen_obj = ThisComponent.createInstance("com.sun.star.drawing.GraphicObjectShape")
oImagen_obj.GraphicURL = ImagenURL
oSize = oImagen_obj.Size
oSize.Height = 3000
oSize.Width = 3000
oImagen_obj.Size = oSize
oPos = oImagen_obj.Position
oPos.X = 2000
oPos.Y = 3000
oImagen_obj.Position = oPos
oDP = ThisComponent.DrawPages.getbyIndex(index)
oDP.add(oImagen_obj)

next index


I cant upload original pdf, but I just changed text for 'XXXX' , so the template is the same. Pdf has only 4 pages, because changing around 50-100 pages would take me years. In this PDF can be for example 15 pages with only ONE "EX" on page and 4 pages with TWO and MORE "EX" on page and also pages can be without "EX". I inserted "image" on pages where it should be in attached PDF.

Are you aware of the fact that you cannot 'Save As...' the edited document as a pdf?
Do you intend to export it to pdf again?

Hmm, Always I used to press "Export as PDF" and it worked, but I dont need this If I cant.

Is it ok for you that the new image will always be the logically last one on the slide even if is inserted above other shapes?

It doesn't matter.

In what way should your "ex" identify the correct slide? What if "ex" has multiple occurrences?

If on page would be TWO or MORE "EX" then I need just one image like in the attached PDF, but if it is not possible, and there will be as many images as "EX" on page but in the same place, then I think this should be okay too.
Attachments
eg.pdf
(31.16 KiB) Downloaded 60 times
On Windows 10: LibreOffice 5.4.4
umba
 
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: search macro [DRAW]

Postby Lupp » Wed Jun 06, 2018 12:09 am

You may try the attached demo. I made it with LibO 5.4.4. and did a short test with AOO 4.1.5.
It's experimental!
Attachments
aoo93817MatchDrawpagesAgainstSearchString_eg_1.odg
(18.25 KiB) Downloaded 25 times
On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Let's create a powerful UFO: United Free Office!
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1982
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: search macro [DRAW]

Postby umba » Wed Jun 06, 2018 10:02 am

Wow, it's working perfectly!
Code: Select all   Expand viewCollapse view
Sub insertImage(pDoc as object, pPage As Long)
thePage = pDoc.DrawPages(pPage-1)
pDoc.CurrentController.Select(thePage)
Print "Insert here!"
End Sub

I just change "Print "Insert here!" to:
Code: Select all   Expand viewCollapse view
Folder = "C:\Users\...\Desktop\Logo\"

imagen = "EX.png"
ImagenURL = convertToURL(Folder & imagen)
oImagen_obj = ThisComponent.createInstance("com.sun.star.drawing.GraphicObjectShape")

oImagen_obj.GraphicURL = ImagenURL
oSize = oImagen_obj.Size
oSize.Height = 3000
oSize.Width = 3000
oImagen_obj.Size = oSize
oPos = oImagen_obj.Position
oPos.X = 8500
oPos.Y = 3000
oImagen_obj.Position = oPos

oDP = ThisComponent.DrawPages.getByIndex(pPage-1)
oDP.add(oImagen_obj)

and it's working! :bravo:

Thank you very much!
On Windows 10: LibreOffice 5.4.4
umba
 
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 10 guests