如何在java中合并两个复杂的rtf(具有图像或表)

如何在java中合并两个复杂的rtf(具有图像或表),java,merge,rtf,Java,Merge,Rtf,下面是我用于简单rtf合并的代码片段。但它不适用于复杂的rtf合并,就像rtf具有一些图像或表一样。有人能帮上忙吗?如何合并具有图像或表格的两个rtf File input1 = new File("C:\\input\\document1.rtf"); File input2 = new File("C:\\input\\document2.rtf"); File output = new File ("C:\\output\\res.rtf"); FileInpu

下面是我用于简单rtf合并的代码片段。但它不适用于复杂的rtf合并,就像rtf具有一些图像或表一样。有人能帮上忙吗?如何合并具有图像或表格的两个rtf

 File input1 = new File("C:\\input\\document1.rtf");
    File input2 = new File("C:\\input\\document2.rtf");
    File output = new File ("C:\\output\\res.rtf");

    FileInputStream fis1 = null;
    FileInputStream fis2 = null;
    FileOutputStream fw = null;
    try {
        fis1 = new FileInputStream(input1);
        fis2 = new FileInputStream(input2);
        fw = new FileOutputStream(output);
    } catch (IOException e1) {
        e1.printStackTrace();
    }


    try {
        Document doc1 = load(fis1);
        Document doc2 = load(fis2);
        //String contents1 = doc1.getText(0, doc1.getLength());
        //String contents2 = doc2.getText(0, doc2.getLength());
        mergeDocument((DefaultStyledDocument)doc2, (DefaultStyledDocument)doc1);
        RTFEditorKit rtf = new RTFEditorKit();
        rtf.write(fw, doc1, 0, doc1.getLength());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
    finally{
        try {
            fis1.close();
            fis2.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static Document load(InputStream is) throws IOException {
    RTFEditorKit rtf = new RTFEditorKit();
    Document doc = rtf.createDefaultDocument();
    BufferedReader input = new BufferedReader(new InputStreamReader(is));
    try {
        rtf.read(input, doc, 0);
    } catch (BadLocationException ble) {
        throw new IOException(ble);
    }
    return doc;
}

public static void mergeDocument(DefaultStyledDocument source, DefaultStyledDocument dest) throws BadLocationException {
    ArrayList<DefaultStyledDocument.ElementSpec> specs=new ArrayList<DefaultStyledDocument.ElementSpec>();
    DefaultStyledDocument.ElementSpec spec=new DefaultStyledDocument.ElementSpec(new SimpleAttributeSet(), 
             DefaultStyledDocument.ElementSpec.EndTagType);
    specs.add(spec);
    fillSpecs(source.getDefaultRootElement(), specs, false);
    spec=new DefaultStyledDocument.ElementSpec(new SimpleAttributeSet(), DefaultStyledDocument.ElementSpec.StartTagType);
    specs.add(spec);

    DefaultStyledDocument.ElementSpec[] arr = new DefaultStyledDocument.ElementSpec[specs.size()];
    specs.toArray(arr);
    insertSpecs(dest, dest.getLength(), arr);
}

protected static void insertSpecs(DefaultStyledDocument doc, int offset, DefaultStyledDocument.ElementSpec[] specs) {
    try {
        Method m=DefaultStyledDocument.class.getDeclaredMethod("insert", new Class[] {int.class, DefaultStyledDocument.ElementSpec[].class});
        m.setAccessible(true);
        m.invoke(doc, new Object[] {offset, specs});
    } catch (Exception e) {
        e.printStackTrace();
    }
}

protected static void fillSpecs(Element elem, ArrayList<DefaultStyledDocument.ElementSpec> specs, boolean includeRoot) throws BadLocationException{
    DefaultStyledDocument.ElementSpec spec;
    if (elem.isLeaf()) {
        String str=elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset()-elem.getStartOffset());
        spec=new DefaultStyledDocument.ElementSpec(elem.getAttributes(), 
                 DefaultStyledDocument.ElementSpec.ContentType,str.toCharArray(), 0, str.length());
        specs.add(spec);
    }
    else {
        if (includeRoot) {
            spec=new DefaultStyledDocument.ElementSpec(elem.getAttributes(), DefaultStyledDocument.ElementSpec.StartTagType);
            specs.add(spec);
        }
        for (int i=0; i<elem.getElementCount(); i++) {
            fillSpecs(elem.getElement(i), specs, true);
        }

        if (includeRoot) {
            spec=new DefaultStyledDocument.ElementSpec(elem.getAttributes(), DefaultStyledDocument.ElementSpec.EndTagType);
            specs.add(spec);
        }
    }
File input1=新文件(“C:\\input\\document1.rtf”);
File input2=新文件(“C:\\input\\document2.rtf”);
文件输出=新文件(“C:\\output\\res.rtf”);
FileInputStream fis1=null;
FileInputStream fis2=null;
FileOutputStream fw=null;
试一试{
fis1=新文件InputStream(input1);
fis2=新文件InputStream(input2);
fw=新文件输出流(输出);
}捕获(IOE1异常){
e1.printStackTrace();
}
试一试{
文件doc1=加载(fis1);
文件doc2=加载(fis2);
//字符串contents1=doc1.getText(0,doc1.getLength());
//字符串contents2=doc2.getText(0,doc2.getLength());
合并文档((DefaultStyledDocument)doc2,(DefaultStyledDocument)doc1);
RTFEditorKit rtf=新的RTFEditorKit();
write(fw,doc1,0,doc1.getLength());
}捕获(IOE异常){
e、 printStackTrace();
}捕获(错误位置异常e){
e、 printStackTrace();
}
最后{
试一试{
fis1.关闭();
fis2.close();
fw.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
公共静态文档加载(InputStream is)引发IOException{
RTFEditorKit rtf=新的RTFEditorKit();
Document doc=rtf.createDefaultDocument();
BufferedReader输入=新的BufferedReader(新的InputStreamReader(is));
试一试{
rtf.read(输入,单据,0);
}捕获(BadLocationException-ble){
抛出新IOException(ble);
}
退货单;
}
公共静态无效合并文档(DefaultStyledDocument源、DefaultStyledDocument目标)引发BadLocationException{
ArrayList specs=新的ArrayList();
DefaultStyledDocument.ElementSpec=新的DefaultStyledDocument.ElementSpec(新的SimpleAttributeSet(),
DefaultStyledDocument.ElementSpec.EndTagType);
规范。添加(规范);
fillSpecs(source.getDefaultRootElement(),specs,false);
spec=新的DefaultStyledDocument.ElementSpec(新的SimpleAttributeSet(),DefaultStyledDocument.ElementSpec.StartTagType);
规范。添加(规范);
DefaultStyledDocument.ElementSpec[]arr=新的DefaultStyledDocument.ElementSpec[specs.size()];
规格到阵列(arr);
insertSpecs(dest,dest.getLength(),arr);
}
受保护的静态void insertSpecs(DefaultStyledDocument文档、int offset、DefaultStyledDocument.ElementSpec[]规范){
试一试{
方法m=DefaultStyledDocument.class.getDeclaredMethod(“插入”,新类[]{int.class,DefaultStyledDocument.ElementSpec[].class});
m、 setAccessible(true);
m、 调用(doc,新对象[]{offset,specs});
}捕获(例外e){
e、 printStackTrace();
}
}
受保护的静态void fillSpecs(元素元素、ArrayList specs、布尔includeRoot)引发BadLocationException{
DefaultStyledDocument.ElementSpec;
if(elem.isLeaf()){
String str=elem.getDocument().getText(elem.getStartOffset(),elem.getEndOffset()-elem.getStartOffset());
spec=新的DefaultStyledDocument.ElementSpec(elem.getAttributes(),
DefaultStyledDocument.ElementSpec.ContentType,str.ToCharray(),0,str.length();
规范。添加(规范);
}
否则{
如果(包括根){
spec=new DefaultStyledDocument.ElementSpec(elem.getAttributes(),DefaultStyledDocument.ElementSpec.StartTagType);
规范。添加(规范);
}

对于(inti=0;iYou要求某人实现合并“复杂RTF文件”,这要求很高,也离题了。事实上,我尝试并能够合并简单的rtf文档,但当我尝试使用带有图像的rtf时,我的代码不起作用。您应该共享代码并告诉我们您遇到了什么问题。您好,请查看上面的代码片段。您要求有人实现合并“复杂rtf文件”,这要求很高,也离题了。实际上,我尝试并能够合并简单的rtf文档,但当我尝试使用rtf图像时,我的代码不起作用。您应该共享代码并告诉我们您遇到了什么问题。您好,请查看上面的代码片段。