package com.ntc.openoffice;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.commons.io.FilenameUtils;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
public class PDFDemo {
public static boolean officeToPDF(String sourceFile, String destFile) {
try {
String OpenOffice_HOME = "D:\\Users\\OpenOffice 4\\";
//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的
// 启动OpenOffice的服务
String command = OpenOffice_HOME + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
Process pro = Runtime.getRuntime().exec(command);
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
// 找不到源文件, 则返回false return false;
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
//用于测试openOffice连接时间
System.out.println("连接时间:" + df.format(new Date()));
DocumentConverter converter = new ConverterDocument(connection);
converter.convert(inputFile, outputFile);
//测试word转PDF的转换时间
System.out.println("转换时间:" + df.format(new Date()));
//关闭服务
// close the connection
connection.disconnect();
// 关闭OpenOffice服务的进程
pro.destroy();
return true;
} catch (ConnectException e) {
e.printStackTrace();
System.err.println("openOffice连接失败!请检查IP,端口");
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 将pdf转换成图片
*
* @param pdfPath
* @param imagePath
* @return 返回转换后图片的名字
* @throws Exception
*/
public static List<String> pdf2Imgs(String pdfPath, String imgDirPath) throws Exception {
Document document = new Document();
document.setFile(pdfPath);
float scale = 5f;//放大倍数
float rotation = 0f;//旋转度数
List<String> imgNames = new ArrayList<String>();
int pageNum = document.getNumberOfPages();
File imgDir = new File(imgDirPath);
if (!imgDir.exists()) {
imgDir.mkdirs();
}
for (int i = 0; i < pageNum; i++) {
BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
try {
String filePath = imgDirPath + File.separator + i + ".jpg";
File file = new File(filePath);
ImageIO.write(rendImage, "jpg", file);
imgNames.add(FilenameUtils.getName(filePath));
} catch (IOException e) {
e.printStackTrace();
return null;
}
image.flush();
}
document.dispose();
return imgNames;
}
public static void doc2Pdf(String docPath, String pdfPath) throws ConnectException {
File inputFile = new File(docPath);
File outputFile = new File(pdfPath);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//converter.convert(inputFile, outputFile);
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
DocumentFormat txt = formatReg.getFormatByFileExtension("odt") ;
DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf") ;
converter.convert(inputFile, txt, outputFile, pdf);
connection.disconnect();
}
public static void doc2Imags(String docPath, String imgDirPath){
String pdfPath =String.format("%s%s.pdf", FilenameUtils.getFullPath(docPath), FilenameUtils.getBaseName(docPath));
try {
doc2Pdf(docPath, pdfPath);
pdf2Imgs(pdfPath, imgDirPath);
File pdf = new File(pdfPath);
if(pdf.isFile()){
pdf.delete();
}
} catch (ConnectException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//转pdf
officeToPDF("E:\\test.xlsx", "E:\\test.pdf");
//专图片194625406
String docPath = "E:\\test.xlsx";
String pdfPath = "E:\\test";
doc2Imags(docPath, pdfPath);
}
}