Java Word转PDF(直接转和以图片形式转)、PDF转图片、图片转PDF
在淘宝上找了一家写代码的店铺写了一个工具类,再参考网上的代码,改了改
用到的类库:
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
<!--word转pdf-->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<!-- pdf转图片和图片转pdf-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
工具类代码:
import com.documents4j.api.DocumentType;
import com.documents4j.job.LocalConverter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class FileConvertor {
/**
* 根据word每页生成图片,再根据得到的图片,合并生成PDF
*/
public static void wordToImagePDF(String wordPath,String imageDirectory, String targetPDF) throws IOException {
//生成临时pdf文件
File tempFile = File.createTempFile("temp", ".pdf");
String temporaryPDF = tempFile.getAbsolutePath();
//将word转换为pdf,并写入临时pdf
wordToPDF(wordPath, temporaryPDF);
//将临时pdf转换为图片
List<String> images = pdfToImages(temporaryPDF,imageDirectory,300,"png");
//将转换出来的图片合并为pdf
try (PDDocument pdDocument = new PDDocument()) {
for (String imagePath : images) {
imageToPDF(imagePath, pdDocument);
}
pdDocument.save(targetPDF);
}
//删除临时pdf
Files.delete(Paths.get(temporaryPDF));
}
/**
* Word文档转换为PDF文档
* @param wordPath Word文档路径
* @param pdfPath 生成的PDF路径
*/
public static void wordToPDF(String wordPath, String pdfPath) throws IOException {
File inputWord = new File(wordPath);
File outputFile = new File(pdfPath);
InputStream docxInputStream = Files.newInputStream(inputWord.toPath());
OutputStream outputStream = Files.newOutputStream(outputFile.toPath());
LocalConverter.builder().build().convert(docxInputStream)
.as(DocumentType.DOCX)
.to(outputStream)
.as(DocumentType.PDF).execute();
outputStream.close();
docxInputStream.close();
}
/**
* PDF文件转图片(word一页对应一个图像)
* 图片将保存文件夹下,根据页数命名
* @param pdfPath PDF文件路径
* @param imageDirectory 生成的图片保存的文件夹
* @param dpi 生成图片dpi
* @param formatName 生成图片的格式
*/
public static List<String> pdfToImages(String pdfPath,String imageDirectory,
float dpi, String formatName) throws IOException {
File file = new File(pdfPath);
ArrayList<String> list = new ArrayList<>();
try (PDDocument doc = PDDocument.load(file)) {
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
int pageIndex = i + 1;
String imagePath = imageDirectory +File.separator + pageIndex + "."+formatName;
ImageIO.write(image, formatName, new File(imagePath));
list.add(imagePath);
}
}
return list;
}
/**
* 图片转PDF
*/
public static void imageToPDF(String imagePath, PDDocument document) throws IOException {
//获取图片长宽
File file = new File(imagePath);
String fileSuffix = imagePath.substring(imagePath.lastIndexOf(".") + 1);
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(fileSuffix);
ImageReader reader = readers.next();
ImageInputStream input = ImageIO.createImageInputStream(Files.newInputStream(file.toPath()));
reader.setInput(input, true);
int width = reader.getWidth(0);
int height = reader.getHeight(0);
//根据图片大小动态生成pdf页面
PDPage pdPage = new PDPage(new PDRectangle(width, height));
document.addPage(pdPage);
PDImageXObject pdImageXObject = PDImageXObject.createFromFile(imagePath, document);
PDPageContentStream contentStream = new PDPageContentStream(document, pdPage);
//写入图片
contentStream.drawImage(pdImageXObject, 0, 0);
contentStream.close();
input.close();
}
}