<?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>org.example</groupId>
<artifactId>pdf</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency><!--
<dependency>
<groupId>ework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.3</version>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</project>
package org.example.tool;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Value;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class pdf {
@Value("${png.path}")
String pngPath;
public static void main(String[] args) throws Exception {
replace("${123}", "替换替换", "D:/111.docx");
wordToPdf("D:/data/111.docx");
pdfToPng("D:/data/111.pdf");
}
//替换docx中文件内容 oldString不能是中文
public static void replace(String oldString, String newString, String path) {
try {
File file = new File(path);
BufferedReader br = new BufferedReader(new FileReader(file));
ArrayList<String> strings = new ArrayList<String>();
String s;//读取的每一行数据
while ((s = br.readLine()) != null) {
if (s.contains(oldString)) {
s = s.replace(oldString, newString);
System.out.println("替换成功");
}
strings.add(s);//将数据存入集合
}
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
for (String string : strings) {
bw.write(string);//一行一行写入数据
bw.newLine();//换行
}
bw.close();
} catch (Exception e) {
e.getMessage();
}
}
//pdf转docx
public static void wordToPdf(String wordPath) {
File inPath = new File(wordPath);
File outPath = new File(wordPath.replace(".docx", ".pdf"));
try {
InputStream docxInputStream = new FileInputStream(inPath);
OutputStream outputStream = new FileOutputStream(outPath);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
outputStream.close();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
//pdf转png
public static void pdfToPng(String pdfPath) {
try {
// PDF文件路径
String pdfFilePath = pdfPath;
// 输出目录路径
String outputDirPath = pdfPath.substring(0, pdfPath.lastIndexOf("/"));
// 加载PDF文档
PDDocument document = PDDocument.load(new File(pdfFilePath));
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 遍历PDF的每一页
for (int page = 0; page < document.getNumberOfPages(); ++page) {
// 渲染PDF页面为图像
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300); // 设置DPI为300
// 将图像保存为PNG文件
String pngFileName = outputDirPath + "/pdf" + "/page-" + (page + 1) + ".png";
ImageIO.write(bim, "png", new File(pngFileName));
}
// 关闭文档
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package org.example.tool;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import static java.lang.System.in;
public class wordAddPng {
public static void main(String[] args) throws Exception {
wordAddPng w = new wordAddPng();
String filePath = "D:/data/111.docx";
if (!Files.exists(Paths.get(filePath))){
Files.createFile(Paths.get(filePath));
}
InputStream docis = new FileInputStream("D:/data/111.docx");
//转成word
CustomXWPFDocument document = new CustomXWPFDocument(docis);
//写入图片
String s = "D:/data/111.png,D:/data/123.jpg";
String[] split = s.split(",");
List<String> list = Arrays.asList(split);
for (String s1 : list) {
w.insertImage(document, s1,filePath);
}
}
private void insertImage(CustomXWPFDocument document, String s,String filePath) throws IOException, InvalidFormatException {
//图片
System.out.println(s);
FileInputStream in = new FileInputStream(s);
//段落集合
List<XWPFParagraph> paragraphs = document.getParagraphs();
System.out.println(paragraphs.get(paragraphs.size() - 1).getText());
byte[] ba = new byte[in.available()];
int len = in.read(ba);
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(ba, 0, len);
//设置图片
if (s.contains("png")) {
document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_PNG);
} else {
document.addPictureData(byteInputStream, XWPFDocument.PICTURE_TYPE_JPEG);
}
//创建一个word图片,并插入到文档中-->像素可改
document.createPicture(document.getAllPictures().size() - 1, 240, 240, paragraphs.get(paragraphs.size() - 1));
System.out.println(document.getAllPictures().get(0));
OutputStream out = Files.newOutputStream(Paths.get(filePath));
document.write(out);
out.flush();
out.close();
}
}