Generate RTF files

Hey guys,

can anyone tell me, how to generate RTF files? iText dropped support for this format. And it looks like you can handle RTF files with pure Java. I found the RTFEditorKit. But I couldn’t find any good documentation, that actually tells me, how to use it. I would like to use it as follows.

  • Create the document
  • add styled text (left/center/right/justify aligned, bold, italic, underlined, font, colors)
  • insert page brakes
  • read current page number

I don’t know, if RTF supports page headers and footers. If it does, I would like to add them, too.

What is the best way to do this?

Marvin

Better stop myself from typing a bunch of stuff and ask the most important question,

Why (the hell) would you need RTF in this day and age?

RTF Wiki
There is a list of links to the specifications for the various versions near the bottom.
I used a subset of it once years ago as an export format, but you can do so much layout with HTML these days, it’s difficult to see the need to carry on with RTF. Only a subset is defined in Java and apparently not very well.
Java RTF Bug Report
Another bug report listing not-implemented features

I think headers and footers are not supported.

Thanks for the answers.

We want to provide a report download in a web application which can be directly opened with M$ Word (yes, I know ;)). And we don’t want to use PIO (for this).

Do you suggest to create the RTF source directly? Well this might be doable. But this really should be done behind the scenes on a nice API.

Marvin

If the export format doesn’t have to be WYSIWYG…
I’d be tempted to generate a report template in word, with tag’s where the content should go, save that as RTF and use it as a template. If the report format is fixed and you just need to put text into it, this is easy. If you need to dynamically generate tables, there’s a bit more work, especially to control layout.
You will still need to escape special characters of course.

No, nothing about WYSIWYG. I want to generate the RTF purely through coding. And I cannot use a template. I really only need to do, what I scatched up above.

Marvin

A cunning tip I picked up from someone is that if you name an HTML file with a .doc extension then Word will happily open it. Of course, HTML doesn’t include syntax for headers and footers.

There’s as noted, html, pdf (but I take it open in word means open and edit it) and we have ODF and ooxml of which there should be api’s around,
http://www.javadocx.com/features which lists an lgpl version (no idea about the quality)

and as mentioned taking a premade document and adding some string replace sprikles also works wonders