如何使用XWPF在word doc中以相同格式保存富文本区域内容 [英] How to save rich text area content in same format in a word doc using XWPF
问题描述
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.
新信.jsp
New Letter.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.removed" label="Content" style=""
cssStyle="vertical-align:top;width: 600px; height: 500px;" />
</p>
<s:submit value="Add Letter" align="center" />
</s:form>
LetterAction.java
LetterAction.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();
请帮助我,如何从富文本区域内容创建word文档或pdf文档,而不更改文本格式。我在谷歌搜索过这个解决方案.......感谢您分享这个想法
我尝试了什么:
我想生成一封信件文件。我正在用java开发一个struts应用程序。
我使用了XWPF和iText类来生成pdf或msword等文档。
程序从jsp中定义的文本区域获取字母内容。当我使用普通文本区域时,它正常工作,但我想使用富文本区域用于不同的字体样式。我正在使用富文本区域
< html>
< head>
< script src =// cdn.tinymce.com/4/tinymce.min.js\"></script>
< script> tinymce.init({selector:'textarea'} );< / script>
< / head>
< body>
< textarea>简单(免费!)您应该查看我们的高级功能。< / textarea>
< / body>
< / html>
但是它在使用富文本区域时没有生成文档
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
推荐答案
我很确定理查德到目前为止已经给了你最好的答案 - 你需要将从tinyMCE获取内容的内容,或者通过
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'});
然后分析解析/ go通过该内容,将其分解为Paragraphs,Chunks等格式化,您可以使用XWPF和/或iText ...也许iText可以处理tinyMCE的HTML输出
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
这篇关于如何使用XWPF在word doc中以相同格式保存富文本区域内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!