PdfUtils.java 748 B

123456789101112131415161718192021222324252627
  1. package com.onemap.file.utils;
  2. import com.spire.doc.*;
  3. import java.io.*;
  4. public class PdfUtils {
  5. /**
  6. * @param toFilePath 文件夹路径
  7. * @param fileName 文件名
  8. * @param type 文件类型
  9. * @return
  10. * @throws Exception
  11. */
  12. public static String file2pdf(String toFilePath, String fileName, String type) throws Exception {
  13. String infilepath = toFilePath;
  14. String outfilepath = infilepath.replace(type,".pdf");
  15. //实例化Document类的对象
  16. Document doc = new Document();
  17. //加载Word
  18. doc.loadFromFile(infilepath);
  19. //保存为PDF格式
  20. doc.saveToFile(outfilepath,FileFormat.PDF);
  21. return fileName.replace(type,".pdf");
  22. }
  23. }