[Risolto] Inserimento immagini

Creare una macro - Scrivere uno script - Usare le API
Rispondi
Nomen
Messaggi: 7
Iscritto il: sabato 13 novembre 2010, 23:07

[Risolto] Inserimento immagini

Messaggio da Nomen »

Salve a tutti,
dovrei riuscire ad inserire delle immagini dentro un testo in writer.
Credevo di essere riuscito a risolvere con questo codice che ho preso girovagando sul web:

Codice: Seleziona tutto

sub addimm
   oDoc = thisComponent
   oDrawPage = oDoc.getDrawPage()
   
   ' CHANGE ME !!!
   ' CHANGE ME !!!
   cFile = "/home/utente/cover.jpg"
   cUrl = ConvertToUrl( cFile )thisComponent
   
   ' Convert the URL into an internal URL within the document.
   ' If you comment out this line, then the shape that is created from the url
   '  will refer to the external graphic, which must always be present.
   cUrl = LoadGraphicIntoDocument( oDoc, cUrl, "Cash Cow" )
   ' Now the URL points to a graphic *inside* of the document's Zip file,
   '  rather than an external url.
   
   ' Create a GraphicObjectShape.
   oShape = MakeGraphicObjectShape( oDoc, MakePoint( 1500, 3000 ), MakeSize( 3000, 4000 ) )
   ' Add it to the drawing page.
   oDrawPage.add( oShape )
   ' Set its URL to a particular graphic.
   oShape.GraphicURL = cUrl 
end sub
' Given a URL to an external graphic resource,
'  load that graphic permanently into this drawing document,
'  and return a new URL to the internal resource.
' The new URL can be used in place of the old URL.
Function LoadGraphicIntoDocument( oDoc As Object, cUrl As String, cInternalName As String ) As String
   ' Get the BitmapTable from this drawing document.
   ' It is a service that maintains a list of bitmaps that are internal
   '  to the document.
   oBitmaps = oDoc.createInstance( "com.sun.star.drawing.BitmapTable" )
   
   ' Add an external graphic to the BitmapTable of this document.
   oBitmaps.insertByName( cInternalName, cUrl )
   
   ' Now ask for it back.
   ' What we get back is an different Url that points to a graphic
   '  which is inside this document, and remains with the document.
   cNewUrl = oBitmaps.getByName( cInternalName )
   
   LoadGraphicIntoDocument = cNewUrl
End Function 
Function MakePoint( ByVal x As Long, ByVal y As Long ) As com.sun.star.awt.Point
   oPoint = createUnoStruct( "com.sun.star.awt.Point" )
   oPoint.X = x
   oPoint.Y = y
   MakePoint = oPoint
End Function

Function MakeSize( ByVal width As Long, ByVal height As Long ) As com.sun.star.awt.Size
   oSize = createUnoStruct( "com.sun.star.awt.Size" )
   oSize.Width = width
   oSize.Height = height
   MakeSize = oSize
End Function

Function MakeRectangleShape( oDoc As Object,_
               Optional oPosition As com.sun.star.awt.Point,_
               Optional oSize As com.sun.star.awt.Size ) As com.sun.star.drawing.RectangleShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.RectangleShape" )
   If Not IsMissing( oPosition ) Then
      oShape.Position = oPosition
   EndIf
   If Not IsMissing( oSize ) Then
      oShape.Size = oSize
   EndIf
   MakeRectangleShape = oShape
End Function

Function MakeGraphicObjectShape( oDoc As Object,_
               Optional oPosition As com.sun.star.awt.Point,_
               Optional oSize As com.sun.star.awt.Size ) As com.sun.star.drawing.GraphicObjectShape
   oShape = oDoc.createInstance( "com.sun.star.drawing.GraphicObjectShape" )
   If Not IsMissing( oPosition ) Then
      oShape.Position = oPosition
   EndIf
   If Not IsMissing( oSize ) Then
      oShape.Size = oSize
   EndIf
   MakeGraphicObjectShape = oShape
End Function 
Però mi restano un paio di problemi che non riesco a risolvere:
1) L'immagine creata con questo codice non è come un immagine inserita da menu (in parole povere se doppioclicco sopra l'immagine del documento non mi apre la finestra di dialogo immagine dove, per esempio, si può decidere l'allineamento etc etc) e a me invece servirebbe proprio una di quel tipo.
2) Forse legato al punto uno non riesco a ricavare le dimensioni dell'imagine caricata per fare un resize giusto.

Spero che qualcuno possa darmi una dritta.

Ciao

PS: Uso ubuntu 10.10, Open office 3.2.1.
Ultima modifica di Nomen il sabato 19 marzo 2011, 14:25, modificato 1 volta in totale.
OpenOffice 4.1.0 su Windows 7
Nomen
Messaggi: 7
Iscritto il: sabato 13 novembre 2010, 23:07

Re: Inserimento immagini

Messaggio da Nomen »

Credo di aver risolto così:

Codice: Seleziona tutto

sub addImage(surl,odoc,otext)
   oCursor = oText.createTextCursor()
   'oCursor.gotoStart( False )
   oGraphic = oDoc.createInstance( "com.sun.star.text.GraphicObject" )
   oText.insertTextContent( oCursor, oGraphic, False )
   oGraphic.AnchorType = com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH
   oGraphic.GraphicURL = ConvertToURL( surl )
   'oGraphic.HoriOrientPosition = 5500
   'oGraphic.VertOrientPosition = 4200
   'oGraphic.Width = 4400
   'oGraphic.Height = 4000
   ImmaginiWeb
end sub
Sub ImmaginiWeb()
 oDPage = ThisComponent.DrawPage
 oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
 Dim oProps(0) as new com.sun.star.beans.PropertyValue
 oProps(0).Name  = "URL"
  If oDPage.hasElements() Then
   For I = 0 To oDPage.Count - 1
    oGraph = oDPage.getByIndex(I)
    If oGraph.supportsService("com.sun.star.text.TextGraphicObject") Then
     oProps(0).Value = oGraph.GraphicUrl
     oGraph.Graphic = oProvider.queryGraphic(oProps())
    End If
   Next I
 End If
End Sub
Ciao a tutti.
OpenOffice 4.1.0 su Windows 7
Rispondi