关于word文档内容填写及使用apache poi进行word转pdf的格式问题

qq_33220605 2017-10-09 03:20:50
最近javaweb项目中使用到了用apache poi将word转成pdf的功能。
具体过程如下,首先将作为word文档(docx的格式,内容为合同、表格等)转成zip格式后解压,之后将解压文件中的document.xml文件取出,转为ftl文件。随后使用freemarker以此ftl为模板生成新的document.xml,再将其替代原document.xml文件压缩成zip,转为docx格式,通过这种方式在word文件中填入我们需要的各项内容。最后使用apache poi将该新生成的word文档生成pdf。
然而在其转换过程中遇到了格式上的问题,列举如下:
(1)即使在word文档中设置了中文格式习惯(即按照中文格式控制首尾字符),在实际操作中仍然出现了标点符号等出现在行文行首的问题。
(2)为解决问题1,我曾换用了openoffice来实现word转pdf的操作,但用上述freemarker方式生成的wrod文档(docx格式)在java项目中无法被openoffice组件识别,但是当该文档在word中打开并另存为后,又可以被识别了。
针对上述问题,希望有大神可以给我解答其原因及解决思路。同时,如果有精确格式的word转pdf方法,希望不吝赐教。
...全文
1111 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwn_2016 2017-10-10
  • 打赏
  • 举报
回复
引用 3 楼 qq_33220605 的回复:
[quote=引用 1 楼 xwn_2016 的回复:] jacob不错,可以试试
请问下是否支持对word文档特定位置特定内同的数据填充呢?[/quote]
   /**
     * 向 document 中插入文本内容
     *
     * @param textToInsert
     *            插入的文本内容
     */
    public void insertText(String textToInsert) {
        // 在指定的位置插入文本内容
        Dispatch.put(selection, "Text", textToInsert);
        // 取消选中,应该就是移动光标
        Dispatch format = Dispatch.get(selection, "ParagraphFormat").toDispatch();
        // 设置段落格式为首行缩进2个字符
        Dispatch.put(format, "CharacterUnitFirstLineIndent", new Variant(2));
        Dispatch.call(selection, "MoveRight", new Variant(1), new Variant(1));
        //moveRight(1);
        Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //Dispatch.call(selection, "MoveUp");
        //moveDown(1);
    }

    /**
     * 插入标题
     * @param num  标题编号
     * @param level 标题级别:-2:一级标题;-3:二级标题;-4:三级标题;-5:四级标题
     * @param text 标题题目
     */
    public void insertTitle(String num, int level, String text) {
        Dispatch activeDocument = Dispatch.get(word, "ActiveDocument").toDispatch();

        //Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //moveDown(1);
        Dispatch.put(selection, "Text", num + " " + text);
        Dispatch style = Dispatch.call(activeDocument, "Styles", new Variant(level)).toDispatch();;
        Dispatch.put(selection, "Style", style);
        moveRight(1);
        Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //moveDown(1);
    }
qq_33220605 2017-10-10
  • 打赏
  • 举报
回复
引用 5 楼 xwn_2016 的回复:
[quote=引用 3 楼 qq_33220605 的回复:] [quote=引用 1 楼 xwn_2016 的回复:] jacob不错,可以试试
请问下是否支持对word文档特定位置特定内同的数据填充呢?[/quote]
   /**
     * 向 document 中插入文本内容
     *
     * @param textToInsert
     *            插入的文本内容
     */
    public void insertText(String textToInsert) {
        // 在指定的位置插入文本内容
        Dispatch.put(selection, "Text", textToInsert);
        // 取消选中,应该就是移动光标
        Dispatch format = Dispatch.get(selection, "ParagraphFormat").toDispatch();
        // 设置段落格式为首行缩进2个字符
        Dispatch.put(format, "CharacterUnitFirstLineIndent", new Variant(2));
        Dispatch.call(selection, "MoveRight", new Variant(1), new Variant(1));
        //moveRight(1);
        Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //Dispatch.call(selection, "MoveUp");
        //moveDown(1);
    }

    /**
     * 插入标题
     * @param num  标题编号
     * @param level 标题级别:-2:一级标题;-3:二级标题;-4:三级标题;-5:四级标题
     * @param text 标题题目
     */
    public void insertTitle(String num, int level, String text) {
        Dispatch activeDocument = Dispatch.get(word, "ActiveDocument").toDispatch();

        //Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //moveDown(1);
        Dispatch.put(selection, "Text", num + " " + text);
        Dispatch style = Dispatch.call(activeDocument, "Styles", new Variant(level)).toDispatch();;
        Dispatch.put(selection, "Style", style);
        moveRight(1);
        Dispatch.call(selection, "TypeParagraph");// 插入一个空行
        //moveDown(1);
    }
[/quote] 谢谢~我去试试哈
qq_33220605 2017-10-10
  • 打赏
  • 举报
回复
没有大神遇到过类似的问题吗?
qq_33220605 2017-10-09
  • 打赏
  • 举报
回复
引用 1 楼 xwn_2016 的回复:
jacob不错,可以试试
请问下是否支持对word文档特定位置特定内同的数据填充呢?
qq_33220605 2017-10-09
  • 打赏
  • 举报
回复
请问下是否支持对word文档特定位置特定内同的数据填充呢?
xwn_2016 2017-10-09
  • 打赏
  • 举报
回复
jacob不错,可以试试

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧