合并文件方法:
/**
* 合并docx输出文件流
*
* @param streams
* 要合并的word文件的输入流
* @return InputStream
* @throws Docx4JException
* @throws IOException
*/
public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException {
WordprocessingMLPackage target = null;
final File generated = File.createTempFile("tmp", ".docx");
int chunkId = 0;
Iterator<InputStream> it = streams.iterator();
while (it.hasNext()) {
InputStream is = it.next();
if (is != null) {
try {
if (target == null) {
//第一次插入创建
OutputStream os = new FileOutputStream(generated);
os.write(IOUtils.toByteArray(is));
os.close();
target = WordprocessingMLPackage.load(generated);
} else {
//其他文档在原有基础上追加
MainDocumentPart documentPart = target.getMainDocumentPart();
// 另起一页,换页
addPageBreak(documentPart);
// 插入文件内容
insertDocx(documentPart, IOUtils.toByteArray(is), chunkId++);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
is.close();
}
}
}
generated.deleteOnExit();
if (target != null) {
target.save(generated);
return new FileInputStream(generated);
} else {
return null;
}
}
插入分页方法:
public static void addPageBreak(MainDocumentPart documentPart) {
P paragraph = factory.createP();
R run = factory.createR();
Br br = factory.createBr();
br.setType(STBrType.PAGE);
run.getContent().add(br);
paragraph.getContent().add(run);
documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
发布者:admin,转转请注明出处:http://www.yc00.com/web/1742359582a4429122.html
评论列表(0条)