#热门
Java实现pdf转word 完美转换,附代码,不限页数

2023-08-04 搭建部署 0 320
郑重承诺丨京强资源网提供安全交易、信息保真!
增值服务:
¥ 5金币

普通成员 5折升级普通成员

开通VIP尊享优惠特权
立即下载 升级会员
信息属性
  • 评分
  • 5
  • 大小
  • MB
  • 语言
  • 简体中文
  • 格式
  • 压缩文件
  • 官网
  • 来源
  • 网络
详情介绍

Java实现pdf转word 完美转换,附代码,不限页数
Java实现pdf转word 完美转换,附代码,不限页数


4.0.0

org.example
pdf2word
1

8
8


org.javassist
javassist
3.20.0-GA



org.apache.poi
poi-ooxml
4.1.2


com.aspose
aspose-pdf
22.1


org.apache.maven.plugins
maven-deploy-plugin
2.8.2
maven-assembly-plugin

false

jar-with-dependencies



demo.PDFHelper3





make-assembly package
assembly





localRepository
file:C:/Users/shao/.m2/repository


源代码

package demo;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class PDFHelper3 {

public static void main(String[] args) throws IOException {
System.out.println("请输入要转换的pdf文件路径和文件名(例如C:\\A.pdf)," +
"根据文件大小需要一点的时间,请耐心等候!可以直接拖pdf文文件进来。");
Scanner sc = new Scanner(System.in);
String path = sc.nextLine();
path = path.replace("\\\\","\\\\\\\\");
path = path.replace("\"","");
pdf2doc(path);
System.out.println("请在源文件处查看docx文件");

}

//移除文字水印
public static boolean removeWatermark(File file) {
try {
XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
// 段落
List paragraphs = doc.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
String text=paragraph.getText();
if("Evaluation Only. Created with Aspose.PDF. Copyright 2002-2021 Aspose Pty Ltd.".equals(text)){
List runs = paragraph.getRuns();
runs.forEach(e-> e.setText("",0));
}
}
FileOutputStream outStream = new FileOutputStream(file);
doc.write(outStream);
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

public static void pdf2doc(String pdfPath) {
long old = System.currentTimeMillis();
try {
//新建一个pdf文档
String wordPath=pdfPath.substring(0,pdfPath.lastIndexOf("."))+".docx";
File file = new File(wordPath);
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word文档
Document doc = new Document(pdfPath);
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.DocX);
os.close();
//去除水印
removeWatermark(new File(wordPath));
//转化用时
long now = System.currentTimeMillis();
System.out.println("Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
System.out.println("Pdf 转 Word 失败...");
e.printStackTrace();
}
}

}

资源下载此资源下载价格为5金币立即购买,普通成员 5折
收藏 (0) 打赏

感谢您的支持,我会继续努力的!