[Solved] [DRAW] searching image through pdf

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
umba
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

[Solved] [DRAW] searching image through pdf

Post by umba »

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

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
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [DRAW] searching image through pdf

Post by Lupp »

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 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
umba
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Post by umba »

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 117 times
On Windows 10: LibreOffice 5.4.4
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [DRAW] searching image through pdf

Post by Lupp »

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 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
umba
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: [DRAW] searching image through pdf

Post by umba »

Oh geez, haha :knock: . Thanks for reply! I will try something later.
On Windows 10: LibreOffice 5.4.4
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [DRAW] searching image through pdf

Post by Lupp »

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 151 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
umba
Posts: 7
Joined: Tue Jun 05, 2018 9:56 am

Re: [DRAW] searching image through pdf

Post by umba »

It's working. Thank yoou very much!
On Windows 10: LibreOffice 5.4.4
Post Reply