当前位置: 代码迷 >> 综合 >> java word(doc docx excel) to pdf
  详细解决方案

java word(doc docx excel) to pdf

热度:92   发布时间:2023-10-17 18:50:05.0

java 代码

 

 

package com.ibeetl.admin.core.util.file;import java.io.*;import org.aspectj.weaver.ast.Test;import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;/*** @author Administrator* @version $Id$* @since* @see*/
public class WordToPdfUtil {//此插件不需要安装office服务但是此插件加密,如不注册会有水印
//    public static void main(String[] args) {
//        //doc2pdf("C:/Users/lss/Desktop/test.doc");
//     doc2pdf("F:\\doc\\a.doc","F:\\doc\\a.pdf");
//    }public static boolean getLicense() {boolean result = false;try {InputStream is = Test.class.getClassLoader().getResourceAsStream("/license/license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下License aposeLic = new License();//aposeLic.setLicense("GsNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=");aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}public static void doc2pdf(String inPath, String outPath) {
//        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
//            return;
//        }try {long old = System.currentTimeMillis();File file = new File(outPath); // 新建一个空白pdf文档FileOutputStream os = new FileOutputStream(file);Document doc = new Document(inPath); // Address是将要被转化的word文档doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互转换long now = System.currentTimeMillis();System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时} catch (Exception e) {e.printStackTrace();}}public static void doc2pdf(File inFile, File outFile) {// 验证License 若不验证则转化出的pdf文档会有水印产生
//        if (!getLicense()) {
//            return;
//        }FileInputStream os_inFile = null;try {long old = System.currentTimeMillis();//File file = new File(outPath); // 新建一个空白pdf文档os_inFile = new FileInputStream(inFile);FileOutputStream os_outfile = new FileOutputStream(outFile);// Address是将要被转化的word文档Document doc = new Document(os_inFile);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换doc.save(os_outfile, SaveFormat.PDF);long now = System.currentTimeMillis();System.out.println(os_inFile+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时} catch (Exception e) {e.printStackTrace();}finally {try {os_inFile.close();} catch (IOException e) {e.printStackTrace();}}}public static void savedocx(String inPath, String outPath) {if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {long old = System.currentTimeMillis();File file = new File(outPath); // 新建一个空白pdf文档FileOutputStream os = new FileOutputStream(file);Document doc = new Document(inPath); // Address是将要被转化的word文档doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,// EPUB, XPS, SWF 相互转换long now = System.currentTimeMillis();System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";String wordFile = PDFTIMEDIR + "新建DOC文档.doc";String pdfFile = PDFTIMEDIR + "新建DOC文档.doc.pdf";//doc2pdf(wordFile, pdfFile);doc2pdf(new File(wordFile), new File(pdfFile));//    String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//    String wordFile = PDFTIMEDIR + "新建DOCX文档.docx.docx";
//    String pdfFile = PDFTIMEDIR + "新建DOCX文档.docx.pdf";
//    doc2pdf(wordFile, pdfFile);//    String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//    String wordFile = PDFTIMEDIR + "新建PPT演示文稿.ppt";
//    String pdfFile = PDFTIMEDIR + "新建PPT演示文稿.ppt.pdf";
//    doc2pdf(wordFile, pdfFile);//
//        String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//        String wordFile = PDFTIMEDIR + "新建 RTF 文档.rtf";
//        String pdfFile = PDFTIMEDIR + "新建 RTF 文档.rtf.pdf";
//        doc2pdf(wordFile, pdfFile);}}

 

maven代码

主要是aspose 这个jar公共仓库下载不到

需要去aspose的私有仓库下载

地址:https://repository.aspose.com/repo

 

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>word</groupId><artifactId>wordToPdf</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>wordToPdf</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- word to pdf --><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>19.4</version><classifier>jdk17</classifier></dependency></dependencies><repositories><!-- aspose word to pdf --><repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://repository.aspose.com/repo/</url></repository></repositories>
</project>
  相关解决方案