您现在的位置是:首页 > 编程 > 

docx4j处理word文档另起一页合并的解决方法,wps软件和office软件打开均有分页效果

2025-07-18 03:09:52
合并文件方法: /** * 合并docx输出文件流 * * @param streams * 要合并的word文件的输入流 * @return InputStream * @throws Docx4JException * @throws IOExcepti

合并文件方法:

/**
     * 合并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 = (tmp, .docx);
        int cId = 0;
        Iterator<InputStream> it = streams.iterator();
        while (it.hat()) {
            InputStream is = ();
            if (is != null) {
                try {
                    if (target == null) {
                        //第一次插入创建
                        OutputStream os = new FileOutputStream(generated);
                        os.write((is));
                        ();
                        target = WordprocessingMLPackage.load(generated);
                    } else {
                        //其他文档在原有基础上追加
                        MainDocumentPart documentPart = target.getMainDocumentPart();
                        // 另起一页,换页
                        addPageBreak(documentPart);
                        // 插入文件内容
                        insertDocx(documentPart, (is), cId);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    ();
                }
            }
        }
        generated.deleteOnExit();
        if (target != null) {
            target.save(generated);
            return new FileInputStream(generated);
        } else {
            return null;
        }
    }

插入分页方法:

public static void addPageBreak(MainDocumentPart documentPart) {
        P paragraph = ();
        R run = ();
        Br br = ();
        br.setType(STBrType.PAGE);
        run.getContent().add(br);
        paragraph.getContent().add(run);
        documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1098957.html

相关标签:无
上传时间: 2025-07-15 10:25:19
留言与评论(共有 9 条评论)
本站网友 什么是白殿疯
16分钟前 发表
IOException { WordprocessingMLPackage target = null; final File generated = (tmp
本站网友 手癣
29分钟前 发表
cId); } } catch (Exception e) { e.printStackTrace(); } finally { (); } } } generated.deleteOnExit(); if (target != null) { target.save(generated); return new FileInputStream(generated); } else { return null; } } 插入分页方法: public static void addPageBreak(MainDocumentPart documentPart) { P paragraph = (); R run = (); Br br = (); br.setType(STBrType.PAGE); run.getContent().add(br); paragraph.getContent().add(run); documentPart.getJaxbElement().getBody().getContent().add(paragraph); }
本站网友 中天微
15分钟前 发表
合并文件方法: /** * 合并docx输出文件流 * * @param streams * 要合并的word文件的输入流 * @return InputStream * @throws Docx4JException * @throws IOException */ public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException
本站网友 跟着贝尔去冒险
17分钟前 发表
合并文件方法: /** * 合并docx输出文件流 * * @param streams * 要合并的word文件的输入流 * @return InputStream * @throws Docx4JException * @throws IOException */ public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException
本站网友 阳萎的治疗
27分钟前 发表
IOException { WordprocessingMLPackage target = null; final File generated = (tmp
本站网友 千岁寒
28分钟前 发表
合并文件方法: /** * 合并docx输出文件流 * * @param streams * 要合并的word文件的输入流 * @return InputStream * @throws Docx4JException * @throws IOException */ public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException
本站网友 增值服务
15分钟前 发表
(is)
本站网友 母乳奶粉混合喂养
15分钟前 发表
IOException { WordprocessingMLPackage target = null; final File generated = (tmp