RTF Line count in Java

元气小坏坏 提交于 2019-12-11 13:29:33

问题


Kindly let me know any API to calculate the line count for RTF document.

Apache POI or Aspose works for document, but its not able to find line count for RTF.

Thanks.


回答1:


Java already has a built-in RTF-Parser: RTFEditorKit.

Take a look at its read method.

For example:

test.rtf file contents

hello

stackoverflow

users

So, it has 3 lines separated by \n.

Code:

FileInputStream stream = new FileInputStream("test.rtf");
RTFEditorKit kit = new RTFEditorKit();

Document doc = kit.createDefaultDocument();
kit.read(stream, doc, 0);
String plainText = doc.getText(0, doc.getLength());
System.out.println(plainText.split("\\n").length);

Output = 3




回答2:


You can use Aspose.Words for Java to get the number of lines of an RTF document. Please do the following:

  1. Read RTF file using document class
  2. Get BuiltInDocumentProperties object using getBuiltInDocumentProperties method
  3. Now, get number of lines using getLines property of BuiltInDocumentProperties object

I hope this helps. Please note that I work as developer evangelist at Aspose. If you need any help with Aspose, do let me know.



来源:https://stackoverflow.com/questions/9138318/rtf-line-count-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!