word转pdf

jar包

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>3.0.1</version>
</dependency>

 

代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List; import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; public class Word2Pdf {
public static void main(String[] args) {
try { long start = System.currentTimeMillis(); InputStream is = new FileInputStream(
new File("E:\\1234.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
List sections = wordMLPackage.getDocumentModel().getSections();
for (int i = 0; i < sections.size(); i++) { System.out.println("sections Size" + sections.size());
wordMLPackage.getDocumentModel().getSections().get(i)
.getPageDimensions().setHeaderExtent(3000);
}
Mapper fontMapper = new IdentityPlusMapper(); PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
"Comic Sans MS"); fontMapper.getFontMappings().put("Algerian", font); wordMLPackage.setFontMapper(fontMapper);
PdfSettings pdfSettings = new PdfSettings();
org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage); OutputStream out = new FileOutputStream(new File(
"E:\\1234.pdf"));
conversion.output(out, pdfSettings);
System.out.println("Time taken to Generate pdf "
+ (System.currentTimeMillis() - start) + "ms");
System.out.println("end--------------------------"+System.currentTimeMillis());
} catch (Exception e) {
e.printStackTrace();
System.err.println("error--------------------------"+System.currentTimeMillis());
}
}
}

  好像只支持docx,不支持doc,原因正在查找中

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;import org.docx4j.fonts.IdentityPlusMapper;import org.docx4j.fonts.Mapper;import org.docx4j.fonts.PhysicalFont;import org.docx4j.fonts.PhysicalFonts;import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Word2Pdf {    public static void main(String[] args) {        try {
            long start = System.currentTimeMillis();
            InputStream is = new FileInputStream(                    new File("E:\\1234.docx"));            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage                    .load(is);            List sections = wordMLPackage.getDocumentModel().getSections();            for (int i = 0; i < sections.size(); i++) {
                System.out.println("sections Size" + sections.size());                wordMLPackage.getDocumentModel().getSections().get(i)                        .getPageDimensions().setHeaderExtent(3000);            }            Mapper fontMapper = new IdentityPlusMapper();
            PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(                    "Comic Sans MS");
            fontMapper.getFontMappings().put("Algerian", font);
            wordMLPackage.setFontMapper(fontMapper);            PdfSettings pdfSettings = new PdfSettings();            org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(                    wordMLPackage);
            OutputStream out = new FileOutputStream(new File(                    "E:\\1234.pdf"));            conversion.output(out, pdfSettings);            System.out.println("Time taken to Generate pdf  "                    + (System.currentTimeMillis() - start) + "ms");            System.out.println("end--------------------------"+System.currentTimeMillis());        } catch (Exception e) {            e.printStackTrace();            System.err.println("error--------------------------"+System.currentTimeMillis());        }    }}

[java,2019-01-15] word转pdf的更多相关文章

  1. 【转】Java通过IText导出word和pdf

    原帖地址:http://blog.csdn.net/zwx19921215/article/details/34439851 最近做的项目中需要用到把Highcharts图表导出word和pdf的功能 ...

  2. java 使用jacob把word转pdf

    一.使用前要下载必要包及文件 链接: https://pan.baidu.com/s/1nvutQxb 密码: qgpi 二.引包和dll文件 1.引包:eclipse引包就不用说了,idea引包步骤 ...

  3. Java用OpenOffice将word转换为PDF

    一.      软件安装以及jar包下载 官网的下载地址如下(英文): OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http: ...

  4. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  5. JAVA使用aspose实现word文档转pdf文件

    引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...

  6. java word 转 pdf

    这里使用jacob将word转pdf,使用的是jacob.jar import java.io.File;import com.jacob.activeX.ActiveXComponent;impor ...

  7. java 调用OpenOffice将word格式文件转换为pdf格式

    一:环境搭建 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/projects/jo ...

  8. Java使用Openoffice将word、ppt转换为PDF

    最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...

  9. Java 将word转为pdf jacob方式

    package com.doctopdf; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jac ...

随机推荐

  1. 前端---js02

    主要内容 1.数组 2.字符串 3.Date日期对象 4.内置对象 5.定时器 6.DOM 7.伪数组 内置对象: 1 数组(列表) Array (1) 数组的创建 <script>//字 ...

  2. python学习笔记(1)python中的注释和安装python

    注释 目标 注释的作用 单行注释 多行注释 01注释的作用 在程序中对代码的标注说明,增强代码的可读性 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到辅助说明作用 为 ...

  3. 简述linux操作系统启动流程

    Linux启动流程 POST-->BootSequence(BIOS)->Bootloader(MBR,grub)-->kernnel(ramdisk,initrd)-->ro ...

  4. hbase整合

    hbase與hive整合    1. hive中有數據 --> 創建hive管理表映射hbase    例如:        1)hive創建內部表            create tabl ...

  5. Cisco Packet Tracer7.1 rip协议实验

    设备: 路由器:三个1941:router0,router1,router2; 终端用户:二个PC-PT:PC0,PC1; 网络配置: 网络 设备 接口 IP 设备 接口 IP 192.168.0.0 ...

  6. mysql Incorrect usage of UNION and ORDER BY 错误备忘

    出现这个错误的语句是酱紫的 select xxx from aaa order by xxx union all select yyy from bbb order by yyy 错误原因居然是,如果 ...

  7. What You Can Learn from Actifio Logs

    The Actifio services generate many logs, some of which are useful for troubleshooting. This section ...

  8. WinForm关于更新程序的设计思路

    开发WINDOWS应用程序一般都会有一个自动更新的功能,这就需要提供一个单独的更新程序来更新主程序,那么主程序怎么检测是否有更新,以及更新程序怎么去更新主程序呢?下面将分开研究分析. 用VS发布向导发 ...

  9. 给jumpserver双机配置glusterfs共享复制卷

    为什么要使用glusterfs呢. 本身Haproxy+Keepalived对jumpserver进行了负载均衡和反向代理.但是真实的视频只会存储在一个节点上 否则播放视频的时候会出现找不到的情况 为 ...

  10. MySQL innobackupex全量备份恢复

    转自 http://blog.itpub.net/27099995/viewspace-1295099/ 先简单介绍一下这个工具:innobackupexinnobackupex比xtarbackup ...