Sidebar

How do I convert an RTF document to plain text?

0 votes
2.7K views
asked Jul 22, 2015 by ben-s-7515 (12,640 points)
I need to remove all of the RTF control codes and output plain text.

1 Answer

+1 vote

Java has a native class to help with this.  It is the RTFEditorKit.java.

Here is some sample code that will convert the message from RTF to plain text.

// THis function will take a string and convert RTF to text
function convertRtfToText(inputString) {
   inputString = new java.lang.String(inputString);
   /*jshint undef:false*/
   var rtfEditorKit = new javax.swing.text.rtf.RTFEditorKit();
   /*jshint undef:true*/
   var rtfDocument = rtfEditorKit.createDefaultDocument();
   rtfEditorKit.read(new java.io.ByteArrayInputStream(inputString.getBytes()), rtfDocument, 0);
   return rtfDocument.getText(0, rtfDocument.getLength());
}

// convertRtfToText will take a string that has the RTF formatting and return
// the content without the RTF formmatting.

// This example will replace the entire message object
message.setNode('/', convertRtfToText(message.getNode('/')));

// This example will replace just the OBX-5 value
message.setNode('OBX-5', convertRtfToText(message.getNode('OBX-5')));

answered Jul 22, 2015 by ben-s-7515 (12,640 points)
edited Nov 17, 2023 by brandon-w-8204
commented Oct 10, 2016 by ramaswamy-s-6353 (440 points)
Hi, can you tell me how I can use this class on just the OBX-5 value? I tried this -
message.setNode('OBX-5', rtfDocument.getText(0, rtfDocument.getLength()));
but the OBX-5 value still had LF characters  [x0A]
...