写作样式的文本,以.docx文件 [英] Writing Styled Text to a .docx file

查看:233
本文介绍了写作样式的文本,以.docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写会写文本到.docx文件的应用程序。我的应用程序使用的JTextPane,使用户可以写他/她想要什么,而且还提供了许多按钮,如粗体,字体颜色,字体大小...等。什么我在与被保持在J​​TextPane的文本的风格,当我写的.docx文件有问题。我是相当新的摇摆和Apache POI等等例如code和/或一个详细的解释将是有益的。

我已经是这样的:(垫指的JTextPane)

 的FileOutputStream输出=新的FileOutputStream(文件);
XWPFDocument文档=新XWPFDocument();
XWPFParagraph段= document.createParagraph();
XWPFRun运行= paragraph.createRun();
run.setText(pad.getText());
文件撰写(输出);
output.close();


解决方案

有关我的例子中我假设你使用的 一个HTMLEditorKit 在你的 的JTextPane 。然后我会解析 StyledDocument中的 窗格并设置相应textruns。

当然,这仅仅是一个启动,你需要分析所有可能的风格和自己进行转换他们在下面的循环。

我已经承认,我从来没有做过的东西与HTMLEditorKit和因此,我不知道如何妥善处理CSS.CssValues​​。

 进口java.awt.Color中;
进口java.io.FileOutputStream中;
进口java.lang.reflect.Field中;
进口的java.util.Enumeration;
进口的javax.swing *。
导入的javax.swing.text *。
导入的javax.swing.text.html *。
导入org.apache.poi.xwpf.usermodel *。
公共类的StyledText {
    公共静态无效的主要(字串[] args)抛出异常{
        // prepare
        JTextPane的垫=新的JTextPane();
        pad.setContentType(text / html的);
        一个HTMLEditorKit套件=(一个HTMLEditorKit)pad.getEditorKit();
        HTMLDocument的HTMLDOC =(HTMLDocument的)kit.createDefaultDocument();
        kit.insertHTML(HTMLDOC,htmldoc.getLength(),&所述p为H.;段&所述b取代1所述; / B个;&下; / P>中,0,0,NULL);
        kit.insertHTML(HTMLDOC,htmldoc.getLength(),< P>段<跨度风格= \\颜色:红\\> 2< / SPAN>< / P>中,0,0,NULL) ;
        pad.setDocument(HTMLDOC);        // 兑换
        StyledDocument中的DOC = pad.getStyledDocument();
        XWPFDocument DOCX =新XWPFDocument();        INT lastPos = -1;
        而(lastPos< doc.getLength()){
            元件行= doc.getParagraphElement(lastPos + 1);
            lastPos = line.getEndOffset();
            XWPFParagraph段= docX.createParagraph();
            对(INT elIdx = 0; elIdx&下; line.getElementCount(); elIdx ++){
                最后一个元素FRAG = line.getElement(elIdx);                XWPFRun运行= paragraph.createRun();
                串潜台词= doc.getText(frag.getStartOffset(),frag.getEndOffset() - frag.getStartOffset());
                run.setText(潜台词);                最终的AttributeSet为= frag.getAttributes();
                最后枚举<> AE = as.getAttributeNames();                而(ae.h​​asMoreElements()){
                    最终目标ATTRIB = ae.nextElement();                    如果(CSS.Attribute.COLOR.equals(ATTRIB)){
                        //我不知道如何真正与CSS-Swing类工作...
                        域F = as.getAttribute(ATTRIB).getClass()getDeclaredField(C)。
                        f.setAccessible(真);
                        颜色C =(彩色)f.get(as.getAttribute(ATTRIB));
                        run.setColor(的String.format(%1 $ 02X%2 $ 02X%3 $ 02X,c.getRed(),c.getGreen(),c.getBlue()));
                    }否则如果(CSS.Attribute.FONT_WEIGHT.equals(ATTRIB)){
                        如果(大胆.equals(as.getAttribute(ATTRIB)的ToString())){
                            run.setBold(真);
                        }
                    }
                }
            }
        }        FOS的FileOutputStream =新的FileOutputStream(test.docx);
        docX.write(FOS);
        fos.close();
    }
}

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();
    }
}

这篇关于写作样式的文本,以.docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆