word转pdf

jar包

  1. <dependency>
  2. <groupId>org.docx4j</groupId>
  3. <artifactId>docx4j</artifactId>
  4. <version>3.0.1</version>
  5. </dependency>

 

代码

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.List;
  7.  
  8. import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
  9. import org.docx4j.fonts.IdentityPlusMapper;
  10. import org.docx4j.fonts.Mapper;
  11. import org.docx4j.fonts.PhysicalFont;
  12. import org.docx4j.fonts.PhysicalFonts;
  13. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
  14.  
  15. public class Word2Pdf {
  16. public static void main(String[] args) {
  17. try {
  18.  
  19. long start = System.currentTimeMillis();
  20.  
  21. InputStream is = new FileInputStream(
  22. new File("E:\\1234.docx"));
  23. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
  24. .load(is);
  25. List sections = wordMLPackage.getDocumentModel().getSections();
  26. for (int i = 0; i < sections.size(); i++) {
  27.  
  28. System.out.println("sections Size" + sections.size());
  29. wordMLPackage.getDocumentModel().getSections().get(i)
  30. .getPageDimensions().setHeaderExtent(3000);
  31. }
  32. Mapper fontMapper = new IdentityPlusMapper();
  33.  
  34. PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
  35. "Comic Sans MS");
  36.  
  37. fontMapper.getFontMappings().put("Algerian", font);
  38.  
  39. wordMLPackage.setFontMapper(fontMapper);
  40. PdfSettings pdfSettings = new PdfSettings();
  41. org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
  42. wordMLPackage);
  43.  
  44. OutputStream out = new FileOutputStream(new File(
  45. "E:\\1234.pdf"));
  46. conversion.output(out, pdfSettings);
  47. System.out.println("Time taken to Generate pdf "
  48. + (System.currentTimeMillis() - start) + "ms");
  49. System.out.println("end--------------------------"+System.currentTimeMillis());
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. System.err.println("error--------------------------"+System.currentTimeMillis());
  53. }
  54. }
  55. }

  好像只支持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. Java第五周总结

    Java抽象类与接口的区别 很多常见的面试题都会出诸如抽象类和接口有什么区别,什么情况下会使用抽象类和什么情况你会使用接口这样的问题.本文我们将仔细讨论这些话题. 在讨论它们之间的不同点之前,我们先看 ...

  2. explain和profiling分析查询SQL时间

    mysql可以通过profiling命令查看到执行查询SQL消耗的时间. 默认情况下,mysql是关闭profiling的,命令: select @@profiling; +------------- ...

  3. P2930 [USACO09HOL]假期绘画Holiday Painting

    线段树水题,考虑到只有15列,所以我们对于每一列,我们都去维护一个线段树. 现在来考虑一下修改操作,因为每次修改的时候,我们都是将黑的改成白的,白的改成黑的,所以我们对线段树的每个节点维护当前这段区间 ...

  4. 本地复现Flash 0day漏洞(CVE-2018-4878)

    影响版本: Adobe Flash Player <= 28.0.0.137 EXP下载地址: 链接: https://pan.baidu.com/s/1_VVQfdx6gsJvEDJj51Jg ...

  5. TensorFlow:检查显卡支持哪个版本的CUDA

    最近想学习TensorFlow深度学习编程,然后就开始查这方面的信息. 第0步是安装环境,配置环境. 首先到TensorFlow的官网查看在Windows上安装的条件和步骤. 可以仅仅安装CPU版本的 ...

  6. 第七节《Git协议与工作协同》

    Git提供了丰富的协议支持,包括:SSH.GIT.HTTPS.FTP.FTPS.RSYNC,这些协议可以分为两类:智能协议和哑协议. <1>智能协议 在会话时使用智能协议,会在会话的两个版 ...

  7. 浏览器输入url回车后,会发生什么?

    通常我们想访问某一个网址,我们会在浏览器中输入它的域名,然后点击回车进行跳转,这样就可以进到网站的主页,看似简单的两步,其实背后都需要大量的代码在运行,支持,才能完成!那浏览器到底都做了哪些事呢? 一 ...

  8. 19.3 Table 1-2.S3C2440A 289-Pin FBGA Pin Assignments (Sheet 4 of 9) (Continued)

    应该为GPA22,这个在中文翻译手册里是正确的.

  9. 【MatConvNet代码解析】 vl_nnsoftmaxloss

    背景知识:http://deeplearning.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 假设softmax层的输入(softmax ...

  10. 一个极其简易版的vue.js实现

    前言 之前项目中一直在用vue,也边做边学摸滚打爬了近一年.对一些基础原理性的东西有过了解,但是不深入,例如面试经常问的vue的响应式原理,可能大多数人都能答出来Object.defineProper ...