I have a rtf file which has some text with bullets as shown in the screenshot below
I want to extract the data along with the bullets but when I print in the console, I get junk values. How do I print exactly the same from console. The way I tried is as below
public static void main(String[] args) throws IOException, BadLocationException {
RTFEditorKit rtf = new RTFEditorKit();
Document doc = rtf.createDefaultDocument();
FileInputStream fis = new FileInputStream("C:\\Users\\Guest\\Desktop\\abc.rtf");
InputStreamReader i =new InputStreamReader(fis,"UTF-8");
rtf.read(i,doc,0);
System.out.println(doc.getText(0,doc.getLength()));
}
Console output:
I assumed junk values are due to console not supporting chareset so I tried to generate a pdf file but in pdf also I get the same junk values. this is the pdf code
Paragraph de=new Paragraph();
Phrase pde=new Phrase();
pde.add(new Chunk(getText("C:\\Users\\Guest\\Desktop\\abc.rtf"),smallNormal_11));
de.add(pde);
de.getFont().setStyle(BaseFont.IDENTITY_H);
document.add(de);
public static String getText() throws IOException, BadLocationException {
RTFEditorKit rtf = new RTFEditorKit();
Document doc = rtf.createDefaultDocument();
FileInputStream fis = new FileInputStream("C:\\Users\\Guest\\Desktop\\abc.rtf");
InputStreamReader i =new InputStreamReader(fis,"UTF-8");
rtf.read(i,doc,0);
String output=doc.getText(0,doc.getLength());
return output;
}
Despite what you said, my guess is that it is a console encoding problem.
Anyway you can easily check it:
Just replace this line:
System.out.println(doc.getText(0,doc.getLength()));
With these 2 lines :
PrintStream ps = new PrintStream(System.out, true, "UTF-8");
ps.println(doc.getText(0,doc.getLength()));
This will force console encoding to UTF-8.
If it is still wrong, I would suspect your file is not fully rtf-compliant.
I made some tests and your code works well (the console one, I did not try the pdf) under Linux, but the console is natively in UTF-8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With