package shop.alien.util.openoffice;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NewOpenOffice {

	public static void main(String[] args) {
		String fileName = "1.docx";
		String tmpdir = "D:\\MyTest\\file\\";
		File file = new File(tmpdir + fileName);
		if (!file.exists()) {
			System.err.println("文件不存在");
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
		String timesuffix = sdf.format(date);
		String htmFileName = null;
		htmFileName = timesuffix + ".pdf";
		File htmlOutputFile = new File(tmpdir + File.separatorChar + htmFileName);
		if (htmlOutputFile.exists())
			htmlOutputFile.delete();
		try {
			htmlOutputFile.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		/**
		 * 由fromFileInputStream构建输入文件
		 */
		OpenOfficeConnection connection = new SocketOpenOfficeConnection("192.168.3.230", 8100);
		try {
			connection.connect();
		} catch (ConnectException e) {
			System.err.println("请检查OpenOffice服务是否启动");
		}
		DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
		try {
			converter.convert(file, htmlOutputFile);
		} catch (OpenOfficeException ex) {
			System.err.println("文件转换出错");
		}
		connection.disconnect();
		System.out.println(htmlOutputFile);
	}
}
