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) throw 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) + "秒");
    }
    

附录

资源
参考博客
上一篇:03-第一个C语言程序的分析


下一篇:菜鸟学SSH(十三)——Spring容器解析及简单实现