GroupDocs.Parser provides the functionality to detect the encoding of a plain text file. The following encodings are supported:
UTF32 LE
UTF32 BE
UTF16 LE
UTF16 BE
UTF8
UTF7
ANSI
Encoding is detected by BOM or by the content of the file (if BOM isn’t present).
Here are the steps to detect the encoding of the document:
Instantiate the LoadOptions object with the default ANSI encoding (a java.nio.charset.Charset object);
Instantiate the Parser object for the initial document;
Call the getDocumentInfo method and check that the result is a com.groupdocs.parser.options.TextDocumentInfo object (use java.instanceOf);
Call the getCharset() method.
The following example shows how to detect the encoding of the document:
constjava=require('java');constgroupdocs=require('@groupdocs/groupdocs.parser');constCharset=java.import('java.nio.charset.Charset');// Create an instance of LoadOptions class with the default ANSI encoding.
// This encoding is returned for ANSI text documents.
constloadOptions=newgroupdocs.LoadOptions(groupdocs.FileFormat.WordProcessing,null,null,Charset.forName('US-ASCII'));// Create an instance of Parser class
constparser=newgroupdocs.Parser('utf8.txt',loadOptions);try{// Get the document info
constinfo=parser.getDocumentInfo();// Check if it's the document info of a plain text document
if(!java.instanceOf(info,'com.groupdocs.parser.options.TextDocumentInfo')){console.log("Isn't a plain text document");}else{// Print the encoding
console.log('Encoding: '+info.getCharset().displayName());}}finally{parser.close();}process.exit(0);
For the UTF-8 sample file the example prints Encoding: UTF-8.
More resources
Free online document parser App
Along with the full-featured library we provide simple but powerful free apps.
You are welcome to extract data from PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our Free Online Document Parser App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.