aspose file tools*
The moose likes I/O and Streams and the fly likes How to save a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to save a "Styled" document" Watch "How to save a "Styled" document" New topic
Author

How to save a "Styled" document

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hi all,

I have created a small application for editing text using StyledDocument and JTextPane. Now, you can set the font size, font colour, and other font related stuff. My question is, how could I save this document? What is the format? When I reopen this document, all the decorations I have done to the file should be there. How do I do this? Please help.


Are you better than me? Then please show me my mistakes..
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

When I did this several years ago, I saved it as XML. The model for a styled document does have a tree structure based on javax.swing.text.Element, so it isn't terribly hard to convert that tree structure into an equivalent XML tree structure. I start by getting the root of the model's tree:



and then I recursively convert that to an XML document. Elements in the tree become elements in the XML document, and attributes of the Elements become attributes of the XML elements, and likewise leaf nodes of the tree correspond to text nodes of the XML document. Of course there's a similar process to take the XML and rebuild the model of a styled document from it.

The process isn't very complicated but it does take some fiddling around with the details of javax.swing.text.Element, plus observing what you actually get out of a styled document.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Paul Clapham wrote:When I did this several years ago, I saved it as XML. The model for a styled document does have a tree structure based on javax.swing.text.Element, so it isn't terribly hard to convert that tree structure into an equivalent XML tree structure. I start by getting the root of the model's tree:



and then I recursively convert that to an XML document. Elements in the tree become elements in the XML document, and attributes of the Elements become attributes of the XML elements, and likewise leaf nodes of the tree correspond to text nodes of the XML document. Of course there's a similar process to take the XML and rebuild the model of a styled document from it.

The process isn't very complicated but it does take some fiddling around with the details of javax.swing.text.Element, plus observing what you actually get out of a styled document.


Thanks for the quick reply. I really appreciate it. Anyway, I found a format called ".rtf". Seems like some has used it without any issue. What about it?
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Yep, Thats the way..

Use RTFEditorKit in java. It will take care of everything..
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

Hey all,

Just wanna tell you that RTFEditorKit provided by Java doesn't support alignments, images, and some other styles. You can resolve this by downloading "AdvancedRTFEditorKit", and "jai_codec" image rendering packages. They are not from Java, but from Java-sl site
Henry Wessex
Greenhorn

Joined: Aug 14, 2014
Posts: 1
Hi,

Can anyone show a code example, how can you save the styledDocument as an XML file?

Thanks
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2289
    
  49
I did a quick search and found this thread on the Oracle forums where someone has provided some code:
https://community.oracle.com/message/4994743

I've no idea if it works though.
 
Consider Paul's rocket mass heater.
 
subject: How to save a "Styled" document