We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Unable to load text document with rtf extension

I have a file with an rtf extension. The file is usually a valid rtf, but there are instances where the file is not a valid rtf.

I can account for an empty file (With file.length) and create a new “empty” valid rtf.
The problem for me now is loading a file with an .rtf extension that contains plan text.

To duplicate, create file in notepad with any plain text contents named "file.rtf"
File file = new File(“file.rtf”);
Document doc = new Document(file.getAbsolutePath());

It throws the following exception
com.aspose.words.UnsupportedFileFormatException: Unknown file format.
at com.aspose.words.Document.b(Document.java:1279)
at com.aspose.words.Document.a(Document.java:1160)
at com.aspose.words.Document.(Document.java:148)

Hi Matthew,

Thanks for your request. I think FileformatUtil.DetectFileFormat is just what you need:


Best regards,

FileFormatInfo ffi = FileFormatUtil.detectFileFormat(originalPath);

ffi.getLoadFormat returns 255/unknown.

In my case, I can safely assume that if I can’t load the document, I should create a new one with the text contents of the file. How would I do that?

I’ve used the DocumentBuilder with some success inserting images, but I do not know how to insert/append text to an empty Document

To clarify
String text = "xyz<$#!"
How would I create a new empty document and append text as the document contents, ready to be saved as rtf/html?


Thanks for your request. I think, the code provide here can be useful for you:


Best regards,

Thank you. I haven’t tested yet, but I see the builder.writeln method. Thanks