[Solved] [DRAW] searching image through pdf

Creating a macro - Writing a Script - Using the API

[Solved] [DRAW] searching image through pdf

Postby umba » Fri Jun 08, 2018 1:25 pm

Hello,
is it possible to do macro in DRAW that search images through PDF?

or maybe i the way to edit this macro written by Lupp in this forum?
Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

Function listMatchingPages(Optional pDoc as Object, Optional pSearch As String, _
                           Optional pCtrl As Long) As String REM pCtrl=1 is CaseSensitive
listMatchingPages = "fail"
If IsMissing(pDoc)    Then pDoc    = ThisComponent
If IsMissing(pSearch) Then pSearch = "ex"
If IsMissing(pCtrl)   Then pCtrl   = 0
n = pDoc.DrawPages.Count
REM === From an unknown source. ===
REM I do not understand the working of this search procedure.
REM Drawings don't work with the XSearchable interface.
REM The UI and the dispatcher know a way to search the shapes of a drawing for text nonetheless.
REM The options are restricted, however.
dim ccF   as object
dim dispatcher as object
ccF   = pDoc.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "SearchItem.SearchString"
args1(0).Value = pSearch
REM === End ===
h = ""
For j = 1 To n
    On Error Goto nextj
    dispatcher.executeDispatch(ccF, ".uno:ExecuteSearch", "", 0, args1())
    k = 0
    f = pDoc.CurrentSelection
    If IsNull(f) Then Goto nextj
    If (pCtrl=0) OR (f.String=pSearch) Then
        k = f.Text.Parent.Number
        h = h & k & ","
    End If
nextj:
Next j
r = ""
For j = 1 To n
    If Instr("," & h, "," & j & ",")>0 Then
        r = r & j & ","
    End If
Next j
If Len(r)>0 Then r = Left(r, Len(r) - 1)
listMatchingPages = r
End Function

Sub insertImage(pDoc as object, pPage As Long)
thePage = pDoc.DrawPages(pPage-1)
pDoc.CurrentController.Select(thePage)
Print "Insert here!"
End Sub

Sub doIt()
list = listMatchingPages(ThisComponent, "EX", 1)
Print list
splitList = Split(list, ",")
For j = 1 To Ubound(splitList)+1
    insertImage(ThisComponent, CInt(splitList(j-1))
Next j
End Sub


PDF has many pages, on several pages is that image on rest is not. If on page is that image I'm searching for I want to insert new image. I know that on every page is only ONE image(that I'm searching for), I know the name of this image and position too if it could help. Is it possible?
Thanks!
Last edited by robleyd on Tue Jun 12, 2018 9:29 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: [DRAW] searching image through pdf

Postby Lupp » Fri Jun 08, 2018 11:17 pm

Things may depend on the source of the pdf.
A pdf opened with LibreOffice or with AOO behaves as a "com.sun.star.drawing.DrawingDocument".
I don't know an example of YOUR pdf. (Please always attach example files in such cases!)
A DrawingDocument generated with LibO/AOO having an image (.jpg e.g.) inserted and embedded does not contain a name of that image except for a pseudo URL generated in an obscure way. Only images inserted as links without embedding them are associated with a shape knowing the link URL and can be identified by it (all the disacvantages aside).

As I do not know anything about specifications or guidelines concerning the implementation of the insertion and the embedding of images (graphical objects) I cannot tell without time-conuming experiments whether the same picture inserted in different places or on different pages will always get the same pseudo-URL. I would assume, however, that an image that was inserted and then copied and pasted without changes to additional positions while the document was continuously open should use always the same internal storage.
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: 1999
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Postby umba » Mon Jun 11, 2018 7:39 am

Here is that file. There are only 4 pages, because file was too big to upload. Also I had to delete some lines because personal information thats why pages are a litle empty. This image what I was talking about is that litle "prime" on page 2 and 3. PDF was made by PDFcreator, thats why text is as a bezier cuvre I think.
Attachments
eg.odg
(127.27 KiB) Downloaded 16 times
On Windows 10: LibreOffice 5.4.4
umba
 
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: [DRAW] searching image through pdf

Postby Lupp » Mon Jun 11, 2018 11:57 am

If your concern is about pages containing any graphic, you may go through all the shapes contained in the current page (recursively ungrouping them as I demonstrated earlier if needed) and ask them for a property theShape.GraphicURL. A shape having that property is a graphic.

If it is about the repeated occurrence of a specific graphic, you need to first identify the .GraphicURL for an example, and then compare it to the .GraphicURL found elsewhere.

In the above attached example I found (visually) two graphics, both being embedded and identical concerning the original graphic file, and therefor having the same internal URL:
vnd.sun.star.GraphicObject:100000000000009400000029371D081F7C9CE67E

I should remind you of the fact that I do not know if the same graphic embedded in a different 'Draw' document can be expected to get the same GraphicURL.

Editing (See strike-out above.):
Sorry. I was in error about the demo I gave in your other thread. I also have a "macro" for the recursive inspection of shapes, but I cannot immediately find the time to reactivate and adapt it.
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: 1999
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [DRAW] searching image through pdf

Postby umba » Mon Jun 11, 2018 1:18 pm

Oh geez, haha :knock: . Thanks for reply! I will try something later.
On Windows 10: LibreOffice 5.4.4
umba
 
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: [DRAW] searching image through pdf

Postby Lupp » Mon Jun 11, 2018 3:14 pm

Well, there was some spare time for the experiment. See attachment.
(I didn't tidy up at all. There will be a lot of unused code copied over from a different demonstration and the used code is only superficially adapted.)

Just run the "macro" Standard.work.temp" contained in the attachment. You will see that the pages containing the logo (?) also contain a factually invisible graphic at the right and nearer to the top of the page. You may rework the code to get what you want.

I had to delete the fourth page to comply with the maximum upload size.
Attachments
aoo93859FindGraphicsInOdg_umba_eg_reworked.odg
(104.59 KiB) Downloaded 15 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: 1999
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [DRAW] searching image through pdf

Postby umba » Tue Jun 12, 2018 9:24 am

It's working. Thank yoou 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: Ciakimba, JeJe and 10 guests