B4J Question Insert Image to word

jesuslizonsoluciones

Member
Licensed User
Longtime User
Hi

I would like to know how to insert an image in docx document.

At this time, replace text with this procedure:





B4X:
Dim m As Map = CreateMap("jesus": "lizon")

    Dim doc As JavaObject = OpenDocx("C:\test","document.docx")
    Dim AllParagrpahs As List
    AllParagrpahs.Initialize
    Dim headers As List = doc.RunMethod("getHeaderList", Null)
    For Each header As JavaObject In headers
        AllParagrpahs.AddAll(header.RunMethod("getParagraphs", Null))
    Next
    AllParagrpahs.AddAll(doc.RunMethod("getParagraphs", Null))
    For Each p As JavaObject In AllParagrpahs
        Dim runs As List = p.RunMethod("getRuns", Null)
        If runs.IsInitialized Then
            For Each r As JavaObject In runs
                Dim text As String = r.RunMethod("getText", Array(0))
                If text <> Null Then
                    For Each key As String In m.Keys
                        If text.Contains("$"&key&"$") Then
                            r.RunMethod("setText", Array(""&m.Get(key)&"", 0))
                        End If
              
                    Next
                End If
            Next
        End If
    Next
    SaveDocument(doc, "C:\test","TEST2.docx")
 

xulihang

Active Member
Licensed User
Longtime User
B4X:
Dim img As B4XBitmap=content
        Dim imgPath As String=File.Combine(File.DirApp,"image.jpg")
        Dim out As OutputStream=File.OpenOutput(imgPath,"",False)
        img.WriteToStream(out,"100","JPEG")
        out.Close
        Dim ratio As Double=img.Width/img.Height
        Dim width As Int=Min(img.Width,100)*9525
        Dim height As Int=width/ratio
        Dim bytes() As Byte
        bytes=File.ReadBytes(File.DirApp,"image.jpg")
        Dim data As InputStream
        data.InitializeFromBytesArray(bytes,0,bytes.Length)
        Dim para As JavaObject = Cell.RunMethodJO("addParagraph",Null)
        Dim run As JavaObject
        run=para.RunMethodJO("createRun",Null)
        run.RunMethod("addPicture",Array(data,5,"image.jpg",width,height))
        data.Close

Use the addPicture method. Pay attention to the unit of width and height. It is not pixel
 
Upvote 0

GabrielM

Member
Licensed User
Longtime User
Hello xulihang
I am also trying to learn how to insert a JPG type image in a MSWord document: in the header or anywhere else on the page and I am failing at:
B4X:
B4J Version: 8.10
Java Version: 8
Parsing code.    Error
Error parsing program.
Error description: Undeclared variable 'cell' is used before it was assigned any value.
Error occurred on line: 115 (Main)
Dim para As JavaObject = Cell.RunMethodJO("addParagraph",Null)
Could you let me know how/where to declare the "Cell" object as in your code example, please?
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…