ppc84222 2017-06-30 06:49 采纳率: 0%
浏览 2724

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条回答

  • 非凡巅峰 2017-06-30 09:15
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件