CSDN话题挑战赛第2期
参赛话题:Java技术分享
由于市场上目前的各种格式文件的转换基本上都需要会员,怎么办呢?
这是个人新建的一个项目:gitee项目链接word
首先,创建一个springboot项目
然后由于有一个jar包无法下载,所以咱们直接去maven仓库下载
仓库地址为:maven仓库地址
下载后,放入项目中
项目pom所有内容
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.2
com.pdf
word
0.0.1-SNAPSHOT
word
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.javassist
javassist
3.20.0-GA
com.aspose
aspose-pdf
21.6
system
${project.basedir}/libs/aspose-pdf-21.6.jar
AsposeJavaAPI
Aspose Java API
https://repository.aspose.com/repo/
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
true
word
repackage
这个类PDFJarCrack 是用来破解pdf的jar包的类,将开始下载的jar全路径放入,然后运行,会在同级生成一个jar包,将原有的删掉,用新生成的替换(如果自己操作可以这样), 我上面给出的项目中的jar包已经是 生成的破解jar包
运行这个类PDFHelper3 可以直接将一个pdf转为word
破解类
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;
/**
* @date 2022-05-16
* @user tarzan
*/
public class PDFJarCrack {
public static void main(String[] args) throws Exception {
String jarPath = "D:\\word\\libs\\aspose-pdf-21.6.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();
}
}
转换类
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\\DELL\\Desktop\\20220816150519.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();
}
}
}
这里面主要是那个文件其实是需要破解的,然后我提供的链接下载中的项目 jar包已经破解过了,可以直接使用;
这里面不仅支持pdf转word,还支持其他格式的转换,感兴趣的小伙伴,可以自行尝试!!