Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to insert a word document or an RTF document into RichTextEditor?

726877Oct 28 2010 — edited Oct 29 2010
How to insert a word document or an RTF document into af:richTextEditor. I am using Apache POI for reading the Word document and getting its contents. I am able to display the whole content of the document except the table and image within the document. The data in the table is getting displayed as a string and not as a table inside the editor.

Can we insert a word/RTF document into a rich text editor?
Can we insert images into the rich text editor?

The following is the code that I used. On clicking a button the word document has to be inserted into the <af:richTextEditor>.

<af:richTextEditor id="rte1" autoSubmit="true"
immediate="true"
columns="110" rows="20">
<af:dropTarget dropListener="#{SendEmail.richTextEditorDrop}">
<af:dataFlavor flavorClass="java.lang.String"/>
</af:dropTarget>
</af:richTextEditor>

<af:commandButton text="Insert at position" id="cb2">
<af:richTextEditorInsertBehavior for="rte1" value="#{RichTextEditorUtil.docFile}"/>
</af:commandButton>

Java Code: I am using Apache POI for reading the word document.

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public String getDocFile() {
File docFile = null;
WordExtractor docExtractor = null ;
WordExtractor exprExtractor = null ;
try {
docFile = new File("C:/temp/test.doc");
//A FileInputStream obtains input bytes from a file.
FileInputStream fis=new FileInputStream(docFile.getAbsolutePath());

//A HWPFDocument used to read document file from FileInputStream
HWPFDocument doc=new HWPFDocument(fis);

docExtractor = new WordExtractor(doc);
}
catch(Exception exep)
{
System.out.println(exep.getMessage());
}

//This Array stores each line from the document file.
String [] docArray = docExtractor.getParagraphText();
String fileContent = "";
for(int i=0;i<docArray.length;i++)
{
if(docArray[i] != null)
System.out.println("Line "+ i +" : " + docArray);
fileContent += docArray[i] + "\n";
}
System.out.println(fileContent);
return fileContent;
}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 26 2010
Added on Oct 28 2010
1 comment
1,082 views