栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java WORD转PDF

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java WORD转PDF

需要的依赖和仓库地址

         
            e-iceblue
            spire.doc.free
            5.2.0
        

        
            com.e-iceblue
            e-iceblue
            https://repo.e-iceblue.cn/repository/maven-public/
        

直接上代码


import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.aspose.cells.License;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class WordToPdf {
	
	public static void main(String[] args) {
		doc2pdf("C:\Users\admin\Desktop\1.docx","E:\a\excel转pdf\1.pdf");
	}
	
	   
	   public static boolean getLicense() {
	       boolean result = false;
	       try {
	    	   String license = "rn" + 
	    	   		"  rn" + 
	    	   		"    rn" + 
	    	   		"      Aspose.Total for Javarn" + 
	    	   		"      Aspose.Words for Javarn" + 
	    	   		"    rn" + 
	    	   		"    Enterprisern" + 
	    	   		"    20991231rn" + 
	    	   		"    20991231rn" + 
	    	   		"    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7rn" + 
	    	   		"  rn" + 
	    	   		"  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=rn" + 
	    	   		"";
	    	   
	           InputStream is = new  ByteArrayInputStream(license.getBytes("UTF-8"));
	           License aposeLic = new License();
	           aposeLic.setLicense(is);
	           result = true;
	       } catch (Exception e) {
	           e.printStackTrace();
	       }
	       return result;
	   }
	   
	 public static boolean doc2pdf(String inPath, String outPath) {
	        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生  
	            return false;  
	        }
            Document document = new Document();

            document.loadFromFile(inPath);
            document.saveToFile(outPath, FileFormat.PDF);
	        return true;
	    }  
}

 用接口测试,代码如下

	@PostMapping("/doc2pdf")
	public String doc2pdf(MultipartFile file ) throws IllegalStateException, IOException{
		String fileName = file.getOriginalFilename();

		String localFilePath = StrUtil.appendIfMissing("/data/test", "/") + fileName;
		
		
		file.transferTo(new File(localFilePath));
		WordToPdf.doc2pdf(localFilePath, "/data/test/" + fileName.split("\.")[0] + ".pdf");

		return "success可喜可贺";
	}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/984996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号