今天是:
带着程序的旅程,每一行代码都是你前进的一步,每个错误都是你成长的机会,最终,你将抵达你的目的地。
title

Java word 转换为pdf

JAVA 中 word转pdf的方式主要有:

  1.    Use Apache POI & iText (代码较多, 且转换格式不够完美).
  2.   jacob + msOfficeWord + SaveAsPDFandXPS (完美保持原doc格式,效率最慢,只能在windows环境下进行)
  3.  Aspose.Words  (完美保持原doc格式,使用简单,收费. 破解包下载)

本文使用第一张和第三种方式

一. Aspose.Words 为收费版,要想转换完成的去掉水印,需要付费或个人使用破解版

@Test
public  void doc2PDF() {
    String docPath ="D:/2222.docx";
    String pdfPath="D:/aspose.pdf";
    try {
        File file = new File(pdfPath);
        FileOutputStream os = new FileOutputStream(file);
        com.aspose.words.Document doc = new com.aspose.words.Document(docPath);
        doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 二. Apache POI & iText

      所需jar包,----->maven 依赖

<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>4.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.pdf -->
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
    <version>1.0.6</version>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
    <version>1.0.6</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.9</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.9</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version><!--$NO-MVN-MAN-VER$-->
</dependency>
@Test
public void convert3() throws Exception{
    String inputFile="D:/test3.docx";
    String outputFile="D:/convert.pdf";
    System.out.println("inputFile:" + inputFile + ",outputFile:"+ outputFile);
    FileInputStream in=new FileInputStream(inputFile);
    XWPFDocument document=new XWPFDocument(in);
    File outFile=new File(outputFile);
    OutputStream out=new FileOutputStream(outFile);
    PdfOptions options=null;
    PdfConverter.getInstance().convert(document,out,options);
}

   

分享到:

专栏

类型标签

网站访问总量