莫辂技术
今天是:
我需要三件东西:爱情友谊和图书。然而这三者之间何其相通!炽热的爱情可以充实图书的内容,图书又是人们最忠实的朋友。
User Image Java word 转换为pdf 2019-09-03

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);
}

   

45 likes - 2 comments

我的

类型标签

外部链接

网站访问总量