admin管理员组

文章数量:1605160

1. 下载Aspose.PDF for Java21.11官方jar包

调用方法直接转换,这样会有水印和数量限制

 public static void main(String[] args) throws Exception {
       //调用授权方法
        InputStream is = new FileInputStream("license.xml");//license文件的位置
        License license = new License();
        license.setLicense(is);
     pdf2doc(sourceFile,targetFile);
    }
/**
     * PDF转Word操作
     * @param sourceFile 源文件
     * @param targetFile 目标文件
     */
    public static void pdf2doc(String sourceFile, String targetFile) {
        try {
            long old = System.currentTimeMillis();
            FileOutputStream os = new FileOutputStream(targetFile);
            Document doc = new Document(sourceFile);//加载源文件数据
            doc.save(os, com.aspose.pdf.SaveFormat.DocX);//设置转换文件类型并转换
            os.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

license.xml
文件内容这里是个过期的文件主要是格式

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>
        sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    </Signature>
</License>

下面是没有水印和数量限制的(前提是修改L9f.class的内容)

  1. 添加Javassist修改class字节码文件
<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.28.0-GA</version>
</dependency>
  1. 添加修改方法
 public static void main(String[] args) throws Exception {
       //调用授权方法
        InputStream is = new FileInputStream("license.xml");//license文件的位置
        License license = new License();
        license.setLicense(is);
     modifyPDFJar(sourceFile,targetFile);
    }
/**
 * 修改pdf jar包里面的校验
 */
public static void modifyPDFJar() {
    try {
        //这一步是完整的jar包路径,选择自己解压的jar目录
        ClassPool.getDefault().insertClassPath("***aspose.pdf-21.11.jar");
        //获取指定的class文件对象
        CtClass zzZJJClass = ClassPool.getDefault().getCtClass("com.aspose.pdf.l9f");
        //从class对象中解析获取所有方法
        CtMethod[] methodA = zzZJJClass.getDeclaredMethods();
        for (CtMethod ctMethod : methodA) {
            //获取方法获取参数类型
            CtClass[] ps = ctMethod.getParameterTypes();
            //筛选同名方法,入参是Document
            if (ps.length == 1 && ctMethod.getName().equals("lI") && ps[0].getName().equals("java.io.InputStream")) {
                System.out.println("ps[0].getName==" + ps[0].getName());
                //替换指定方法的方法体
                ctMethod.setBody("{this.l0if = com.aspose.pdf.l10if.lf;com.aspose.pdf.internal.imaging.internal.p71.Helper.help1();lI(this);}");
            }
        }
        //这一步就是将破译完的代码放在桌面上
        zzZJJClass.writeFile("C:\\Users\\ISC\\Desktop\\");

    } catch (Exception e) {
        System.out.println("错误==" + e);
    }
}

运行修改方法后会在桌面生成 com 修改后的文件夹

  1. 修改jar包里面的数据

打开jar包将桌面com文件夹覆盖到jar包com文件夹
删除jar包里面的.RSA和.SF文件

4. 重新导入修改后的jar包进行测试
maven移除旧的jar包,导入修改后的jar包
调用测试方法进行测试转换后的文件是否去除水印和数量限制成功

 static String sourceFile = "C:\\Users\\ISC\\Desktop\\b.pdf";//输入的文件
 static String targetFile = "C:\\Users\\ISC\\Desktop\\a.docx";//输出的文件
 public static void main(String[] args) throws Exception {
        InputStream is = new FileInputStream("**\\license.xml");
        License license = new License();
        license.setLicense(is);
     pdf2doc(sourceFile,targetFile);
    }
/**
 * PDF转Word操作
 *
 * @param sourceFile 源文件
 * @param targetFile 目标文件
 */
public static void pdf2doc(String sourceFile, String targetFile) {
    try {
        long old = System.currentTimeMillis();
        FileOutputStream os = new FileOutputStream(targetFile);
        com.aspose.pdf.Document doc = new com.aspose.pdf.Document(sourceFile);//加载源文件数据
        doc.save(os, com.aspose.pdf.SaveFormat.DocX);//设置转换文件类型并转换
        os.close();
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
    } catch (Exception e) {
        e.printStackTrace();
    }
}

如果想转Excel就把targetFile 的后缀改成Excel的,还有pdf2doc方法里面的save参数改成SaveFormat.Excel
测试有效,如果你转换的时候还是有水印和数量限制,请认真检查是不是没修改到i9f类文件,转换成功后样式较PDF还是有一点点点瑕疵,有大佬知道怎么完善的欢迎留言,教教弟弟!!
声明 本方法只做个人研究学习使用,切勿用于商用。

本文标签: 水印数量PDFJavaExcel