Aspose-Wrods去水印


一、原理

自行到猿小武的博客:https://blog.csdn.net/xxw19950701/article/details/115724571進行了解。

二、准備aspose-words-21.1.0-jdk17.jar

aspose-words-21.1.0-jdk17.jar下載地址

如果失敗下載不了,可能tick失效了,自己到官網[https://www.aspose.com/#]注冊一個賬號。到[https://downloads.aspose.com/words/java]找到[Aspose.Words for Java 21.1]進行下載。

三、反編譯

  • 引入反編譯工具

    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.27.0-GA</version>
    </dependency>
    
  • 反編譯

    public static void main(String[] args) throw Exception {
      //這一步是完整的jar包路徑
      ClassPool.getDefault().insertClassPath("/Users/langkye/Development/Personal/awesome-auth/tester/lib/aspose-words-21.1.0-java/lib/aspose-words-21.1.0-jdk17.jar");
      CtClass zzZJJClass = ClassPool.getDefault().getCtClass("com.aspose.words.zzZE0");
      CtMethod zzZ4h = zzZJJClass.getDeclaredMethod("zzZ4h");
      CtMethod zzZ4g = zzZJJClass.getDeclaredMethod("zzZ4g");
      zzZ4h.setBody("{return 1;}");
      zzZ4g.setBody("{return 1;}");
      //反編譯后的“zzZE0.class"保存目錄[xxx/com/aspose/words/zzZE0.class]
      zzZJJClass.writeFile("/Users/langkye/Development/Personal/awesome-auth/tester/lib");
    }
    
  • 重新打包

    # 1.將官網下載的jar[aspose-words-21.1.0-jdk17.jar]拆包。
    # 拆包后的目錄:
    #           ./.
    #             |__com/aspose/words/
    #             |    |__...
    #             |    |__zzZE0.class(刪除zzZE0.class文件。這個文件會通過反編譯重新生成進行替換。)
    #             |__META-INF/
    #             |    |__...
    #             |    |__xxx.SF(刪除.SF文件)
    #             |    |__xxx.RSA(刪除.RSA文件)
    #             |__/
    #            
    tar -zxvf aspose-words-21.1.0-jdk17.jar
    
    # 2.刪除文件
    # 刪除[com/aspose/words/zzZE0.class]、[META-INF/xxx.SF]、[META-INF/xxx.RSA]文件。
    
    # 3.替換原文件
    # 將反編譯生成的[zzZE0.class]替換掉[com/aspose/words/zzZE0.class]
    
    # 4.打包
    jar cvfm aspose-words-21.1.0-jdk17.jar META-INF/MANIFEST.MF com/
    
    # 5.安裝到本地maven倉庫(可選)
    mvn install:install-file\
     -Dfile=/Users/langkye/Development/Personal/awesome-auth/tester/lib/new/aspose-words-21.1.0-jdk17.jar\
     -DgroupId=com.aspose\
     -DartifactId=aspose-words\
     -Dversion=21.1.0\
     -Dpackaging=jar
    

四、使用

  • 在項目中引入

    • 引入安裝到maven倉庫的依賴(二選一)。

      <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>21.1.0</version>
      </dependency>
      
    • 引入項目中lib的依賴(二選一)。

      <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words</artifactId>
        <version>21.1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/aspose-words-21.1.0-jdk17.jar</systemPath>
      </dependency>
      
      <!-- 打包需要稍作修改 -->
      <build>
      	<!-- ... -->
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 打包時會將本地jar一起打包 -->
            <configuration>
              <!-- 引入系統scope級別的jar -->
              <includeSystemScope>true</includeSystemScope>
            </configuration>     
          </plugin>
        </plugins>
      </build>
      
  • 測試·Word轉PDF

    public static void main(String[] args) throws Exception {
      // inPath = word文檔的本地路徑和名稱
      String inPath = "/Users/langkye/Downloads/16359649365739982767.docx";
      // outPath = 輸出的pdf文件的路徑和名稱
      String outPath = "/Users/langkye/Desktop/aspose_word_test.pdf";
      
      long old = System.currentTimeMillis();
      File file = new File(outPath);
      FileOutputStream os = new FileOutputStream(file);
      Document doc = new Document(inPath);
      doc.save(os, SaveFormat.PDF);
      long now = System.currentTimeMillis();
      System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
    }
    

附錄

資源
參考博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM