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