2

テキストを .docx ファイルに書き込むアプリケーションを作成しようとしています。私のアプリケーションは JTextPane を使用しているため、ユーザーは好きなように書くことができ、太字、フォントの色、フォント サイズなどの多くのボタンも提供されます。私が問題を抱えているのは、.docx ファイルに書き込むときに JTextPane のテキストのスタイルを維持することです。私は Swing と Apache POI にかなり慣れていないので、サンプル コードや詳細な説明が役に立ちます。

私が持っているのはこれです:(パッドは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();
4

1 に答える 1

2

私の例では、 で a を使用すると仮定HTMLEditorKitしますJTextPane。次に、ペインの を解析し、StyledDocumentそれに応じて textruns を設定します。

もちろん、これは単なるスターターです。考えられるすべてのスタイルを解析し、以下のループで自分で変換する必要があります。

私は HTMLEditorKit で何かをしたことがないので、CSS.CssValues を適切に処理する方法がわかりません。

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();
    }
}
于 2013-07-24T00:05:38.680 に答える