admin管理员组文章数量:1024072
合并文件方法:
/**
* 合并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);
}
合并文件方法:
/**
* 合并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);
}
版权声明:本文标题:docx4j处理word文档另起一页合并的解决方法,wps软件和office软件打开均有分页效果 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1742359583a1950799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论