OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

load HTML into writer document

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sun Oct 01, 2006 12:08 pm    Post subject: load HTML into writer document Reply with quote

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
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Mon Oct 02, 2006 9:40 am    Post subject: Reply with quote

Thank you ms777. I will have a look later.
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
Fernand
OOo Enthusiast
OOo Enthusiast


Joined: 22 Jun 2004
Posts: 142

PostPosted: Wed Oct 18, 2006 2:06 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Fri Oct 20, 2006 3:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
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