Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


New Letter.jsp
Java
 <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.removed" label="Content" style=""
                    cssStyle="vertical-align:top;width: 600px; height: 500px;" />
        </p>
        <s:submit value="Add Letter" align="center" />
    </s:form>


LetterAction.java
Java
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;


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

What I have tried:

I want to generate a letter document. I am developing an struts application in java.
I have used both XWPF and iText classes for generating document such as pdf or msword.
The program taking the letter content from a text area defined in jsp. it is working properly when I am using normal text area, but I want to use rich text area for different font style. I am using the rich text area through

<html>
<head>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
</head>
<body>
<textarea>Easy (and free!) You should check out our premium features.</textarea>
</body>
</html>

But its not generating the document while using rich text area
Posted
Updated 23-Apr-16 22:11pm
Comments
Richard MacCutchan 24-Apr-16 2:35am    
You will need to examine all the rich text to find out what styles are applied, and add that information to your document as you create it.
LukmanulHakeem.T 24-Apr-16 2:56am    
when I am getting content from the normal jsp text area its displaying in the word document generated(using XWPF) and in the pdf(using iText).
But when I am replacing the normal text area in the jsp with rich text area using <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>, then the generating document displaying some content like


<p><!-- [if gte mso 9]><xml> <o:officedocumentsettings> <o:relyonvml> <o:allowpng> </xml><![endif]--></p> <p><!-- [if gte mso 9]><xml> <w:worddocument> <w:view>Normal <w:zoom>0 <w:trackmoves> <w:trackformatting> <w:punctuationkerning> <w:validateagainstschemas> <w:saveifxmlinvalid>false <w:ignoremixedcontent>false <w:alwaysshowplaceholdertext>false <w:donotpromoteqf> <w:lidthemeother>EN-US <w:lidthemeasian>X-NONE <w:lidthemecomplexscript>AR-SA <w:compatibility> <w:breakwrappedtables> <w:snaptogridincell> <w:wraptextwithpunct> <w:useasianbreakrules> <w:dontgrowautofit> <w:splitpgbreakandparamark> <w:enableopentypekerning> <w:dontflipmirrorindents> <w:overridetablestylehps> <w:usefelayout> <m:mathpr> <m:mathfont m:val="Cambria Math"> <m:brkbin m:val="before"> <m:brkbinsub m:val="--"> <m:smallfrac m:val="off"> <m:dispdef> <m:lmargin m:val="0"> <m:rmargin m:val="0"> <m:defjc m:val="centerGroup"> <m:wrapindent m:val="1440"> <m:intlim m:val="subSup"> <m:narylim m:val="undOvr"> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:latentstyles deflockedstate="false" defunhidewhenused="false" defsemihidden="false" defqformat="false" defpriority="99" latentstylecount="371"> <w:lsdexception locked="false" priority="0" qformat="true" name="Normal"> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 1"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 2"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 3"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 4"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 5"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 6"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 7"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 8"> <w:lsdexception locked="false" priority="9" semihidden="true" unhidewhenused="true" qformat="true" name="heading 9"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 1"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 2"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 3"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 4"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 5"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 6"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 7"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 8"> <w:lsdexception locked="false" semihidden="true" unhidewhenused="true" name="index 9">

1 solution

I'm pretty sure Richard has given you the best answer so far - you need something that will get the content from tinyMCE, either by

// Get the HTML contents of the currently active editor
console.debug(tinyMCE.activeEditor.getContent());


or

// Get the raw contents of the currently active editor
tinyMCE.activeEditor.getContent({format : 'raw'});


and then somehting to parse/go through that content, to break it down into Paragraphs, Chunks etc with formatting, that you can the use XWPF and/or iText on .. maybe iText can deal with the HTML output from tinyMCE
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900