上传备份

master
王兵 4 weeks ago
commit e8757753ac

21
.gitignore vendored

@ -0,0 +1,21 @@
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
/.idea
*.iml
/.settings
/bin
/gen
/build
/gradle
/classes
.classpath
.project
*.gradle
gradlew
local.properties
node_modules/
data/

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xyz.wbsite</groupId>
<artifactId>starter-pdf2doc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>8</java.version>
</properties>
<repositories>
<!-- 将中央仓库地址指向阿里云聚合仓库,提高下载速度 -->
<repository>
<id>central</id>
<name>Central Repository</name>
<layout>default</layout>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
<pluginRepositories>
<!-- 将插件的仓库指向阿里云聚合仓库解决低版本maven下载插件异常或提高下载速度 -->
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- 糊涂工具包含常用API避免重复造轮子 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.24</version>
</dependency>
<dependency>
<groupId>spire</groupId>
<artifactId>doc</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/Spire.Doc.jar</systemPath>
</dependency>
<dependency>
<groupId>spire</groupId>
<artifactId>pdf</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/Spire.Pdf.jar</systemPath>
</dependency>
</dependencies>
</project>

@ -0,0 +1,9 @@
package xyz.wbsite.pdf2doc;
public class Demo {
public static void main(String[] args) {
String res = new PdfToWord().pdftoword("D:\\材料\\8、相关标准\\GB6944-2012 危险货物分类和品名编号.pdf");
System.out.println(res);
}
}

@ -0,0 +1,22 @@
package xyz.wbsite.pdf2doc;
import java.io.File;
public class FileDeleteTest {
//删除文件和目录
public void clearFiles(String workspaceRootPath){
File file = new File(workspaceRootPath);
if(file.exists()){
deleteFile(file);
}
}
public void deleteFile(File file){
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i=0; i<files.length; i++){
deleteFile(files[i]);
}
}
file.delete();
}
}

@ -0,0 +1,33 @@
package xyz.wbsite.pdf2doc;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import java.io.File;
public class MergeWordDocument {
public static boolean merge(String docPath,String desPath){
File[] fs = getSplitFiles(docPath);
System.out.println(docPath);
Document document = new Document(docPath+"test0.docx");
for(int i=1;i<fs.length;i++) {
document.insertTextFromFile(docPath+"test"+i+".docx",FileFormat.Docx_2013);
}
//第四步对合并的doc进行保存2
document.saveToFile(desPath);
return true;
}
// 取得某一路径下所有的pdf
private static File[] getSplitFiles(String path) {
File f = new File(path);
File[] fs = f.listFiles();
if (fs == null) {
return null;
}
return fs;
}
}

@ -0,0 +1,112 @@
package xyz.wbsite.pdf2doc;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.widget.PdfPageCollection;
import java.io.File;
public class PdfToWord {
// 涉及到的路径
// 1、pdf所在的路径真实测试种是从外部引入的
// 2、如果是大文件需要进行切分保存的子pdf路径
String splitPath = "./split/";
// 3、如果是大文件需要对子pdf文件一个一个进行转化
String docPath = "./doc/";
public String pdftoword(String srcPath) {
// 4、最终生成的doc所在的目录默认是和引入的一个地方开源时对外提供下载的接口。
String desPath = srcPath.substring(0, srcPath.length() - 4) + ".docx";
boolean result = false;
try {
// 0、判断输入的是否是pdf文件
//第一步:判断输入的是否合法
boolean flag = isPDFFile(srcPath);
//第二步:在输入的路径下新建文件夹
boolean flag1 = create();
if (flag && flag1) {
// 1、加载pdf
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile(srcPath);
PdfPageCollection num = pdf.getPages();
// 2、如果pdf的页数小于11那么直接进行转化
if (num.getCount() <= 10) {
pdf.saveToFile(desPath, FileFormat.DOCX);
}
// 3、否则输入的页数比较多就开始进行切分再转化
else {
// 第一步:将其进行切分,每页一张pdf
pdf.split(splitPath + "test{0}.pdf", 0);
// 第二步将切分的pdf一个一个进行转换
File[] fs = getSplitFiles(splitPath);
for (int i = 0; i < fs.length; i++) {
PdfDocument sonpdf = new PdfDocument();
sonpdf.loadFromFile(fs[i].getAbsolutePath());
sonpdf.saveToFile(docPath + fs[i].getName().substring(0, fs[i].getName().length() - 4) + ".docx", FileFormat.DOCX);
}
//第三步对转化的doc文档进行合并合并成一个大的word
try {
result = MergeWordDocument.merge(docPath, desPath);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
System.out.println("输入的不是pdf文件");
return "输入的不是pdf文件";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//4、把刚刚缓存的split和doc删除
if (result == true) {
new FileDeleteTest().clearFiles(splitPath);
new FileDeleteTest().clearFiles(docPath);
}
}
return "转换成功";
}
private boolean create() {
File f = new File(splitPath);
File f1 = new File(docPath);
if (!f.exists()) {
f.mkdirs();
}
if (!f.exists()) {
f1.mkdirs();
}
return true;
}
// 判断是否是pdf文件
private boolean isPDFFile(String srcPath2) {
File file = new File(srcPath2);
String filename = file.getName();
if (filename.endsWith(".pdf")) {
return true;
}
return false;
}
// 取得某一路径下所有的pdf
private File[] getSplitFiles(String path) {
File f = new File(path);
File[] fs = f.listFiles();
if (fs == null) {
return null;
}
return fs;
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.