Browse Source

pdf转word相关功能

wukai 1 year ago
parent
commit
c0272bf11e

+ 2 - 1
doc-admin/pom.xml

@@ -80,6 +80,7 @@
                 <version>2.1.1.RELEASE</version>
                 <configuration>
                     <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
+                    <includeSystemScope>true</includeSystemScope>
                 </configuration>
                 <executions>
                     <execution>
@@ -102,4 +103,4 @@
         <finalName>${project.artifactId}</finalName>
     </build>
 
-</project>
+</project>

+ 125 - 0
doc-admin/src/test/java/com/test/PDFJarCrack.java

@@ -0,0 +1,125 @@
+package com.test;
+
+import org.apache.ibatis.javassist.*;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+
+/**
+ * 找到你的jar包地址,引入依赖的去maven仓库地址找到jar,将地址填入代码中的jarPath,运行Main方法会在jar包统计目录下生成破解的包
+ * aspose-pdf-22.4.cracked.jar,替换掉原来jar包就可以了
+ * @author wukai
+ *
+ */
+public class PDFJarCrack {
+    public static void main(String[] args) throws Exception {
+        String jarPath = "jar包地址";
+        crack(jarPath);
+    }
+
+    private static void crack(String jarName) {
+        try {
+            ClassPool.getDefault().insertClassPath(jarName);
+            CtClass ctClass = ClassPool.getDefault().getCtClass("com.aspose.pdf.ADocument");
+            CtMethod[] declaredMethods = ctClass.getDeclaredMethods();
+            int num = 0;
+            for (int i = 0; i < declaredMethods.length; i++) {
+                if (num == 2) {
+                    break;
+                }
+                CtMethod method = declaredMethods[i];
+                CtClass[] ps = method.getParameterTypes();
+                if (ps.length == 2
+                        && method.getName().equals("lI")
+                        && ps[0].getName().equals("com.aspose.pdf.ADocument")
+                        && ps[1].getName().equals("int")) {
+                    // 最多只能转换4页 处理
+                    System.out.println(method.getReturnType());
+                    System.out.println(ps[1].getName());
+                    method.setBody("{return false;}");
+                    num = 1;
+                }
+                if (ps.length == 0 && method.getName().equals("lt")) {
+                    // 水印处理
+                    method.setBody("{return true;}");
+                    num = 2;
+                }
+            }
+            File file = new File(jarName);
+            ctClass.writeFile(file.getParent());
+            disposeJar(jarName, file.getParent() + "/com/aspose/pdf/ADocument.class");
+        } catch (NotFoundException e) {
+            e.printStackTrace();
+        } catch (CannotCompileException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
+    private static void disposeJar(String jarName, String replaceFile) {
+        List<String> deletes = new ArrayList<>();
+        deletes.add("META-INF/37E3C32D.SF");
+        deletes.add("META-INF/37E3C32D.RSA");
+        File oriFile = new File(jarName);
+        if (!oriFile.exists()) {
+            System.out.println("######Not Find File:" + jarName);
+            return;
+        }
+        //将文件名命名成备份文件
+        String bakJarName = jarName.substring(0, jarName.length() - 3) + "cracked.jar";
+        //   File bakFile=new File(bakJarName);
+        try {
+            //创建文件(根据备份文件并删除部分)
+            JarFile jarFile = new JarFile(jarName);
+            JarOutputStream jos = new JarOutputStream(new FileOutputStream(bakJarName));
+            Enumeration entries = jarFile.entries();
+            while (entries.hasMoreElements()) {
+                JarEntry entry = (JarEntry) entries.nextElement();
+                if (!deletes.contains(entry.getName())) {
+                    if (entry.getName().equals("com/aspose/pdf/ADocument.class")) {
+                        System.out.println("Replace:-------" + entry.getName());
+                        JarEntry jarEntry = new JarEntry(entry.getName());
+                        jos.putNextEntry(jarEntry);
+                        FileInputStream fin = new FileInputStream(replaceFile);
+                        byte[] bytes = readStream(fin);
+                        jos.write(bytes, 0, bytes.length);
+                    } else {
+                        jos.putNextEntry(entry);
+                        byte[] bytes = readStream(jarFile.getInputStream(entry));
+                        jos.write(bytes, 0, bytes.length);
+                    }
+                } else {
+                    System.out.println("Delete:-------" + entry.getName());
+                }
+            }
+            jos.flush();
+            jos.close();
+            jarFile.close();
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static byte[] readStream(InputStream inStream) throws Exception {
+        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
+        byte[] buffer = new byte[1024];
+        int len = -1;
+        while ((len = inStream.read(buffer)) != -1) {
+            outSteam.write(buffer, 0, len);
+        }
+        outSteam.close();
+        inStream.close();
+        return outSteam.toByteArray();
+    }
+}

+ 35 - 0
doc-admin/src/test/java/com/test/Pdf2Word.java

@@ -0,0 +1,35 @@
+package com.test;
+
+
+import com.aspose.pdf.Document;
+import com.aspose.pdf.SaveFormat;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class Pdf2Word {
+    public static void main(String[] args) throws IOException {
+        pdf2doc("D:\\SYSTEM\\Desktop\\temp\\pdf\\test11.pdf");
+    }
+
+    //pdf转doc
+    public static void pdf2doc(String pdfPath) {
+        long old = System.currentTimeMillis();
+        try {
+            //新建一个word文档
+            String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".docx";
+            FileOutputStream os = new FileOutputStream(wordPath);
+            //doc是将要被转化的word文档
+            Document doc = new Document(pdfPath);
+            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
+            doc.save(os, SaveFormat.DocX);
+            os.close();
+            //转化用时
+            long now = System.currentTimeMillis();
+            System.out.println("Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒");
+        } catch (Exception e) {
+            System.out.println("Pdf 转 Word 失败...");
+            e.printStackTrace();
+        }
+    }
+}

BIN
doc-biz/lib/aspose-pdf-22.4.cracked.jar


+ 10 - 21
doc-biz/pom.xml

@@ -16,6 +16,14 @@
     </description>
 
     <dependencies>
+        <!--PDF转WORD-->
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-pdf</artifactId>
+            <version>22.4</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/lib/aspose-pdf-22.4.cracked.jar</systemPath>
+        </dependency>
         <!--这些只是apache ftp server相关的依赖,springboot项目本身的依赖大家自己添加即可-->
         <dependency>
             <groupId>org.apache.ftpserver</groupId>
@@ -23,11 +31,6 @@
             <version>1.2.0</version>
         </dependency>
 
-<!--        <dependency>-->
-<!--            <groupId>org.apache.ftpserver</groupId>-->
-<!--            <artifactId>ftplet-api</artifactId>-->
-<!--            <version>1.2.0</version>-->
-<!--        </dependency>-->
         <dependency>
             <groupId>org.apache.mina</groupId>
             <artifactId>mina-core</artifactId>
@@ -38,7 +41,7 @@
         <dependency>
             <groupId>com.jjt</groupId>
             <artifactId>jjt-common</artifactId>
-        </dependency> 
+        </dependency>
         <dependency>
             <groupId>com.jjt</groupId>
             <artifactId>jjt-system</artifactId>
@@ -56,21 +59,7 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
-            <!--                        <version>3.0.4</version>-->
-            <!--            <exclusions>-->
-            <!--                <exclusion>-->
-            <!--                    <groupId>org.elasticsearch</groupId>-->
-            <!--                    <artifactId>elasticsearch</artifactId>-->
-            <!--                </exclusion>-->
-            <!--            </exclusions>-->
         </dependency>
-        <!--        <dependency>-->
-        <!--            <groupId>org.elasticsearch</groupId>-->
-        <!--            <artifactId>elasticsearch</artifactId>-->
-        <!--            <version>7.17.3</version>-->
-        <!--        </dependency>-->
         <!-- 集成elasticsearch end -->
-
     </dependencies>
-
-</project>
+</project>

+ 16 - 0
doc-biz/src/main/java/com/doc/biz/controller/DocInfoController.java

@@ -20,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
+import org.yaml.snakeyaml.util.UriEncoder;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
@@ -378,4 +379,19 @@ public class DocInfoController extends BaseController {
         DocInfo info = docInfoService.selectDocInfoByDocId(docId);
         return mongoService.download(info.getFileId(), true);
     }
+
+    @ApiOperation("pdf转word")
+    @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
+    @PostMapping("/pdf2word")
+    public ResponseEntity<Object> uploadFile(@ApiParam(value = "文件", required = true) @RequestPart(value = "file") MultipartFile file) {
+        String disposition = "attachment; filename=\"" + UriEncoder.encode(file.getOriginalFilename()) + "\"";
+        //TODO 未完
+        return null;
+//        return ResponseEntity.ok()
+//                .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
+//                .header(HttpHeaders.CONTENT_TYPE, "docx")
+//                .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
+//                .header("Connection", "close")
+//                .body(vo.getData());
+    }
 }

+ 1 - 1
pom.xml

@@ -248,4 +248,4 @@
         </pluginRepository>
     </pluginRepositories>
 
-</project>
+</project>