package cn.csu.utils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* Created by 胡歌的小迷弟 on 2019/9/9 14:17
*/
public class Main {
public static void main(String[] args) throws Exception {
//pdf2png("C:\\Users\\wy\\Documents\\Tencent Files\\920937242\\FileRecv","3","png");
png2word("C:\\Users\\wy\\Documents\\Tencent Files\\920937242\\FileRecv\\1\\3-代表论文1\\11_1.png");
}
/**
* 转换全部的pdf
* @param fileAddress 文件地址
* @param filename PDF文件名
* @param type 图片类型
*/
public static void pdf2png(String fileAddress,String filename,String type) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(fileAddress+"\\"+filename+".pdf");
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, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(fileAddress+"\\1"+filename+"_"+(i+1)+"."+type));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*自由确定起始页和终止页
* @param fileAddress 文件地址
* @param filename pdf文件名
* @param indexOfStart 开始页 开始转换的页码,从0开始
* @param indexOfEnd 结束页 停止转换的页码,-1为全部
* @param type 图片类型
*/
public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) {
// 将pdf装图片 并且自定义图片得格式大小
File file = new File(fileAddress+"\\"+filename+".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = indexOfStart; i < indexOfEnd; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
// BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void png2word(String filename) throws Exception {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph title = doc.createParagraph();
XWPFRun run = title.createRun();
// run.setText("Fig.1 A Natural Scene");
run.setBold(true);
title.setAlignment(ParagraphAlignment.CENTER);
String imgFile = filename;
FileInputStream is = new FileInputStream(imgFile);
run.addBreak();
run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
is.close();
File file=new File("c:\\zdcc\\test4.docx" );
if(!file.exists()){
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
doc.write(fos);
fos.close();
}
}
<dependency>
<groupId>xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>