save rich text area content in same format in a word doc using XWPF

972 Views Asked by At

It can easily convert the normal text area content to word document using XWPF or to pdf using iTextpdf, but when I am trying to get data from rich text area, the content is not displaying in word document or pdf

I want to create a word document using XWPF from a rich text area content. But the content not displaying in the word document created. The word document content shows as follows.

</o:OfficeDocumentSettings> </xml>

Normal</w:View> 0</w:Zoom> false</w:SaveIfXMLInvalid> false</w:IgnoreMixedContent> false</w:AlwaysShowPlaceholderText> EN-US</w:LidThemeOther> X-NONE</w:LidThemeAsian> AR-SA</w:LidThemeComplexScript> </w:Compatibility> </m:mathPr></w:WordDocument> </xml>

It displays the correct data in word document created when getting data from normal text area.

enter code here

NewLetter.jsp

 <head>
  <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>
</head>
         <s:form action="letter.action" method="post" class="normalform">               
            <p>
                  <label style="vertical-align: middle;"></label>
                  <s:textarea name="letter.content" label="Content" style=""
                    cssStyle="vertical-align:top;width: 600px; height: 500px;" />
        </p>
        <s:submit value="Add Letter" align="center" />
    </s:form>

Action Class

   XWPFDocument document= new XWPFDocument(); 
       //Write the Document in file system
       File file=new File("C:\\Myuploads\\createparagraph.docx");
       FileOutputStream out = new FileOutputStream(file);

       //create Paragraph
       XWPFParagraph paragraph = document.createParagraph();

       XWPFRun run=paragraph.createRun();
       run.setText(letter.getContent());
       document.write(out);
       out.close();
       System.out.println("createparagraph.docx written successfully");
      return file;

I tried to create pdf instead of word document, still the same. That is for normal text area it creates the pdf but for the rich text area content it does not create the pdf in correct way

enter code here

Document document = new Document(); File file = new File("C:\Myuploads\" + referenceId + ".pdf");

    PdfWriter.getInstance(document, new FileOutputStream(file));
    document.open();
    Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
    Paragraph p = new Paragraph();

    toName.setIndentationLeft(56f);
    Paragraph p2 = new Paragraph();
    // p2.setSpacingBefore(56f);
    p2.setIndentationLeft(56f);
    p.add(new Chunk("Ref"));
    p.setTabSettings(new TabSettings(56f));
    p.add(Chunk.TABBING);
    p.add(new Chunk(letter.getReferenceId()));
    p.add(new Chunk("\nDate"));
    p.setTabSettings(new TabSettings(56f));
    p.add(Chunk.TABBING);
    p.add(new Chunk(letter.getLetterDate()));
    p.add(new Chunk("\n\nTo"));
    p.setTabSettings(new TabSettings(56f));
    p.add(Chunk.TABBING);
    p = new Paragraph();        
    p.add("\n" + letter.getContent());

    document.add(p);
    document.close();

Please help me, how to create the word document or pdf document from the rich text area content, without changing text format. I have searched in Google for this solution....... Advance thanks for sharing the idea

0

There are 0 best solutions below