• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1555
  • Last Modified:

How to remove RTF code from a string?

Hello Experts,

I've been trying for sometime now to remove all RTF codes from a file using the Java language. I've tried different approached; none to my satisfaction.

Thank You,
AshDash
0
AshDash
Asked:
AshDash
  • 4
  • 3
  • 3
1 Solution
 
CEHJCommented:
Why do you want to - why not just ignore them?
0
 
AshDashAuthor Commented:
Well... I have the need... We are generating reports for the elements that comprise the files which contains the RTF codes. Now my generated report contains these unwanted RTF codes, which make no sence in the report. So I though of writing a java code to parse the files and remove all the RTF codes.... can you help...?

What do you mean by ignore them.... I guess, I cannot in this case?
0
 
CEHJCommented:
This is a rough and ready way of ignoring it:


public static String getPlain(String path) throws Exception {
        String result = null;
        RTFEditorKit kit = new RTFEditorKit();
        InputStream in = new FileInputStream(path);
        Document doc = new DefaultStyledDocument();
        kit.read(in, doc, 0);
        result = doc.getText(0, doc.getLength());

        return result;
    }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
try this:

http://helpdesk.objects.com.au/java/how-do-i-extract-just-the-text-form-a-html-document-ie-strip-out-all-the-html-tags

just replace

EditorKit editorKit = new HTMLEditorKit();

with:

EditorKit editorKit = new RTFEditorKit();
0
 
objectsCommented:
Why don't you just generate the reports in a different format?
0
 
AshDashAuthor Commented:
Thank you for your comments experts.

@CEHJ
I can see that your code works fine for RTF files, but my case is diferent. I do not have RTF files. The files are in a different format (consider .txt) which contains RTF codes.

An ideal scenario for a solution in this case would be to remove all RTF codes from an ascii string/file. If we can do this, I think my problem will be solved. Do we need to use regular expressions or can we still achieve this using the RTFEditorKit.

@objects
Though I did not try executing your solution yet, I believe the same constraint as discussed above would apply; considering I've plain text files with RTF codes in it.

Even if I try to generate reort in the RTF format, due to a bug in the tool, I get RFT codes are generated as it is in my generated report. Hence, forced to think of a workaround.

Thank you both in advance for further guidence and advice. Please help.
0
 
objectsCommented:
0
 
AshDashAuthor Commented:
If a better reqular expression solution using Java can be provided I would appreciate the same...
0
 
CEHJCommented:
>>The files are in a different format (consider .txt) which contains RTF codes.


Please post some examples
0
 
CEHJCommented:
:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now