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. 同时使用多个JDK版本的方法

    开发环境需要使用JDK1.8,但是有些系统仅支持JDK1.6,以下方法可满足此需求: 1.使用安装包安装JDK1.6,安装之后记得设置不自动更新,控制面板-JAVA:        2.使用免安装版J ...

  2. springmvc 在非controller下使用@autowired

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...

  3. sku

    以淘宝为例,sku是具体到某一个商家具体规格商品,比如某商家红色64g的iPhone6,sku对应有对应的价格和库存.而SPU就是我们在输入框里输入的iPhone6,它是多个商家的集合.淘宝的“宝贝” ...

  4. 2018.5.11 B树总结

    小结 B树:二叉树,每个结点只存储一个关键字,等于则命中,小于走左结点,大于 走右结点: B-树:多路搜索树,每个结点存储M/2到M个关键字,非叶子结点存储指向关键 字范围的子结点: 所有关键字在整颗 ...

  5. 爬虫模块介绍--request(发送请求模块)

    爬虫:可见即可爬   # 每个网站都有爬虫协议 基础爬虫需要使用到的三个模块 requests 模块  # 模拟发请求的模块 PS:python原来有两个模块urllib和urllib的升级urlli ...

  6. Python练习四

    1.任意输入一串文字加数字,统计出数字的个数,数字相连的视为一个,如:12fd2表示两个数字,即12为一个数字. content = input("请输入内容:") for i i ...

  7. C++动态链接库方法调用

    //定义内存的信息结构 [StructLayout(LayoutKind.Sequential)] public struct MEMORY_INFO { public uint dwLength; ...

  8. 各种BUG

    1.下载VS2005,版本太低和win10不好兼容 2.由于提前下载好了,visual studio2010,在下载VS2012过程中提示说检测到要更新VS2010才可以,百度后,返回安装上一步,取消 ...

  9. Comedi的安装

    1.comedi的安装:(需要注意的是comedi和comedilib安装顺序随意)    1.comedi与大多数2.2,2.4和2.6Linux内核配合使用,不支持2.6-2.6.6的内核,对2. ...

  10. Salesforce Invoking Http Callouts and Testing Http Callouts

    本文参考官方文档和zero zhang的博客: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/ape ...