如何将一个.docx中的某些内容复制到另一个.docx,使用POI而不丢失格式?

fly*_*use 5 java apache-poi

假设我有两个.DOCX文件,input.docx以及output.docx我需要选择一些内容input.docx,并复制它们output.docx.在newdoc显示其在控制台中的内容似乎是正确的,但我没有得到的东西output.docx,除了空行.任何人都可以提供建议吗?

InputStream is = new FileInputStream("D:\\input.docx"); 
XWPFDocument doc = new XWPFDocument(is);

List<XWPFParagraph> paras = doc.getParagraphs();  
List<XWPFRun> runs;
XWPFDocument newdoc = new XWPFDocument();                                     
for (XWPFParagraph para : paras) {  
            runs = para.getRuns();      
            if(!para.isEmpty())
            {
                XWPFParagraph newpara = newdoc.createParagraph(); 
                XWPFRun newrun = newpara.createRun();
                for (int i=0; i<runs.size(); i++) {                       
                    newrun=runs.get(i);
                    newpara.addRun(newrun);
                }
            }
        }


        List<XWPFParagraph> newparas = newdoc.getParagraphs(); 
        for (XWPFParagraph para1 : newparas) {  
            System.out.println(para1.getParagraphText());
        }// in the console, I have the correct information

        FileOutputStream fos = new FileOutputStream(new File("D:\\output.docx"));
        newdoc.write(fos);
        fos.flush();
        fos.close();
Run Code Online (Sandbox Code Playgroud)

Den*_*ASH 6

我稍微修改了您的代码,它复制文本而不更改文本格式。

public static void main(String[] args) {
    try {
        InputStream is = new FileInputStream("Japan.docx"); 
        XWPFDocument doc = new XWPFDocument(is);

        List<XWPFParagraph> paras = doc.getParagraphs();  

        XWPFDocument newdoc = new XWPFDocument();                                     
        for (XWPFParagraph para : paras) {  

            if (!para.getParagraphText().isEmpty()) {       
                XWPFParagraph newpara = newdoc.createParagraph();
                copyAllRunsToAnotherParagraph(para, newpara);
            }

        }

        FileOutputStream fos = new FileOutputStream(new File("newJapan.docx"));
        newdoc.write(fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// Copy all runs from one paragraph to another, keeping the style unchanged
private static void copyAllRunsToAnotherParagraph(XWPFParagraph oldPar, XWPFParagraph newPar) {
    final int DEFAULT_FONT_SIZE = 10;

    for (XWPFRun run : oldPar.getRuns()) {  
        String textInRun = run.getText(0);
        if (textInRun == null || textInRun.isEmpty()) {
            continue;
        }

        int fontSize = run.getFontSize();
        System.out.println("run text = '" + textInRun + "' , fontSize = " + fontSize); 

        XWPFRun newRun = newPar.createRun();

        // Copy text
        newRun.setText(textInRun);

        // Apply the same style
        newRun.setFontSize( ( fontSize == -1) ? DEFAULT_FONT_SIZE : run.getFontSize() );    
        newRun.setFontFamily( run.getFontFamily() );
        newRun.setBold( run.isBold() );
        newRun.setItalic( run.isItalic() );
        newRun.setStrike( run.isStrike() );
        newRun.setColor( run.getColor() );
    }   
}
Run Code Online (Sandbox Code Playgroud)

fontSize仍然存在一些问题。有时 POI 无法确定运行的大小(我将其值写入控制台以跟踪它)并给出 -1。当我自己设置字体时,它完美地定义了字体的大小(例如,我在 Word 中选择了一些段落并手动设置其字体,大小或字体系列)。但是当它处理另一个 POI 生成的文本时,它有时会给出 -1。因此,当 POI 给出 ​​-1 时,我引入了要设置的默认字体大小(在上面的示例中为 10)。

Calibri 字体系列似乎出现了另一个问题。但是在我的测试中,POI 默认将其设置为 Arial,所以我对默认 fontFamily 没有相同的技巧,因为它是 fontSize。

其他字体属性(粗体、斜体等)运行良好。

可能所有这些字体问题都是由于在我的测试中文本是从 .doc 文件复制的。如果您有 .doc 作为输入,请在 Word 中打开 .doc 文件,然后“另存为..”并选择 .docx 格式。然后在你的程序中使用 onlyXWPFDocument而不是HWPFDocument,我想它会没事的。