libreOffice将文档转换pdf的java代码

public String getLinuxOfficeHome() {
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
return "/opt/libreoffice5.3/program/soffice";
} else if (Pattern.matches("Windows.*", osName)) {
return "C:\LibreOffice\program\soffice.exe";
}
return null;
}
/**
* libreOffice调用命令启动及转换
* @param sourceFile
* @return
*/
public int libreOffice2PDF(String sourceFile) {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;//文件不存在
}
String OpenOffice_HOME = getLinuxOfficeHome();
String path = sourceFile.substring(0,sourceFile.lastIndexOf(File.separator));
// 启动OpenOffice的服务
String command = OpenOffice_HOME + " --convert-to pdf:writer_pdf_Export --outdir "+path+" "+sourceFile;
Process pro = null;
log.error(command);
try {
pro = Runtime.getRuntime().exec(command);
InputStream in = pro.getErrorStream();
while (in.read() != -1) {
System.out.println(in.read());
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return -2;
}
pro.destroy();
log.debug(sourceFile+":转换成功");
return 0;
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问