| View previous topic :: View next topic |
| Author |
Message |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
Posted: Sun Oct 01, 2006 12:08 pm Post subject: load HTML into writer document |
|
|
Some times there is the need to insert a HTML into a writer document ( http://www.oooforum.org/forum/viewtopic.phtml?t=44599 , http://www.oooforum.org/forum/viewtopic.phtml?t=44456 ).
The below routine fulfills that purpose. The procedure is as follows:
(i) Write the HTML to a Storage, similar to http://www.oooforum.org/forum/viewtopic.phtml?t=43637
(ii) create a new document by loadComponentFromUrl, accessing this storage
(iii) select all of the new document
(iv) copy the selection into the original document at the view cursor position. Note that it is not necessary to use the system clipboard, when copying, because the Transferables of both controllers are accessed
Put this into a writere doc, and let me know, when problems occur ...
ms777
The main routine:
| Code: | sub TestInsertHTML
s = ""
s = s & "<P></P>"
s = s & "<P>Paragraph1</P>"
s = s & "Hello from ms777"
s = s & "<b>Bold</b>"
s = s & "<H1>Header 1</H1>"
s = s & "<P>Paragraph2</P>"
s = s & "<CENTER>Center</CENTER>"
s = s & "<TABLE>"
s = s & "<TR><TD bgcolor=#336699>Cell 1</TD><TD bgcolor=#663399>Cell 2</TD></TR>"
s = s & "<TR><TD bgcolor=#123456>the third cell</TD><TD bgcolor=#654321>Cell 4</TD></TR>"
s = s & "</TABLE>"
call InsertHTML2Writer(ThisComponent, s)
end sub |
The sub, which does all the work:
| Code: | 'here the subroutine starts
private boFinished as Boolean
sub InsertHTML2Writer(oDoc as Object, sHTML as String)
'oDoc contains the writer document, into which we want to insert the HTML
oContr = oDoc.CurrentController
'create a temporary storage
oStorageFac = createUnoService("com.sun.star.embed.StorageFactory")
oStorage = oStorageFac.createInstance
oStream = oStorage.openStreamElement("ms777", com.sun.star.embed.ElementModes.READWRITE)
'now write the HTML String to the stream
oTextOutputStream = createUNOService ("com.sun.star.io.TextOutputStream")
oTextOutputStream.setOutputStream(oStream)
oTextOutputStream.writeString(sHTML)
oTextOutputStream.flush
oTextOutputStream.closeOutput
'register a global event listener, because
'we have to wait until loading has finished
oGlob = createUnoService("com.sun.star.frame.GlobalEventBroadcaster")
oListener = CreateUnoListener( "EvList_","com.sun.star.document.XEventListener" )
oGlob.addEventListener( oListener )
boFinished = false
'create the to-be-inserted doc (=doc1) from the stream
Dim aProps(2) as new com.sun.star.beans.PropertyValue
aProps(0).Name = "FilterName"
aProps(0).Value = "HTML (StarWriter)"
aProps(1).Name = "InputStream"
aProps(1).Value = oStream
aProps(2).Name = "Hidden"
aProps(2).Value = true
oDoc1 = StarDesktop.loadComponentFromURL("private:stream", "_default", 0, aProps)
'wait until loading has finished
k=100
while (k>0) and (not boFinished)
k=k-1
wait(50)
wend
oGlob.removeEventListener( oListener )
'select all of doc1
oContr1 = oDoc1.CurrentController
oVC1 = oContr1.ViewCursor
oVC1.gotoStart(false)
oVC1.gotoEnd(true)
'insert the selection of doc1 into doc
'note that it is not necessary to involve the clipboard
'for copying
oContr.insertTransferable(oContr1.Transferable)
oDoc1.close(true)
'redraw the ComponentWindow, because sometimes the changes are not
'immediately reflected
oCompWin = oContr.Frame.ComponentWindow
with com.sun.star.awt.InvalidateStyle
oCompWin.invalidate(.CHILDREN+.UPDATE+.NOCLIPCHILDREN)
end with
End Sub
Sub EvList_notifyEvent( o as object )
if o.EventName = "OnLoadFinished" then
if o.Source.CurrentController.Frame.Title = " - OpenOffice.org Writer" then
boFinished = true
endif
endif
end sub
Sub EvList_disposing()
End Sub |
|
|
| Back to top |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
Fernand OOo Enthusiast

Joined: 22 Jun 2004 Posts: 142
|
Posted: Wed Oct 18, 2006 2:06 am Post subject: |
|
|
Fine peace of code !
When putting some styles into the HTML-code they comes as OO-styles in the final doc, but only the styles who are used in the body, not all-styles in the <style> tag.
When opening a html doc with the OO-html-import filter ALL styles are imported, when copying (what thos code is doing) whe only import the USED styles !
I try to findout if we can "load styles" from the "storage" or from a other Doc.
| Code: |
sub TestInsertHTML
s = "<style media=""all"" type=""text/css"">"
s = s & "a.homelinkgrijsvet:visited {font-family:Arial,sans-serif;font-weight:bold;font-size:11.333334px;color:#333333;background-color:transparent;text-decoration:none}"
s = s & "p.homeagenda {margin-left:11.451969px;margin-right:0;margin-top:3.817323px;margin-bottom:7.5968504px;border:none;padding:0;text-indent:-7.672441px;font-family:Arial,sans-serif;font-size:11.333334px;color:#333333}"
s = s & "p.hometitel {margin-left:3.817323px;margin-right:0;margin-top:3.817323px;margin-bottom:3.817323px;border:none;padding:0;text-indent:0;text-align:center;font-family:Arial,sans-serif;font-weight:bold;font-size:11.333334px;color:#990000}"
s = s & "</style>"
s = s & "<P></P>"
s = s & "<p class=""homeagenda"">• Weervast staal</p>"
s = s & "<p class=""hometitel"">• RVS als dakbedekkingsmateriaal</p>"
s = s & "<p class=""hometitel"">• Spanplafonds</p>"
s = s & "<p class=""hometitel""><span class=""homekleur"">Volgende editie</span> (nr.91) - verschijnt 10/11:</p>"
s = s & "<p class=""hometitel"">• Weervast staal</p>"
s = s & "<p class=""hometitel"">• RVS als dakbedekkingsmateriaal</p>"
s = s & "<p class=""hometitel"">• Spanplafonds</p>"
s = s & "<P>Paragraph1</P>"
s = s & "Hello from ms777"
s = s & "<b>Bold</b>"
s = s & "<H1>Header 1</H1>"
s = s & "<P>Paragraph2</P>"
s = s & "<CENTER>Center</CENTER>"
s = s & "<TABLE>"
s = s & "<TR><TD bgcolor=#336699>Cell 1</TD><TD bgcolor=#663399>Cell 2</TD></TR>"
s = s & "<TR><TD bgcolor=#123456>the third cell</TD><TD bgcolor=#654321>Cell 4</TD></TR>"
s = s & "</TABLE>"
call InsertHTML2Writer(ThisComponent, s)
end sub
|
|
|
| Back to top |
|
 |
ms777 Super User


Joined: 07 Feb 2004 Posts: 1355
|
Posted: Fri Oct 20, 2006 3:50 pm Post subject: |
|
|
have you tried making a full HTML document of your string ? You left out, as well as I did, all the header and footer <HTML> <HEAD>......
I do not see any reason why loading from string, with the mechanism shown, should be any different than loading from file, if the characters read are the same ... |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|