栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 代码实现pdf转word文件 | 无水印 | 无页数限制

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 代码实现pdf转word文件 | 无水印 | 无页数限制

CSDN话题挑战赛第1期
活动详情地址:https://marketing.csdn.net/p/bb5081d88a77db8d6ef45bb7b6ef3d7f
参赛话题:Java学习记录
话题描述:可以记录一下平时学习Java中的一些知识点、心得、例题、常见的问题解决

一、前言
  • 学习概述:简单的介绍一下本篇文章要讲解的Java知识点
  • 学习目标:读者读完这篇文章之后,你希望他掌握你讲解的哪些重要的知识点
二、jar破解 1.项目远程仓库配置

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


		
			AsposeJavaAPI
			Aspose Java API
			https://repository.aspose.com/repo/
		
	
1.pom文件引入相关依赖
	
			org.javassist
			javassist
			3.20.0-GA
		
		
		
			com.aspose
			aspose-pdf
			21.8
		

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

aspose-pdf是一个处理pdf的java类库。

2.破解代码

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 PDFJarCrack {


    public static void main(String[] args) throws Exception {
        String jarPath = "E:\maven_repository\com\aspose\aspose-pdf\21.8\aspose-pdf-21.8.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 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();
    }
  • 修改你本机的aspose-pdf-21.8.jar路径,然后运行主方法,破解成功后,会再同级文件夹下生成一个aspose-pdf-21.8.cracked.jar包,用这个包替换原来的aspose-pdf-21.8.jar包即可。

控制台输出

 

 

三、pdf转word

代码实现

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;

import java.io.*;

public class PDFHelper3 {

    public static void main(String[] args) throws IOException {
        pdf2doc("C:\Users\liuya\Desktop\pdf\中信重工铸锻公司数字化工厂技术要求.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();
        }
    }


}
  • 修改自己的pdf文件路径,运行主方法即可,完成无水印,无页数限制转换word文件

控制台输出

 

 

CSDN话题挑战赛第1期
活动详情地址:https://marketing.csdn.net/p/bb5081d88a77db8d6ef45bb7b6ef3d7f 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号