| | |
| | | package com.ots.project.tool; |
| | | |
| | | import com.aspose.words.Document; |
| | | import com.aspose.words.SaveFormat; |
| | | import com.aspose.words.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.*; |
| | | import java.lang.reflect.Constructor; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | public class PdfUtil { |
| | | |
| | | public static void convertPDF(String input){ |
| | | /** |
| | | * 设置用户字体位置 |
| | | */ |
| | | public static void setFont() { |
| | | String[] fonts = {"C:\\Users\\大头\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "/usr/share/fonts"}; |
| | | List<FontSourceBase> fontSources = new ArrayList(Arrays.asList(FontSettings.getDefaultInstance().getFontsSources())); |
| | | for (int i = 0; i < fonts.length; i++) { |
| | | FolderFontSource folderFontSource = new FolderFontSource(fonts[i], true); |
| | | fontSources.add(folderFontSource); |
| | | } |
| | | FontSourceBase[] fontSourceBases = fontSources.toArray(new FontSourceBase[fontSources.size()]); |
| | | FontSettings.getDefaultInstance().setFontsSources(fontSourceBases); |
| | | } |
| | | |
| | | public static void convertPDF(String input) { |
| | | try { |
| | | registerWord(); |
| | | setFont(); |
| | | Document pres = null; |
| | | //手动替换输出pdf名称 |
| | | String output = input.replaceAll(".docx", ".pdf"); |
| | | long old = System.currentTimeMillis(); |
| | | FileInputStream fileInput = new FileInputStream(input); |
| | | Document pres = new Document(fileInput); |
| | | pres = new Document(fileInput); |
| | | FileOutputStream out = new FileOutputStream(new File(output)); |
| | | pres.save(out, SaveFormat.PDF); |
| | | out.close(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 执行python脚本 docx转pdf |
| | | * @param filePath |
| | | */ |
| | | public static void convertPythonPDF(String pythonScript,String filePath){ |
| | | try { |
| | | File file = new File(filePath); |
| | | if(!file.exists()){ |
| | | file.mkdirs(); |
| | | } |
| | | //手动替换输出pdf名称 |
| | | String output = filePath.replaceAll(".docx", ".pdf"); |
| | | String[] args1 = new String[] { "python3", pythonScript, filePath, output }; |
| | | // 执行py文件 |
| | | Process proc = Runtime.getRuntime().exec(args1); |
| | | BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK")); |
| | | String line = null; |
| | | log.info("python执行命令:{}", Arrays.toString(args1)); |
| | | log.info("python执行返回"); |
| | | while ((line = in.readLine()) != null) { |
| | | log.info(line); |
| | | } |
| | | in.close(); |
| | | proc.waitFor(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * aspose-words:jdk17:23.4 版本 |
| | | */ |
| | | public static void registerWord() throws Exception { |
| | | // 构造一个注册信息 |
| | | Class<?> zzXgCClass = Class.forName("com.aspose.words.zzXgC"); |
| | | Constructor<?> constructors = zzXgCClass.getDeclaredConstructors()[0]; |
| | | constructors.setAccessible(true); |
| | | Object instance = constructors.newInstance("zzW5k", "zzYON"); |
| | | // zzXFN = 1 |
| | | java.lang.reflect.Field zzXFN = zzXgCClass.getDeclaredField("zzXFN"); |
| | | zzXFN.setAccessible(true); |
| | | zzXFN.set(instance, 1); |
| | | |
| | | // 把注册信息放到 zzYVA这个类中来 |
| | | Class<?> zzYVAClass = Class.forName("com.aspose.words.zzYVA"); |
| | | java.lang.reflect.Field zzwP = zzYVAClass.getDeclaredField("zzwP"); |
| | | zzwP.setAccessible(true); |
| | | ArrayList<Object> zzwPValue = new ArrayList<>(); |
| | | zzwPValue.add(instance); |
| | | zzwP.set(null, zzwPValue); |
| | | |
| | | // 生成文档会掉这个来判断 zzXQo |
| | | Class<?> zzXQoClass = Class.forName("com.aspose.words.zzXQo"); |
| | | java.lang.reflect.Field zzHA = zzXQoClass.getDeclaredField("zzHA"); |
| | | zzHA.setAccessible(true); |
| | | zzHA.set(null, 128); |
| | | java.lang.reflect.Field zzWod = zzXQoClass.getDeclaredField("zzWod"); |
| | | zzWod.setAccessible(true); |
| | | zzWod.set(null, false); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | convertPDF("D:\\测评系统\\home\\林_PAQ_IA_CN.docx"); |
| | | //convertPDF("D:\\测评系统\\home\\林_PAQ_GS_TH.docx"); |
| | | convertPDF("C:\\Users\\大头\\Desktop\\林3_CIAQ_IA_CN.docx"); |
| | | } |
| | | |
| | | } |