doc或docx(word)或image类型文件批量转PDF脚本

1.实际生产环境中遇到文件展示只能适配PDF版本的文件,奈何一万个文件有七千个都是word或者image类型的,由此搞个脚本批量转换下上传至OSS,为前端提供数据支撑。

2.环境准备,这里使用的是aspose-words-18.6-jdk16-crack.jar工具包,资源包就不提供了,网上百度一下即可。

3.javaMaven项目,jdk1.8.maven3.6

4.使用aspose-words-18.6-jdk16-crack.jar工具包会产生水印,需要配置resources下去除水印配置:

<?xml version="1.0" encoding="UTF-8" ?>
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

license.xml

5.工具类编写:

package org.utiles.dongl.tools;

import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.log4j.Logger;
import org.utiles.dongl.comment.WordTranPDF; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.List; /**
* @ClassName: FileTranPDFTool
* @Description TODO
* @Author: 东霖
* @Date: 2022/7/23 10:50
* @Version 1.0
**/
public class FileTranPDFTool {
private static Logger logger = Logger.getLogger(FileTranPDFTool.class); public static boolean getLicense() {
boolean result = false;
try {
InputStream is = WordTranPDF.class.getClassLoader().getResourceAsStream("\\license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
} /**
* ImageToPDF
* 支持类型:jpg/tif/..
*
* @param source
* @param target
*/
public static void ImageToPDF(String source, String target) {
Document document = new Document();
//设置文档页边距
document.setMargins(0, 0, 0, 0);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(target);
PdfWriter.getInstance(document, fos);
//打开文档
document.open();
//获取图片的宽高
Image image = Image.getInstance(source);
float imageHeight = image.getScaledHeight();
float imageWidth = image.getScaledWidth();
//设置页面宽高与图片一致
Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
document.setPageSize(rectangle);
//图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
} catch (Exception ioe) {
System.out.println(ioe.getMessage());
} finally {
//关闭文档
document.close();
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
* word 文档类型转pdf
*
* @param inPath
* @param outPath
* @return
*/
public static boolean doc2pdf(String inPath, String outPath) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return false;
}
FileOutputStream os = null;
try {
File file = new File(outPath); // 新建一个空白pdf文档
os = new FileOutputStream(file);
com.aspose.words.Document doc = new com.aspose.words.Document(inPath); // Address是将要被转化的word文档
// doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
} /**
* 遍历指定目录取文件名称
*
* @param foldPath 文件目录绝对路径
* @return
*/
public static List<String> listFileName(String foldPath) {
List<String> listFiles = new ArrayList<>();
//创建文件对象
File f = new File(foldPath);
//列出文件名称存入数组
File[] files = f.listFiles();
for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
listFiles.add(files[i].getName());
}
return listFiles;
} /**
* 删除指定文件
* @param filePath
* @return
*/
public static boolean deleteByFilePath(String filePath) {
File file = new File(filePath);
return file.delete();
} /**
* 遍历指定目录取文件名称并接入路径
*
* @param oldPath 遍历文件目录绝对路径,也是要删除的文件目录
* @return
*/
public static Map<String, String> listFileNameAndPath(String oldPath) {
Map<String, String> listFiles = new HashMap();
//创建文件对象
File f = new File(oldPath);
//列出文件名称存入数组
File[] files = f.listFiles();
for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
listFiles.put(files[i].getPath(), files[i].getName());
}
return listFiles;
} /**
* 获取指定文件目录文件大小为0Size的
* @param foldPath
* @return
*/
public static Integer getFileSize(String foldPath,String newFoldPath) {
int j=1;
//创建文件对象
File file = new File(foldPath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].length()==0){
Boolean aBoolean = WriteToFileExample.moveFileToTarget("D:\\OSS\\ghwb\\ghksj_1_copy\\《金东区卫生健康事业发展“十四五”规划》.pdf", newFoldPath+files[i].getName(),null);
if (aBoolean==true){
j++;
logger.info("移动:"+files[i].getPath()+"到"+newFoldPath);
}
System.out.println(files[i].getPath());
}
}
return j;
} /**
* 文件对比删除重复文件
* @param oldFileNames
* @param newPath 对比文件目录
* @return
*/
public static Integer deleteByFileName(Map<String, String> oldFileNames, String newPath) {
int j = 0;
List<String> newListNames = listFileName(newPath);
for (Map.Entry<String, String> entry : oldFileNames.entrySet()) {
for (int i = 0; i < newListNames.size(); i++) {
String value = entry.getValue();
String s = newListNames.get(i);
if (value.substring(0,value.lastIndexOf(".")).equals(s.substring(0,s.lastIndexOf(".")))) {
boolean b = deleteByFilePath(entry.getKey());
if (b==true){
logger.info("成功删除指定文件:"+entry.getKey()+",共计:"+j+"个");
j++;
}else{
logger.error("指定文件不存在:"+entry.getKey());
}
}
}
}
return j;
} public static void main(String[] args) {
//文件对比删除
Map<String, String> map = listFileNameAndPath("D:\\OSS\\ghwb\\word");
int b = deleteByFileName(map, "D:\\OSS\\ghwb\\ghksj - 副本");
//word转pdf
doc2pdf("D:\\OSS\\ghwb\\13c5ad939a0b2001.doc",
"D:\\OSS\\ghwb\\doc2docx\\13c5ad939a0b2001.docx");
//移动文件size为0的数据到指定文件夹
// getFileSize("D:\\OSS\\ghwb\\ghksj_3_copy","D:\\OSS\\ghwb\\test");
}
}

WordORImageTranPDF

6.逻辑代码:

package org.utiles.dongl.comment;

import org.apache.log4j.Logger;
import org.utiles.dongl.tools.FileTranPDFTool;
import org.utiles.dongl.tools.WriteToFileExample; import java.io.*;
import java.util.HashMap;
import java.util.Map; import static org.utiles.dongl.tools.FileTranPDFTool.doc2pdf; /**
* @ClassName: WordTranPDF
* @Description TODO
* @Author: 东霖
* @Date: 2022/7/22 8:55
* @Version 1.0
**/
public class WordTranPDF {
private static Logger logger = Logger.getLogger(WordTranPDF.class); /**
* 获取指定文件路径下所有文件对象
*
* @param inFilePath
* @return
*/
public static Map<String, String> getFilePathName(String inFilePath,String replacePathOld
,String replacePathNew,String wjjl,String pdfToPath) {
Map<String, String> fileList = new HashMap();
//创建文件对象
File f = new File(inFilePath);
//列出文件名称存入数组
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].getName().endsWith("docx") || files[i].getName().endsWith("doc")
|| files[i].getName().endsWith("wps") || files[i].getName().endsWith("rtf"))
{
// String str=files[i].getPath().substring(0,files[i].getPath().lastIndexOf(".")+1)+"pdf";
String str=files[i].getPath().substring(0,files[i].getPath().lastIndexOf(".")+1)+"docx";
fileList.put(files[i].getPath()+"&"+"word",str.replace(replacePathOld,replacePathNew));
// logger.info("当前文件路径为:"+files[i].getPath());
} else if (files[i].getName().endsWith(".png") || files[i].getName().endsWith(".jpg") || files[i].getName().endsWith(".gif")
|| files[i].getName().endsWith(".jpeg") || files[i].getName().endsWith(".tif"))
{
String str=files[i].getPath().substring(0,files[i].getPath().lastIndexOf(".")+1)+"pdf";
fileList.put(files[i].getPath()+"&"+"image", str.replace(replacePathOld,replacePathNew));
// logger.info("当前文件路径为:"+files[i].getPath());
}else if(files[i].getName().endsWith(".pdf")) {
WriteToFileExample.moveFileToTarget(files[i].getPath(),pdfToPath+files[i].getName(),"");
logger.info("移动:"+files[i].getPath()+"到"+pdfToPath);
}else{
WriteToFileExample.writeFileSQL("当前文件无法转换:"+files[i].getPath(),wjjl);
}
}
return fileList;
} public static void start(Map<String, String> hashMap) throws InterruptedException {
long old = System.currentTimeMillis();
int j = 0;
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
// doc2pdf(entry.getKey(),entry.getValue());
String[] split = entry.getKey().split("&");
if(split[1].equals("word")){
System.out.println(entry.getValue());
doc2pdf(split[0],entry.getValue());
Thread.sleep(Long.parseLong("15"));
}else if (split[1].equals("image")){
FileTranPDFTool.ImageToPDF(split[0],entry.getValue());
Thread.sleep(Long.parseLong("15"));
}else {
// break;
}
j++;
logger.info("转换第:"+j+"个!"+"文件名称为:"+entry.getKey());
}
long now = System.currentTimeMillis();
logger.info("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
logger.info("共转换:" + j + "个文件!");
} public static void main(String[] args) throws InterruptedException {
/**
* inFilePath: 需要转换的文件夹路径
* replacePathOld: 抓换后的文件要写入新文件,直接替换文件的上级目录关键字即可
* replacePathNew: 新的文件父路径
* wjjl: 不能转换的文件记录位置及记录名称
* pdfToPath:当文件中已有pdf不用抓换的需配置文件留存方向。会从原文件目录移动至新文件目录
*/
Map<String, String> filePathName = getFilePathName("D:\\OSS\\ghwb\\doc11",
"doc11","doc2docx",
"D:\\OSS\\ghwb\\"+System.currentTimeMillis()+".txt"
,"D:\\OSS\\yjbg\\gjxxzx\\ghksj_copy\\");
start(filePathName);
}
}

7.上述就是word或者image类型的批量脚本,可以在工具类中单元测试之后在使用批量逻辑代码。

doc或docx(word)或image类型文件批量转PDF脚本的更多相关文章

  1. 将doc文件批量转为pdf文件

    需要将不少doc文件转为pdf,WPS带有这种功能,但是鼠标点击次数太多以后整个人都变得很烦躁 用了一下午去搜这方面的工具软件,找到若干.有一些免费,有一些试用的,但总归就找到一个真正能用,虽说生成的 ...

  2. C# 将Word,Execl,PPT,Project, 文件转成PDF, 不依赖Office!!

    git 地址 https://gitee.com/bandung/Execl_WordTOPDF.git 包括了各种破解的dll Word转PDF 挨个引用 Word转PDF public void ...

  3. 文件批量scp分发脚本

    #!/bin/bash SERVERS="172.17.xx.y 172.17.pp.mm" PASSWORD=机器登录密码 auto_ssh_copy_file() { expe ...

  4. 个人永久性免费-Excel催化剂功能第88波-批量提取pdf文件信息(图片、表格、文本等)

    日常办公场合中,除了常规的Excel.Word.PPT等文档外,还有一个不可忽略的文件格式是pdf格式,而对于想从pdf文件中获取信息时,常规方法将变得非常痛苦和麻烦.此篇给大家送一pdf文件提取信息 ...

  5. 基于java 合并.doc和docx格式的Word文件

    注:摘录自 https://www.cnblogs.com/shenzhouyh/articles/7243805.html 之前用过jacob 合并.doc,但是是有jacob有弊端: 服务器必须是 ...

  6. C#仪器数据文件解析-Word文件(doc、docx)

    不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...

  7. Python:读取 .doc、.docx 两种 Word 文件简述及“Word 未能引发事件”错误

    概述 Python 中可以读取 word 文件的库有 python-docx 和 pywin32. 下表比较了各自的优缺点.   优点 缺点 python-docx 跨平台 只能处理 .docx 格式 ...

  8. python对不同类型文件(doc,txt,pdf)的字符查找

    python对不同类型文件的字符查找 TXT文件: def txt_handler(self, f_name, find_str): """ 处理txt文件 :param ...

  9. 手把手教你用 Spring Boot搭建一个在线文件预览系统!支持ppt、doc等多种类型文件预览

    昨晚搭建环境都花了好一会时间,主要在浪费在了安装 openoffice 这个依赖环境上(Mac 需要手动安装). 然后,又一步一步功能演示,记录,调试项目,并且简单研究了一下核心代码之后才把这篇文章写 ...

随机推荐

  1. 【万字长文】使用 LSM Tree 思想实现一个 KV 数据库

    目录 设计思路 何为 LSM-Treee 参考资料 整体结构 内存表 WAL SSTable 的结构 SSTable 元素和索引的结构 SSTable Tree 内存中的 SSTable 数据查找过程 ...

  2. 每天一个 HTTP 状态码 204

    204 No Content 204 No Content 表示服务器成功地处理了客户端的请求,但是没有任何要响应的内容.API 设计上,在用 PUT 请求更新某个资源成功后,后端可以在 HTTP 响 ...

  3. 【单片机】CH32V103C8T6定时器3程序实验

    代码功能:每隔1毫秒进入一次定时器中断. 每隔1秒串口打印一次数据. time.c #include "time.h" #include "ch32v10x.h" ...

  4. CSS3:scrollbar样式设置

    CSS3:scrollbar样式设置 1. 设置出现滚动条的方式 overflow:scroll --- x和y方向都会出现滚动条 或者 overflow-x:scroll --- 只有x方向出现滚动 ...

  5. 搭建自己的个人web项目指南 ---(一)服务器购买与基础配置 | windows连接到自己的云服务器

    (一)服务器购买与基础配置 | windows连接到自己的云服务器 一.服务器选购指南 厂商选择 目前市面上提供服务器租用的厂商很多,比较知名的还是阿里云和腾讯云,两家的稳定性都非常不错,小伙伴们可以 ...

  6. 跟着Vam一起学习Typescript(第一期)

    一.安装环境与配置1.命令行安装 npm i -g typescript 2.快捷打开Vs Code编辑器 创建一个项目文件夹,在该文件夹下打开命令行工具,使用code .命令快速打开编辑器(如果计算 ...

  7. MySQL数据检索时,sql查询的结果如何加上序号

    1.sql语法 @i:类型java定义的变量 @i:=0:这里类似给i初始化值为0 @i:=@i+1 :每次从0开始递增+1 SELECT (@i:=@i+1) as id,TDLINE FROM Y ...

  8. Python爬取某短视频热点

    写在前面的一些话: 随着短视频的大火,不仅可以给人们带来娱乐,还有热点新闻时事以及各种知识,刷短视频也逐渐成为了日常生活的一部分.本文以一个简单的小例子,简述如何通过Pyhton依托Selenium来 ...

  9. Python快速下载商品数据,并连接数据库,保存数据

    开发环境 python 3.8 pycharm 2021.2 专业版 代码实现 发送请求 获取数据 解析数据(筛选数据) 保存数据 连接数据库 开始代码 请求数据 # 伪装 headers = { ' ...

  10. Day02 HTML语法

    有两种类型的标签 双标记<标记名>内容</标记名> 单标记<标记名/> 属性: 对标签的描述--属性,由属性名和属性值组成 <标记名 属性名 = " ...