Convert text to pdf

I tried to convert txt file to pdf
These are my codes:

Document doc = new Document(inputFile);
File file = new File(outputFile);
FileOutputStream fileOS = new FileOutputStream(file);
String s = System.getProperty("file.separator");
String fontPath = systemUtilService.getServletRunningPath() + s + "WEB-INF" + s + "resources" + s + "fonts" + s;
// Retrieve the array of environment-dependent font sources that are searched by default.
// For example this will contain a "Windows\Fonts" source on a Windows machines.
// We add this array to a new ArrayList to make adding or removing font entries much easier.
ArrayList fontSources = new ArrayList(Arrays.asList(FontSettings.getFontsSources()));
// Add a new folder source which will instruct Aspose.Words to search the following folder for fonts.
com.aspose.words.FolderFontSource folderFontSource = new com.aspose.words.FolderFontSource(fontPath, true);
// Add the custom folder which contains our fonts to the list of existing font sources.
fontSources.add(folderFontSource);
// Convert the Arraylist of source back into a primitive array of FontSource objects.
com.aspose.words.FontSourceBase[] updatedFontSources = (com.aspose.words.FontSourceBase[])fontSources.toArray(new com.aspose.words.FontSourceBase[fontSources.size()]);
// Apply the new set of font sources to use.
FontSettings.setFontsSources(updatedFontSources);
// FontSettings.setFontsFolder(fontPath, false);
doc.save(fileOS, com.aspose.words.SaveFormat.PDF);
fileOS.close();
result = true;

but I got the wrong output
The out put are garbled text.
Do I need to set encoding format when I load the input file?
If yes, How to set it ?

Hi there,

Thanks for your inquiry. The input text file shared in this forum thread has 0kb file size. Could you please re-attach it here for testing? We will investigate the issue on our side and provide you more information.

I have already uploaded it.

Hi there,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 16.2.0 and have not found any issue with output Pdf. Please use Aspose.Words for Java 16.2.0.

Please use LoadOptions.Encoding property to set the encoding that will be used to load a text document. You may modify the encoding according to your requirements. Following code example shows how to convert text to Pdf. Hope this helps you.

Charset charset = Charset.forName("GB2312"); 
LoadOptions loadOptions = new LoadOptions(); 
loadOptions.setLoadFormat(LoadFormat.TEXT);
loadOptions.setEncoding(charset); 
Document doc = new Document(MyDir + "183.txt", loadOptions);
doc.save(MyDir + "Out GB2312.pdf");