也感谢有一个技术群里的朋友帮助,非常感谢
/**
*
* @Title:OpenOffice工具类
* @description:利用OpenOffice将office的各种文件转为pdf
* @author 何泳锋
* @date 20180515
* @version V1.0
*/
public class OpenOfficeUtil {
private static OfficeManager manager;
/**
* 文档2pdf(自动启动服务)
*
* @param inputFile
* @param outputFile
* @return
* @throws Exception
* @date 2018年3月11日
*/
public static boolean doc2pdf(File inputFile, File outputFile) {
boolean result = false;
if (inputFile.exists()) {
if (!outputFile.exists()) {
// 初始化OpenOffice配置
initOfficeManagerConfiguration();
// 启动服务
manager.start();
// 开始转换
OfficeDocumentConverter converter = new OfficeDocumentConverter(manager);
converter.convert(inputFile, outputFile);
// 停止服务
manager.stop();
result = true;
} else {
result = true;
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****需要转换的文档不存在,无法转换,文件路径=" + inputFile.getAbsolutePath() + "****");
}
return result;
}
/**
* 初始化OpenOffice配置
*
* @date 2018年5月16日
*/
private static void initOfficeManagerConfiguration() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
// configuration.setWorkDir(new File("/opt"));
String officeHome = getOfficeHome();
configuration.setOfficeHome(officeHome);// 设置安装目录
configuration.setPortNumbers(8100); // 设置端口
configuration.setTaskExecutionTimeout(1000 * 60 * 5L); // 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); // 设置任务队列超时为24小时
manager = configuration.buildOfficeManager();
if (manager == null)
throw new RuntimeException("初始化OpenOffice失败");
}
/**
* 获取OpenOffice的安装路径
*
* @date 2018年5月16日
*/
public static String getOfficeHome() {
String osName = System.getProperty("os.name");
System.out.println("操作系统名称:"+osName);
if (Pattern.matches("Linux.*", osName)) {
return "/opt/openoffice.org4";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:\\Program Files (x86)\\OpenOffice 4";
} else if (Pattern.matches("Mac.*", osName)) {
return "/Application/OpenOffice.org.app/Contents";
}
return null;
}
public static void main(String[] args){
String infile = "D:\\upload\\admin\\2018\\05\\15\\1.xlsx";
String targetfile = "D:\\upload\\admin\\2018\\05\\15\\1.pdf";
File inFile = new File(infile);
File targetFile = new File(targetfile);
try {
doc2pdf(inFile,targetFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
*
* @Title:IText工具类
* @description:利用IText将txt文本转为pdf
* @author 何泳锋
* @date 20180516
* @version V1.0
*/
public class ITextUtil {
public static boolean text2pdf(String text,String pdf){
try{
File pdfFile = new File(pdf);
if(!pdfFile.exists()){
//获取txt文本的编码类型
String charsetName = CpdetecorUtil.getFileEncode(text);
/*BaseFont baseFont = getBaseFont();*/
//直接把字体放到项目的resources中,因为服务器是在linux系统
String fontPath = ITextUtil.class.getClassLoader().getResource("font/SIMHEI.TTF").getPath();
BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(baseFont, 12, Font.NORMAL);
FileOutputStream out = new FileOutputStream(pdf);
Rectangle rect = new Rectangle(PageSize.A4.rotate());
Document doc = new Document(rect);
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.open();
Paragraph p = new Paragraph();
p.setFont(FontChinese);
FileInputStream fileInstream = new FileInputStream(new File(text));
InputStreamReader isr = new InputStreamReader(fileInstream, charsetName); //根据txt文本的编码获取流
BufferedReader read = new BufferedReader(isr);
String line = read.readLine();
while(line != null){
System.out.println(line);
p.add(line+"\n");
line = read.readLine();
}
read.close();
doc.add(p);
doc.close();
return true;
}else{
System.out.println("****已经转换为pdf,不需要再进行转化****");
return true;
}
}catch(DocumentException e){
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 根据当前操作系统获取baseFont
* */
public static BaseFont getBaseFont() throws DocumentException, IOException {
String osName = System.getProperty("os.name");
System.out.println("操作系统名称:"+osName);
if (Pattern.matches("Linux.*", osName)) {
return BaseFont.createFont("/usr/share/fonts/fallback/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} else if (Pattern.matches("Windows.*", osName)) {
return BaseFont.createFont("C:\\Windows\\Fonts\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
return null;
}
public static void main(String[] args) throws Exception {
String text = "D:\\files\\11.txt";
String pdf = "D:\\files\\11.pdf";
text2pdf(text, pdf);
}
}
/**
*
* @Title:Cpdetecor工具类
* @description:判断文件编码
* @author 何泳锋
* @date 20180516
* @version V1.0
*/
public class CpdetecorUtil {
public static String getFileEncode(String filePath) {
String charsetName = null;
try {
File file = new File(filePath);
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
Charset charset = null;
charset = detector.detectCodepage(file.toURI().toURL());
if (charset != null) {
charsetName = charset.name();
} else {
charsetName = "UTF-8";
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return charsetName;
}
public static void main(String[] args) {
String filePath = "D:\\upload\\admin\\2018\\05\\16\\297e0a366366a7a9016366b330cd0006.txt";
String charsetName = getFileEncode(filePath);
System.out.println(charsetName);
}
}