Commit 151cb447 by guanguan1009

pdf转pptx

parents
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jtyjy</groupId>
<artifactId>pdf2pptx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pdf2pptx</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!-- https://mvnrepository.com/artifact/com.lowagie/itext -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.jtyjy.pdf2pptx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Pdf2pptxApplication {
public static void main(String[] args) {
SpringApplication.run(Pdf2pptxApplication.class, args);
}
}
package com.jtyjy.pdf2pptx.converter;
import com.lowagie.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Pdf2Png {
private static final Logger logger = LoggerFactory.getLogger(Pdf2Png.class);
private static int startPage = 0;
private static int endPage = 0;
public static void main(String[] args) {
pdf2Image("F:\\测试文档\\19届高三英语人教版第1期(1).pdf", "F:\\pdf图片", 350);
}
/***
* PDF文件转PNG图片,全部页数
*
* @param PdfFilePath pdf完整路径
* @param PdfFilePath 图片存放的文件夹
* @param dpi dpi越大转换后越清晰,相对转换速度越慢
* @return
*/
public static void pdf2Image(String PdfFilePath, String dstImgFolder, int dpi) {
File file = new File(PdfFilePath);
PDDocument pdDocument = null;
try {
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
// 获取图片文件名
String imagePDFName = file.getName().substring(0, dot);
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
// 获取图片存放的文件夹路径
imgFolderPath = imgPDFPath + File.separator + imagePDFName+"_图片";
} else {
imgFolderPath = dstImgFolder + File.separator + imagePDFName+"_图片";
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
if (pages > 30) {
do {
pdDocument = PDDocument.load(file);
renderer = new PDFRenderer(pdDocument);
startPage = endPage;
endPage = startPage + 30;
if (endPage > pages) {
endPage = pages;
}
ToImage(PdfFilePath, dpi, imagePDFName, imgFolderPath, renderer, endPage, startPage);
reader.close();
pdDocument.close();
} while (endPage < pages);
} else {
endPage = pages;
ToImage(PdfFilePath, dpi, imagePDFName, imgFolderPath, renderer, endPage, startPage);
reader.close();
pdDocument.close();
}
startPage = 0;
endPage = 0;
} else {
logger.info("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
}
} catch (IOException e) {
logger.error("转换PDF文档:{} 错误:{}", PdfFilePath, e);
}
}
private static void ToImage(String PdfFilePath, int dpi, String imagePDFName, String imgFolderPath, PDFRenderer renderer, int endPage,int startPage) throws IOException {
StringBuffer imgFilePath;
for (int i = startPage; i < endPage; i++) {
String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append(String.valueOf(i + 1));
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
}
logger.info("PDF文档:{} 转PNG图片成功!{}页-{}页", PdfFilePath, startPage + 1, endPage);
}
private static boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
}
package com.jtyjy.pdf2pptx.converter;
import com.jtyjy.pdf2pptx.support.Constant;
import com.jtyjy.pdf2pptx.support.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* pdf转pptx
* 常量Constant.SOURCE_PATH为待转换文件根目录
*
* @author guan
*/
@Component
public class Pdf2Pptx {
private static final Logger logger = LoggerFactory.getLogger(Pdf2Pptx.class);
public static void main(String[] args) {
//读取指定文件夹下所有文件并转换为图片
List<String> allFilePathName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(Constant.SOURCE_PATH, allFilePathName);
for (String filePathName : allFilePathName) {
File file = new File(filePathName);
if (file.getName().endsWith(".pdf")) {
//判断图片是否存在
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(Constant.SOURCE_PATH, allDirPathName);
if (!allDirPathName.contains(file.getAbsolutePath().replace(".pdf", "_图片"))) {
logger.info("转换pdf:{} 为图片开始", file.getAbsolutePath());
long begin = System.currentTimeMillis();
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 350);
long end = System.currentTimeMillis();
logger.info("转换pdf:{} 为图片完成,耗时{}ms", file.getAbsolutePath(), end - begin);
} else {
logger.info("文件:{} 已经转换", file.getName());
}
}
}
logger.info("======== pdf转图片已完成,准备生成pptx ========");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//将图片插入PPT
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(Constant.SOURCE_PATH, allDirPathName);
for (String dirPathName : allDirPathName) {
File dir = new File(dirPathName);
//判断ppt是否存在
if (dir.getName().endsWith("_图片")) {
List<String> allPptPathFileName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(Constant.SOURCE_PATH, allPptPathFileName);
if (!allPptPathFileName.contains(dir.getAbsolutePath().replace("_图片", ".pptx"))) {
List<String> filePathNames = FileUtil.getAllFilePathName(dir.getAbsolutePath());
//转换
logger.info("开始转换图片:{} 为pptx", dir.getName());
long begin = System.currentTimeMillis();
Png2Pptx.createPicturePptx(filePathNames, dir);
long end = System.currentTimeMillis();
logger.info("转换图片:{} 为pptx完成,耗时:{}ms", dir.getName(), end - begin);
} else {
logger.info("文件:{} 已经存在", dir.getName() + ".pptx");
}
}
}
}
public void converte(String path){
//读取指定文件夹下所有文件并转换为图片
/*List<String> allFilePathName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allFilePathName);
for (String filePathName : allFilePathName) {
File file = new File(filePathName);
if (file.getName().endsWith(".pdf")) {
//判断图片是否存在
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(path, allDirPathName);
if (!allDirPathName.contains(file.getAbsolutePath().replace(".pdf", "_图片"))) {
logger.info("转换pdf:{} 为图片开始", file.getAbsolutePath());
long begin = System.currentTimeMillis();
Pdf2Png.pdf2Image(file.getAbsolutePath(), file.getParent(), 350);
long end = System.currentTimeMillis();
logger.info("转换pdf:{} 为图片完成,耗时{}ms", file.getAbsolutePath(), end - begin);
} else {
logger.info("文件:{} 已经转换", file.getName());
}
}
}
logger.info("======== pdf转图片已完成,准备生成pptx ========");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//将图片插入PPT
List<String> allDirPathName = new ArrayList<>();
FileUtil.getAllDirPathNameRe(path, allDirPathName);
for (String dirPathName : allDirPathName) {
File dir = new File(dirPathName);
//判断ppt是否存在
if (dir.getName().endsWith("_图片")) {
List<String> allPptPathFileName = new ArrayList<>();
FileUtil.getAllFilePathNameRe(path, allPptPathFileName);
if (!allPptPathFileName.contains(dir.getAbsolutePath().replace("_图片", ".pptx"))) {
List<String> filePathNames = FileUtil.getAllFilePathName(dir.getAbsolutePath());
//转换
logger.info("开始转换图片:{} 为pptx", dir.getName());
long begin = System.currentTimeMillis();
Png2Pptx.createPicturePptx(filePathNames, dir);
long end = System.currentTimeMillis();
logger.info("转换图片:{} 为pptx完成,耗时:{}ms", dir.getName(), end - begin);
} else {
logger.info("文件:{} 已经存在", dir.getName() + ".pptx");
}
}
}
*/
}
}
package com.jtyjy.pdf2pptx.converter;
import com.jtyjy.pdf2pptx.support.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
public class Png2Pptx {
private static final Logger logger = LoggerFactory.getLogger(Png2Pptx.class);
public static void main(String[] args) throws Exception {
// XSLFTextBox textBox = slide.createTextBox();
// textBox.setAnchor(new Rectangle2D.Double(10,10, 0, 0));
// textBox.addNewTextParagraph().addNewTextRun().setText("创建幻灯片");
// createPicturePptx();
}
public static void createPicturePptx(List<String> filePathNames,File dir) {
XMLSlideShow ppt = new XMLSlideShow();
String imageName = null;
try {
for (int i = 1; i <= filePathNames.size(); i++) {
imageName = dir.getName().replace("_图片","")+"_"+i+".png";
//创建幻灯片
XSLFSlide slide = ppt.createSlide();
//插入图片
byte[] bt = FileUtils.readFileToByteArray(new File(dir.getAbsolutePath() + "\\" + imageName));
XSLFPictureData xslfPictureData = ppt.addPicture(bt, XSLFPictureData.PictureType.PNG);
XSLFPictureShape pic = slide.createPicture(xslfPictureData);
pic.setAnchor(new Rectangle2D.Double(45.7, 50.8, 629, 439));
}
ppt.write(new FileOutputStream(dir.getParent() +"\\"+ dir.getName().replace("_图片", ".pptx")));
logger.info("转换:{} 完成", dir.getName());
} catch (Exception e) {
logger.error("转换:{} 错误:{}", imageName, e);
}
}
}
package com.jtyjy.pdf2pptx.service;
public interface ConverterService {
/**
* 转换pdf为pptx
* @param path 待读取文件根目录
*/
void converterPdf2Pptx(String path);
}
package com.jtyjy.pdf2pptx.service.impl;
import com.jtyjy.pdf2pptx.converter.Pdf2Pptx;
import com.jtyjy.pdf2pptx.service.ConverterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ConverterServiceImpl implements ConverterService {
@Autowired
private Pdf2Pptx pdf2Pptx;
@Override
public void converterPdf2Pptx(String path) {
pdf2Pptx.converte(path);
}
}
package com.jtyjy.pdf2pptx.support;
public class Constant {
/**
* 待读取文件根目录
*/
// public static String SOURCE_PATH = "F:\\测试文档";
public static String SOURCE_PATH = "F:\\高二";
}
package com.jtyjy.pdf2pptx.support;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
/**
* 获取某个文件夹下的所有文件全路径名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllFilePathName(String path) {
List<String> fileNameList = new ArrayList<String>();
boolean flag = false;
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
fileNameList.add(tempList[i].getAbsolutePath());
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return fileNameList;
}
/**
* 获取某个文件夹下的所有文件全路径名
*
* @param path 文件夹的路径
* @return
*/
public static List<File> getAllFiles(String path) {
List<File> fileList = new ArrayList<>();
boolean flag = false;
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
fileList.add(tempList[i]);
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return fileList;
}
/**
* 获取某个文件夹下的所有文件名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllFileName(String path) {
List<String> fileNameList = new ArrayList<String>();
boolean flag = false;
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
fileNameList.add(tempList[i].getName());
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return fileNameList;
}
/**
* 获取某个文件夹下的所有文件夹全路径名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllDirPathName(String path) {
List<String> dirNameList = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
dirNameList.add(tempList[i].getAbsolutePath());
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return dirNameList;
}
/**
* 获取某个文件夹下的所有文件夹名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllDirName(String path) {
List<String> dirNameList = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
dirNameList.add(tempList[i].getName());
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return dirNameList;
}
/**
* 获取某个文件夹下的所有文件夹名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllDirNameRe(String path, List<String> dirNameList) {
File file = new File(path);
File[] tempList = file.listFiles();
if (tempList != null) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
dirNameList.add(tempList[i].getName());
getAllDirNameRe(tempList[i].getAbsolutePath(), dirNameList);
}
}
}
return dirNameList;
}
/**
* 获取某个文件夹下的所有文件夹
*
* @param path 文件夹的路径
* @return
*/
public static List<File> getAllDirs(String path) {
List<File> dirNameList = new ArrayList<File>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
dirNameList.add(tempList[i]);
}
/*if (tempList[i].isDirectory()) {
getAllFileName(tempList[i].getAbsolutePath(),fileNameList);
}*/
}
return dirNameList;
}
/**
* 获取某个文件夹下的所有文件全路径名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllFilePathNameRe(String path, List<String> fileNameList) {
File file = new File(path);
File[] tempList = file.listFiles();
if (tempList != null) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
fileNameList.add(tempList[i].getAbsolutePath());
}
if (tempList[i].isDirectory()) {
getAllFilePathNameRe(tempList[i].getAbsolutePath(), fileNameList);
}
}
}
return fileNameList;
}
/**
* 获取某个文件夹下的所有文件夹全路径名
*
* @param path 文件夹的路径
* @return
*/
public static List<String> getAllDirPathNameRe(String path, List<String> dirNameList) {
File file = new File(path);
File[] tempList = file.listFiles();
if (tempList != null) {
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isDirectory()) {
dirNameList.add(tempList[i].getAbsolutePath());
getAllDirPathNameRe(tempList[i].getAbsolutePath(), dirNameList);
}
}
}
return dirNameList;
}
public static void main(String[] args) {
List<String> allDirPathName = getAllDirPathName("F:\\pdf图片");
for (String s : allDirPathName) {
System.out.println(s);
}
}
}
package com.jtyjy.pdf2pptx;
import com.jtyjy.pdf2pptx.service.ConverterService;
import com.jtyjy.pdf2pptx.support.Constant;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Pdf2pptxApplicationTests {
@Autowired
private ConverterService converterService;
@Test
public void converte() {
converterService.converterPdf2Pptx(Constant.SOURCE_PATH);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment