如何在一个.DOCX一些内容复制到其他的.docx,使用POI不失格式? [英] How to copy some content in one .docx to another .docx , using POI without losing format?

查看:1673
本文介绍了如何在一个.DOCX一些内容复制到其他的.docx,使用POI不失格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  InputStream为=新的FileInputStream(D:\\\\ input.docx);
XWPFDocument DOC =新XWPFDocument(是);清单< XWPFParagraph>第= doc.getParagraphs();
清单< XWPFRun>运行;
XWPFDocument newdoc =新XWPFDocument();
对于(XWPFParagraph段:段){
            奔跑= para.getRuns();
            如果(!para.isEmpty())
            {
                XWPFParagraph newpara = newdoc.createParagraph();
                XWPFRun newrun = newpara.createRun();
                的for(int i = 0; I< runs.size();我++){
                    newrun = runs.get(ⅰ);
                    newpara.addRun(newrun);
                }
            }
        }
        清单< XWPFParagraph> newparas = newdoc.getParagraphs();
        对于(XWPFParagraph PARA1:newparas){
            的System.out.println(para1.getParagraphText());
        } //在控制台中,我有正确的信息        FOS的FileOutputStream =新的FileOutputStream(新文件(D:\\\\ output.docx));
        newdoc.write(FOS);
        fos.flush();
        fos.close();


解决方案

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

 公共静态无效的主要(字串[] args){
    尝试{
        InputStream为=新的FileInputStream(Japan.docx);
        XWPFDocument DOC =新XWPFDocument(是);        清单< XWPFParagraph>第= doc.getParagraphs();        XWPFDocument newdoc =新XWPFDocument();
        对于(XWPFParagraph段:段){            如果(!para.getParagraphText()。的isEmpty()){
                XWPFParagraph newpara = newdoc.createParagraph();
                copyAllRunsToAnotherParagraph(对,newpara);
            }        }        FOS的FileOutputStream =新的FileOutputStream(新文件(newJapan.docx));
        newdoc.write(FOS);
        fos.flush();
        fos.close();
    }赶上(FileNotFoundException异常五){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}//将所有从一个段落到另一个运行,保持不变的风格
私有静态无效copyAllRunsToAnotherParagraph(XWPFParagraph oldPar,XWPFParagraph newPar){
    最终诠释DEFAULT_FONT_SIZE = 10;    对于(XWPFRun运行:oldPar.getRuns()){
        串textInRun = run.getText(0);
        如果(textInRun == NULL || textInRun.isEmpty()){
            继续;
        }        INT fontSize的= run.getFontSize();
        的System.out.println(运行文本='+ textInRun +'和fontSize =+ fontSize的);        XWPFRun newRun = newPar.createRun();        //复制文本
        newRun.setText(textInRun);        //应用相同的风格
        (?(fontSize的== -1)DEFAULT_FONT_SIZE:run.getFontSize())newRun.setFontSize;
        newRun.setFontFamily(run.getFontFamily());
        newRun.setBold(run.isBold());
        newRun.setItalic(run.isItalic());
        newRun.setStrike(run.isStrike());
        newRun.setColor(run.getColor());
    }
}

目前仍然是 fontSize的一个小问题。有时,POI不能确定运行的大小(我写它的价值安慰跟踪它),并给出-1。它定义字体的完美尺寸,当我设置它自己(比如,我在Word中选择一些段落,并手动设置其字体,大小或字体系列)。但是,当它把另一个POI生成的文本,它有时会-1。所以,我intriduce一个默认字体大小(10在上面的例子)进行设置时,POI给出-1。

另一个问题似乎与宋体字体家族出现。但在我的测试中,POI其设置默认为Arial,所以我没有默认fontFamily中同样的伎俩,因为它是中文字体。

其他字体属性(粗体,斜体等)的工作。

也许,这些字体问题是由于在我的测试中的文字是从.doc文件复制的事实。如果你有.DOC作为输入,在Word中打开.doc文件,然后选择另存为...,然后选择.DOCX格式。然后在你的程序只使用 XWPFDocument 而不是 HWPFDocument ,我想一切都会好的。

Suppose I have two .docx files, input.docx and output.docx I need to select some of the content in input.docx and copy them to output.docx. The newdoc displays its content in the console seems correct, but I did not get anything in the output.docx, except blank lines. Can anyone provide advices?

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

解决方案

I slightly modified your code, it copies text without changing text format.

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

There's still a little problem with fontSize. Sometimes POI can't determine the size of a run (i write its value to console to trace it) and gives -1. It defines perfectly the size of font when i set it myself (say, i select some paragraphs in Word and set its font manually, either size or font family). But when it treats another POI-generated text, it sometimes gives -1. So i intriduce a default font size (10 in the above example) to be set when POI gives -1.

Another issue seems to emerge with Calibri font family. But in my tests, POI sets it to Arial by default, so i don't have the same trick with default fontFamily, as it was for fontSize.

Other font properties (Bold, italic, etc.) work well.

Probably, all these font problems are due to the fact that in my tests text was copied from .doc file. If you have .doc as input, open .doc file in Word, then "Save as.." and choose .docx format. Then in your program use only XWPFDocument instead of HWPFDocument, and i suppose it will be okay.

这篇关于如何在一个.DOCX一些内容复制到其他的.docx,使用POI不失格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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