Java实现Excel文件转PDF(无水印无限制)

2023-01-21 0 3,842

目录

前言

java处理excel转pdf一直没找到什么好用的免费jar包工具,自己手写的难度,恐怕高级程序员花费一年的事件,也不能做出来非常好用,再说谁会不赚钱,花费一年事件去研究java如何实现excel转pdf的,于是我找到了Aspose公司出的aspose-cells的java的jar包来实现。之前写过一篇技术文章,不过后来觉得实现起来有些繁琐,因为aspose-cells没有商业授权,转换出来的pdf都会带文字和图片水印,且转换pdf的页数也会被受限制,之前的逻辑是自己用aspose-cells转换pdf后,又用apache-pdfbox去实现pdf的水印去除。这样不仅浪费了性能,还加长了处理时间。于是这个版想从aspose-cells入手,破除商业版的限制。教程如下。

一、jar破解

1.项目远程仓库配置

aspose-cells 这个需要配置单独的仓库地址才能下载,不会配置的可以去官网直接下载jar引入项目代码中。

<repositories>
        <repository>
            <id>AsposeJavaAPI</id>
            <name>Aspose Java API</name>
            <url>https://repository.aspose.com/repo/</url>
        </repository>
    </repositories>

2.pom文件引入相关依赖

        <!-- https://mvnrepository.com/artifact/com.aspose/aspose-cells -->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>21.8</version>
        </dependency>
       <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.20.0-GA</version>
        </dependency>

Javassist是一个开源的分析、编辑和创建Java字节码的类库。 

3.代码破解 

import 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;
 
public class ExcelJarCrack {
    public static void main(String[] args) throws Exception {
        String jarPath = \"C:\\\\Users\\\\liuya\\\\Desktop\\\\jar\\\\aspose-cells-21.8.jar\";
        crack(jarPath);
    }
 
    private static void crack(String jarName) throws NotFoundException, CannotCompileException, IOException {
        //这一步是完整的jar包路径
        ClassPool.getDefault().insertClassPath(jarName);
        CtClass LicenseClass = ClassPool.getDefault().getCtClass(\"com.aspose.cells.License\");
        CtMethod[] aMethods = LicenseClass.getDeclaredMethods(\"a\");
        for (CtMethod aMethod : aMethods) {
            CtClass returnType=aMethod.getReturnType();
            if(returnType.getName().equals(\"boolean\")){
                aMethod.setBody(\"{return true;}\");
                break;
            }
        }
        //将文件名命名成备份文件
       File file=new File(jarName);
       LicenseClass.writeFile(file.getParent());
       disposeJar(jarName);
    }
 
    private static void disposeJar(String jarName) {
        List<String> deletes = new ArrayList<>();
        deletes.add(\"META-INF/37E3C32D.SF\");
        deletes.add(\"META-INF/37E3C32D.RSA\");
        List<String> replaces = new ArrayList<>();
        replaces.add(\"com/aspose/cells/License.class\");
        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\";
        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(replaces.contains(entry.getName())){
                        System.out.println(\"Replace:-------\" +entry.getName());
                        JarEntry jarEntry = new JarEntry(entry.getName());
                        jos.putNextEntry(jarEntry);
                        FileInputStream fin = new FileInputStream(oriFile.getParent()+ \"/\"+entry.getName());
                        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();
    }
 
}

修改为你本机的aspose-cells-21.8.jar路径,然后运行主方法,破解成功后,会再同级文件夹下生成一个aspose-cells-21.8.cracked.jar包,用这个包替换原来的aspose-pdf-21.8.jar包即可。

二、Excel转PDF

1.代码实现

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
 
import java.io.FileOutputStream;
 
public class PdfUtils {
 
    public static void main(String[] args) {
        excelToPdf(\"C:\\\\Users\\\\liuya\\\\Desktop\\\\excel\\\\test.xlsx\");
    }
 
    /**
     * Excel文件转换
     * @param excelPath 需要被转换的excel全路径带文件名
     * @Return void
     */
    public static void excelToPdf(String excelPath) {
        License license = new License();
        license.setLicense(\"C:\\\\Users\\\\liuya\\\\Desktop\\\\jar\\\\Aspose.License.xml\");
        long old = System.currentTimeMillis();
        try {
            //新建一个pdf文档
            String pdfPath=excelPath.substring(0,excelPath.lastIndexOf(\".\"))+\".pdf\";
            //Excel文件数据
            Workbook wb = new Workbook(excelPath);
            FileOutputStream fileOS = new FileOutputStream(pdfPath);
            //保存为pdf文件
            wb.save(fileOS, SaveFormat.PDF);
            fileOS.close();
            //转化用时
            long now = System.currentTimeMillis();
            System.out.println(\"EXCEL 转 Pdf 共耗时:\" + ((now - old) / 1000.0) + \"秒\");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
}

2.Aspose.License.xml 授权文件

代码如下:

<License>
    <Data>
        <LicensedTo>Aspose Scotland Team</LicensedTo>
        <EmailTo>billy.lundie@aspose.com</EmailTo>
        <LicenseType>Developer OEM</LicenseType>
        <LicenseNote>Limited to 1 developer, unlimited physical locations</LicenseNote>
        <OrderID>140408052324</OrderID>
        <UserID>94236</UserID>
        <OEM>This is a redistributable license</OEM>
        <Products>
            <Product>Aspose.Total for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SerialNumber>9a59547c-41f0-428b-ba72-7c4368f151d7</SerialNumber>
        <SubscriptionExpiry>20221231</SubscriptionExpiry>
        <LicenseVersion>3.0</LicenseVersion>
        <LicenseInstructions>http://www.aspose.com/corporate/purchase/license-instructions.aspx</LicenseInstructions>
    </Data>
    <Signature>FO3PHsblgDt8F59sMT1l1amyi9qk2V6E8dQkIP7LdTJSxDibNEFu1zOinQbqFfKv/ruttvcxoROkc1tUe0DtO6cP1Zf6J0VemgSY8i/LZECTGszRqJVQRZ0MoVnBhuPAJk5eli7fhVcF8hWd3E4XQ3LzfmJCuaj2NEteRi5Hrfg=</Signature>
</License>

因为jar已破解其核心验证方法,里面的签名可以随便填写,但是格式尽量保持一致,因为验证其他的格式方法还在!

运行成功截图

Java实现Excel文件转PDF(无水印无限制)

Java实现Excel文件转PDF(无水印无限制)

总结

经测试转换时间在几秒之内,样式没有错乱,只是当Excel的表格宽度,大于pdf的宽度时候,转换后部分内容后不显示。

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Java实现Excel文件转PDF(无水印无限制) http://www.20zxx.cn/463099/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务