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. 如何配置Tomcat以使用Apache httpd?

    How to Connect Tomcat 6 to Apache HTTP Server 2 Tomcat can be run as a standalone server. Tomcat can ...

  2. NS3可视化及动画工具(PyViz和NetAnim)

    一.PyViz安装 1.安装依赖包 sudo apt-get install python-dev python-pygraphviz python-kiwi python-pygoocanvas p ...

  3. 我的代码- rf sampling

    # coding: utf-8 # In[6]: import pandas as pdimport numpy as npfrom sklearn import treefrom sklearn.s ...

  4. UGUI之用ScrollRect做下拉刷新

    在ScrollView下有中层次是scorllview->viewport->content  scorllView对象下的ScrollRect组件是关键 ,scorllRect中指定其对 ...

  5. js·逻辑运算

    || 遇到第一个为真就返回 && 遇到第一个为假就终止,返回false,如果没遇到就返回最后那一个 5&&4&&2&&1  ==> ...

  6. Linux本地yum源配置以及使用yum源安装gcc编译环境

    本文档是图文安装本地yum源的教程,以安装gcc编译环境为例. 适用范围:所有的cetos,红帽,fedroa版本 适用人群:有一点linux基础的小白 范例系统版本:CentOS Linux rel ...

  7. 11. IDS (Intrusion detection systems 入侵检测系统 6个)

    Snort该网络入侵检测和防御系统擅长于IP网络上的流量分析和数据包记录. 通过协议分析,内容研究和各种预处理器,Snort可以检测到数千个蠕虫,漏洞利用尝试,端口扫描和其他可疑行为. Snort使用 ...

  8. Linux第十一节课学习笔记

    区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换. RUNTIME模式:当前生效,重启失效:PERM ...

  9. Oracle开启监听缓慢问题

    现象: Oracle数据库中,使用lsnrctl开启.关闭监听或查看监听状态时,非常慢,可以通过断网的方式使动作马上完成. 在客户端使用plsql等方式连接数据库时,也是非常慢,同样也可以通过切换网络 ...

  10. PRESTO安装部署和参数说明(一)

    PRESTO部署和参数说明(一) 一,概要 在部署和使用presto的过程中,在此记录一下部署记录和使用记录以及需要注意的事项.本人使用的presto版本是0.214,3台redhat虚拟机.使用背景 ...