簡體   English   中英

如何在 java 中合並兩個復雜的 rtf(具有圖像或表格)

[英]how to merge two complex rtf(having images or tables) in java

下面是我的代碼片段,適用於簡單的 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);
        }
    }

作為 lowagie.itext 從 2009 年開始丟棄與 RTF(富文本格式)相關的 function。我們必須從頭開始。 基本邏輯是

  1. 刪除源文檔最后一個大括號,添加分頁符
  2. 刪除目標文檔 header(從第一個左大括號到第二個)
  3. 將目標文檔字體表、樣式表、顏色表唯一地合並(聯合)到源文檔(未實現)
  4. append 左目標文檔到源文檔

http://latex2rtf.sourceforge.net/RTF-Spec-1.2.pdf

合並 RTF 文件

 private static byte[] mergeRTF(byte[] sourceRtf, byte[] targetRtf) {
    String sourceStr = new String(sourceRtf, StandardCharsets.UTF_8);
    String targetStr = new String(targetRtf, StandardCharsets.UTF_8);
    //source string remove last right curly brace }
    int lastRightBraceIndex = sourceStr.lastIndexOf('}');
    sourceStr = sourceStr.substring(0, lastRightBraceIndex);
    //target string remove from 1st left Curly brace to 2nd one.
    int secondLeftBraceIndex = StringUtils.ordinalIndexOf(targetStr, "{", 2);
    targetStr = targetStr.substring(secondLeftBraceIndex);
    // source append \page
    StringBuilder mergedStr = new StringBuilder(sourceStr);
    mergedStr.append(System.lineSeparator()).append("\\page").append(System.lineSeparator());
    mergedStr.append(targetStr);

    return mergedStr.toString().getBytes(StandardCharsets.UTF_8);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM