I'm trying to write an application that will write text to a .docx file. My application uses a JTextPane so the user can write whatever he/she wants, and it also provides many buttons such as bold, font color, font size... ect. What I'm having a problem with is maintaining the style of the text on the JTextPane to when I write to the .docx file. I'm fairly new to Swing and Apache POI so example code and/or a detailed explanation would be helpful.
What I have is this: (pad refers to the JTextPane)
FileOutputStream output = new FileOutputStream(file);
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(pad.getText());
document.write(output);
output.close();
For my example I assume you'd use a HTMLEditorKit
in your JTextPane
. I would then parse the StyledDocument
of the pane and set the textruns accordingly.
Of course this is just a starter, you'd need to parse all the possible styles and convert them yourself in the loop below.
I've to admit, that I've never done something with the HTMLEditorKit and therefore I don't know how to handle the CSS.CssValues properly.
import java.awt.Color;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import org.apache.poi.xwpf.usermodel.*;
public class StyledText {
public static void main(String[] args) throws Exception {
// prepare
JTextPane pad = new JTextPane();
pad.setContentType("text/html");
HTMLEditorKit kit = (HTMLEditorKit)pad.getEditorKit();
HTMLDocument htmldoc = (HTMLDocument)kit.createDefaultDocument();
kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <b>1</b></p>", 0, 0, null);
kit.insertHTML(htmldoc, htmldoc.getLength(), "<p>paragraph <span style=\"color:red\">2</span></p>", 0, 0, null);
pad.setDocument(htmldoc);
// convert
StyledDocument doc = pad.getStyledDocument();
XWPFDocument docX = new XWPFDocument();
int lastPos=-1;
while (lastPos < doc.getLength()) {
Element line = doc.getParagraphElement(lastPos+1);
lastPos = line.getEndOffset();
XWPFParagraph paragraph = docX.createParagraph();
for (int elIdx=0; elIdx < line.getElementCount(); elIdx++) {
final Element frag = line.getElement(elIdx);
XWPFRun run = paragraph.createRun();
String subtext = doc.getText(frag.getStartOffset(), frag.getEndOffset()-frag.getStartOffset());
run.setText(subtext);
final AttributeSet as = frag.getAttributes();
final Enumeration<?> ae = as.getAttributeNames();
while (ae.hasMoreElements()) {
final Object attrib = ae.nextElement();
if (CSS.Attribute.COLOR.equals(attrib)) {
// I don't know how to really work with the CSS-swing class ...
Field f = as.getAttribute(attrib).getClass().getDeclaredField("c");
f.setAccessible(true);
Color c = (Color)f.get(as.getAttribute(attrib));
run.setColor(String.format("%1$02X%2$02X%3$02X", c.getRed(),c.getGreen(),c.getBlue()));
} else if (CSS.Attribute.FONT_WEIGHT.equals(attrib)) {
if ("bold".equals(as.getAttribute(attrib).toString())) {
run.setBold(true);
}
}
}
}
}
FileOutputStream fos = new FileOutputStream("test.docx");
docX.write(fos);
fos.close();
}
}