java 实现pdf 转word去除水印和去除页数限制
背景
每次下载的pdf文档想转成word文档,都从网上找各种找网址,网上的网址要么存在必须要充值,要么转化效果不好等等问题。作为一个技术人员,所以想能否实现pdf转化为word文档
代码实现
1.1 pom.xml的dependencies引入maven依赖
<dependency><groupId>org.javassist</groupId><artifactId>javassist</artifactId><version>3.20.0-GA</version></dependency><!-- https://mvnrepository.com/artifact/com.aspose/aspose-pdf --><dependency><groupId>com.aspose</groupId><artifactId>aspose-pdf</artifactId><version>21.8</version></dependency><!-- poi-ooxml是poi的升级版本--><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency>
1.2 在pom.xml中添加仓库地址
<repositories><repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://repository.aspose.com/repo/</url></repository></repositories>
2.修改jar
其中jarPath 为下载下来的依赖的jar包地址
package com.example.mycode.utils;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 = "D:\\maven\\maven-Repository\\com\\aspose\\aspose-pdf\\21.8\\aspose-pdf-21.8.jar";crack(jarPath);}private static void crack(String jarName) throws Exception {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<String> 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();}
}
执行完成后在jar包的目录下会生成aspose-pdf-21.8.cracked.jar
2.2 修改jar包名称
将原来的aspose-pdf-21.8.jar删除。将步骤2生成的jar更名为aspose-pdf-21.8.jar
2.3 源jar包,执行修改后的jar包也可以在百度云盘中下载
链接:https://pan.baidu.com/s/1P283TlJ4_D4sbxdkqkFxeA?pwd=1h3u
提取码:1h3u
3 编写转化代码
package com.example.mycode.utils;import java.io.FileOutputStream;
import java.io.IOException;import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;public class Pdf2Word {public static void main(String[] args) throws IOException {pdf2doc("E:\\tmp\\6_自如房屋租赁合同.pdf");}//pdf转docpublic 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();}}
}
执行完成后会在同级目录生成word文档。经过比对,转化效果比较好。
java 实现pdf 转word去除水印和去除页数限制相关推荐
- java实现PDF转Word(无水印无页数限制)完全开放
jar包破解 1.pom文件 分别复制进pom文件,记得配置maven的中央仓库在settings.xml中配置单独的仓库地址<repositories><repository> ...
- Java为 pdf、word和excel添加水印
1. 引入依赖 <!--easyexcel--><dependency><groupId>com.alibaba</groupId><artifa ...
- java为PDF添加水印,图片水印和文字水印
java为PDF添加水印,文字水印和图片水印 一个需求,下载pdf.word.excel文件时要带有水印,要求铺满.先分开,先介绍为PDF文件添加文字水印和图片水印. 所需jar包:itext-2.0 ...
- Java 处理PDF文档(一):页眉页脚、水印、背景、附件
前言 本文将介绍通过Java编程来处理PDF文档的一些方法,因为一篇文档的处理可能包括很多内容,比如文档安全性设置(水印.加密/解密).文本/图片/图形操作.注释.附件.域.文档转换(其他文件格式转为 ...
- PHP能获取word页数吗,php - 如何在Linux上获取Word文档中的页数?
我看到这个问题.我还需要确定给定word文件(doc/docx)的页数.我试图调查phplivedocx/zf(@hobodave链接到最初的post答案中),但我在那里失去了手脚.我也不能使用任何外 ...
- php判断caj文件页数,2个免费CAJ转PDF的方法,而且不限页数和大小
原标题:2个免费CAJ转PDF的方法,而且不限页数和大小 由于CAJ格式的文档编辑相对比较麻烦,所以日常我们都是直接将CAJ转换成PDF格式后再处理.目前可搜索到的转换工具本身就不多,那有没有不仅免费 ...
- python获取word页数_python,_如何在 Linux 上使用 Python 读取 word 文件信息(如页数)?,python - phpStudy...
如何在 Linux 上使用 Python 读取 word 文件信息(如页数)? R.T. doc 是二进制文件,Python 如何进行读取呢? .docx 可用 python-docx 读取,但如何读 ...
- java实现PDF 转WORD
CSDN话题挑战赛第2期 参赛话题:Java技术分享 引言 由于市场上目前的各种格式文件的转换基本上都需要会员,怎么办呢? 不走寻常路,我们是程序员 我们当然要不走寻常路了,我们要动用一些特殊手段,展 ...
- java实现pdf转word,解决个别排版错乱问题
项目中要实现客户上传完pdf,上传成功后直接就转成word格式的,之前网上一些免费的转出来的word大致还行,但是有些排版就错乱了,如下图: 下面这个是用java改造后的,效果如下,排版整齐,和pdf ...
最新文章
- java后台环境搭建_后端-java环境搭建
- Reflector 插件-Reflexil
- 【Tools】Centos7.5安装MySQL5.7
- D. Binary Literature
- C语言再学习——分支结构
- 机器学习笔记(十二):聚类
- algorithm头文件下的next_permutation()
- java io 创建临时文件,用Java创建一个指定的临时文件
- 认知无线电网络中的频谱切换理论
- 2021:Python的下载安装教程(很详细,初学者也能懂)
- 学习马士兵Java教程
- 皮尔逊相关系数(Pearson Correlation Coefficient)
- 三维旋转矩阵_三维重建中的旋转(Rotation)
- 【移动光猫H2-2的完全破解心路历程及配置】
- 110配线架打法图解_【布线经验】110语音配线架详细安装教程(图文)
- ASP.NET企业员工档案管理系统源码
- Halcon九点及旋转标定流程
- [转]奇文-闲话操作系统(2/4)
- nodejs+vue+elementui高校体育馆场地预订系统
- 地方门户网站如何快速发展盈利