[Solved] Insert an image into Draw document using Basic

Keyboard macros or custom scripts

[Solved] Insert an image into Draw document using Basic

Postby cve60069 » Mon Feb 15, 2021 7:58 pm

Hello

I am wanting to insert an image into a LibreOffice Draw document using Basic. I create a Draw document and then I add a page to the document and then rename the pages. I want ti insert an image into Page1 and then add another Image to Page2. I have the pages created as I want but I am unable to insert the image into the page. Below is my code

Code: Select all   Expand viewCollapse view
sub InsertImage()
    Dim Doc As Object
    Dim Page1 as Object
    Dim Page2 as Object

    Dim DocPath1 as String
        DocPath1 = ConvertToURL("MyImage1.jpg")
    Dim DocPath2 as String
        DocPath2 = ConvertToURL("MyImage2.jpg")

    Dim noArgs() 'An empty array for the arguments
    Dim sURL As String

        sURL = "private:factory/sdraw"

        Doc = StarDesktop.LoadComponentFromUrl(sURL, "_blank", 0, noArgs())
        Page1 = Doc.DrawPages(0)
        Page1.Name = "Image1"

        Page2 = Doc.Drawpages.insertNewByIndex(2)
        Page2.Name = "Image2"

       '    Page1.FillStyle = com.sun.star.drawing.FillStyle.BITMAP
       '    Page1.FillBitmapURL = DocPath1
       
End sub

I have been reading Andrew Pitonyak's book but unable to find a source for what I am trying to do. FillStyle breaks the code.

Any tips please
Last edited by robleyd on Sat Feb 20, 2021 12:50 pm, edited 1 time in total.
Reason: Tagged Solved; add green tick
OpenOffice 3.4.1 on Windows 7
cve60069
 
Posts: 16
Joined: Fri Feb 15, 2013 12:31 pm

Re: Inset an image into an Open Office Draw document using B

Postby JeJe » Mon Feb 15, 2021 9:26 pm

Thread for inserting in Writer... don't know whether will be the same for draw...

viewtopic.php?f=25&t=45003
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1541
Joined: Wed Mar 09, 2016 2:40 pm

Re: Inset an image into an Open Office Draw document using B

Postby Lupp » Tue Feb 16, 2021 6:48 pm

Code: Select all   Expand viewCollapse view
Sub insertImageOriginalSizeLeftTopIntoNewSlide
imgPath = "...\" REM The folder path to your image file
imgName = "..."  REM The name of your image file
imgURL = ConvertToURL(imgPath & imgName)
doc = ThisComponent
dps = doc.DrawPages
nextDp = dps.InsertNewByIndex(dps.Count)
nextDp.Name = "extraPage" & nextDp.Number
imgSh = doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
imgSh.GraphicURL = imgURL
nextDp.add(imgSh)
sz = imgSh.Size
szO = imgSh.Bitmap.Size100thMM
With sz
  .Width = szO.Width
  .Height = szO.Height
End With
pos = imgSh.Position
With pos
  .X = nextDp.BorderLeft
  .Y = nextDp.BorderTop
End With
imgSh.Position = pos
imgSh.Size = sz
End Sub
On Windows 10: LibreOffice 7.1 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 3065
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Insert an image into Draw document using Basic

Postby cve60069 » Sat Feb 20, 2021 12:39 pm

I have got the program to work.

I created two pdf's and saved them in a folder. I run the macro MergePDF and the sub opens Draw, creates the two pages, copes the pdf's onto each page. The document then exports the two pdf's as one. Draw must be closed for this to work.

Code: Select all   Expand viewCollapse view
sub MergePDF()

    Dim Doc As Object 'This workbook

    Dim NewWorkBookURL As String

    NewWorkBookURL = "private:factory/sdraw"

    Dim noArgs() 'An empty array for the arguments

    Dim Point As New com.sun.star.awt.Point
    Dim Size As New com.sun.star.awt.Size

    Point.x = 0
    Point.y = 0
    'A4
    Size.Width = 21000
    Size.Height = 29700

    Dim Page1 As Object 'Excel sheet
    Dim Page2 As Object 'AutoCAD sheet

    Dim Image1 As Object 'PDF1
    Dim Image2 As Object 'PDF2

    Dim DocPath1 As String
    Dim DocPath2 As String
    Dim DocPath3 As String

    DocPath1 = ConvertToURL("C:\Users\pdf1.pdf")
    DocPath2 = ConvertToURL("C:\Users\\pdf2.pdf")
    'Write to pdf
    DocPath2 = ConvertToURL("C:\Users\\pdf2.pdf")

    Doc = StarDesktop.LoadComponentFromUrl(NewWorkBookURL, "_blank", 0, noArgs())

    Page1 = Doc.DrawPages(0)
    Page1.Name = "PDF1"

    Page2 = Doc.Drawpages.insertNewByIndex(2)
    Page2.Name = "PDF2"

   'Page 1 
    Image1 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    Image1.GraphicURL = DocPath1
   
    Image1.Size = Size
    Image1.Position = Point
    Page1.add(Image1)

    'Page 2
    Image2 = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    Image2.GraphicURL = DocPath2
   
    Image2.Size = Size
    Image2.Position = Point
    Page2.add(Image2)

    'ExportToPDF

    dim args2(2) as new com.sun.star.beans.PropertyValue
        args2(0).Name = "URL"
        args2(0).Value = DocPath3
        args2(1).Name = "FilterName"
        args2(1).Value = "calc_pdf_Export"

        Doc.storeToURL(DocPath3,args2())


    msgbox "Done"

    End sub


Very basic but works. Thanks for the other forum members for the advice on the way
OpenOffice 3.4.1 on Windows 7
cve60069
 
Posts: 16
Joined: Fri Feb 15, 2013 12:31 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 7 guests