Free Support Forum - aspose.com

Empty TXT file throws UnsupportedFileFormatException using .NET

Hello,
I’ve noticed that if I try to convert a TXT file which contains only whitespaces, Aspose.Word is throwing the following exception:

Aspose.Words.UnsupportedFileFormatException: Unsupported file format: Unknown

Here’s a sample file causing that exception:
whitespaces.zip (133 Bytes)

The sample code to reproduce it:

var doc = new Document(“whitespaces.txt”);

@grzegorz.brose

Please use the following code example to avoid the shared exception. Hope this helps you.

TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.LoadFormat = LoadFormat.Text;
Document doc = new Document(MyDir + "whitespaces.txt", loadOptions);

@tahir.manzoor
Thank you, the code indeed fixes the issue. Though now I have to code the special case for detecting all TXT files and handle them in a different way than everything else (e.g. DOCX files)… Are there any other formats that need to be handled in the similar way?

@grzegorz.brose

You are importing empty text file into Aspose.Words that does not contain any information about its format. This is the reason you need to specify load options. If you want to import HTML fragment (not whole HTML document) into Aspose.Words’ DOM, you need to use LoadOptions.LoadFormat.

If document contains information about its format type, there is no need to use LoadOptions.LoadFormat. Pleas let us know if you have any more queries.