Java – Could “content” of AttributedString can be saved in file

graphicsjavaswing

I am doing some research on how to "draw"
some attributed text on Graphics2D.

So, i am interested is it possible to save
content of AttributedString in some format ?

I know it could be Java serialized,
but, i don't need that solution here.

Also, if somebody knows some example which shows
how to edit AttributedString ?

Here is some Java code, just to get idea:

public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

AttributedString as new AttributedString( "Lorem ipsum dolor sit amet..." );
Font font1 = new Font( "SansSerif" , Font.PLAIN , 20 );
as.addAttribute( TextAttribute.FONT       , font1       );
as.addAttribute( TextAttribute.FOREGROUND , Color.black );
as.addAttribute( TextAttribute.FOREGROUND , Color.blue , 4 , 9 );

AttributedCharacterIterator aci = as.getIterator();
FontRenderContext           frc = g2.getFontRenderContext();
LineBreakMeasurer           lbm = new LineBreakMeasurer( aci , frc );

TextLayout  textLayout = lbm.nextLayout( wrappingWidth );

int x = 50 , y = 50 ;
textLayout.draw( g2 , x , y );

}

Thanx for any help or advice 🙂

Best Solution

Obviously, the simplest method is java serialization, otherwise you will need to handle all the possible text attributes. If you want an example, here is a report generator that I have written and that make extensive use of AttributedString. Hope it will help: http://www.perry.ch/mo/pureport.zip