- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.ConnectException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.SortedMap;
- import java.util.SortedSet;
- import java.util.concurrent.ConcurrentSkipListMap;
- import java.util.concurrent.ConcurrentSkipListSet;
- import pde.ict.common.DmsTools;
- import pde.ict.service.utils.AttachTempDirClearUtil;
- import pde.ict.service.utils.ClientConverterUtil;
- import pde.ict.service.utils.PDEConstant;
- import com.artofsolving.jodconverter.DocumentConverter;
- import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
- import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
- public class OfficeToPdfUtil {
- private String openOffice_HOME = "C:\\Program Files\\OpenOffice.org 3";
-
- private static final int CONVERT_TYPE = 2; //PDF转换方式 1:openOffice服务转换 , 2:Office服务转换
-
- private String host= "127.0.0.1";
-
- private String port = "8100";
-
- private PdfUtil pdfUtil = null;
-
- public PdfUtil getPdfUtil() {
- if(pdfUtil==null){
- pdfUtil = new PdfUtil();
- }
- return pdfUtil;
- }
- public void setPdfUtil(PdfUtil pdfUtil) {
- this.pdfUtil = pdfUtil;
- }
-
- /**
- * pdf文件缓存列表
- * <源文件,目标PDF文件>
- */
- private SortedMap<String,PdfFile> pdfchche = new ConcurrentSkipListMap<String,PdfFile>();
-
- /**
- * pdf文件缓存排序列表
- * <源文件,目标PDF文件>
- */
- private SortedSet<PdfFile> pdfchcheSorte = new ConcurrentSkipListSet<PdfFile>();
-
- /**
- * 生成PDF文件且使用缓存管理
- * @param inputFile
- * @return
- */
- @SuppressWarnings("all")
- public synchronized PdfFile toPdf(String inputFile){
- PdfFile pdfFile = null;
- String _root = PDEConstant.PDFATTACH_TEMP_DIR_PATH;
- //检查PDF缓存文件夹是否存在
- File folder = new File(_root);
- if(!folder.isDirectory()){
- if(!folder.mkdirs()){
- return null;
- }
- }
- File _inputFile = new File(inputFile);
- if(!_inputFile.isFile()){
- return null;
- }
- if(pdfchche.containsKey(inputFile)){
- pdfFile = pdfchche.get(inputFile);
- pdfchcheSorte.remove(pdfFile);
- } else {
- AttachTempDirClearUtil.execute(folder);//清理临时目录
- int deletenum = 0;
- //如果pdf文件缓存数大于PDFATTACH_MAXNUM时
- long pdfAttachMaxNum = 0L;
- try {
- pdfAttachMaxNum = Long.parseLong(DmsTools.getValueFromConfigFileByKey("pdf_attach_max_num"));
- }catch(Exception e) {
- e.printStackTrace();
- pdfAttachMaxNum = 1000L;
- }
- if(pdfchche.size() > pdfAttachMaxNum){
- deletenum = 100;
- }
- //删除空闲的PDf文件
- for(int i=0;i<deletenum && i<pdfchche.size();i++){
- PdfFile _tempPdfFile = pdfchcheSorte.first();
- if(null!=_tempPdfFile && null!=_tempPdfFile.getPdfFile()){
- File _delFile = new File(_tempPdfFile.getPdfFile());
- if(_delFile.isFile()){
- if(!_delFile.delete()){
- continue;
- }
- }
- }
- pdfchche.remove(_tempPdfFile.getInputFile());
- pdfchcheSorte.remove(_tempPdfFile);
- }
- pdfFile = new PdfFile(inputFile,_root + "/"+_inputFile.getName() + ".pdf");
- }
- if(pdfFile!=null){
- if(!new File(pdfFile.getPdfFile()).isFile()){
- if(CONVERT_TYPE == 1) {//调用OpenOffice服务转换文件
- if(!this.getPdfUtil().convertPDF(inputFile,pdfFile.getPdfFile())){
- return null;
- }
- }else { //调用Office服务转换文件
- ClientConverterUtil.convert(inputFile, pdfFile.getPdfFile());
- if(!new File(pdfFile.getPdfFile()).exists()) {
- return null;
- }
-
- }
-
- }
- pdfFile.setDate(new Date());
- pdfFile.setUserNum(pdfFile.getUserNum()+1);
- pdfchche.put(inputFile,pdfFile);
- pdfchcheSorte.add(pdfFile);
- }
- return pdfFile;
- }
-
- public SortedMap<String, PdfFile> getPdfchche() {
- return pdfchche;
- }
- public void setPdfchche(SortedMap<String, PdfFile> pdfchche) {
- this.pdfchche = pdfchche;
- }
- public SortedSet<PdfFile> getPdfchcheSorte() {
- return pdfchcheSorte;
- }
- public void setPdfchcheSorte(SortedSet<PdfFile> pdfchcheSorte) {
- this.pdfchcheSorte = pdfchcheSorte;
- }
- public int offieToPdf(String sourceFile, String destFile){
- try {
- File inputFile = new File(sourceFile);
- if (!inputFile.exists()) {
- return -1;// 找不到源文件, 则返回-1
- }
- // 假如目标路径不存在, 则新建该路径
- File outputFile = new File(destFile);
- if (!outputFile.getParentFile().exists()) {
- outputFile.getParentFile().mkdirs();
- }
- Process pro = startService();
- // connect to an OpenOffice.org instance running on port 8100
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(
- host,Integer.parseInt(port));
- connection.connect();
- // convert
- DocumentConverter converter = new OpenOfficeDocumentConverter(
- connection);
- converter.convert(inputFile, outputFile);
- // close the connection
- connection.disconnect();
- // 封闭OpenOffice服务的进程
- pro.destroy();
- return 0;
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return -1;
- } catch (ConnectException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return 1;
- }
- private Process startService() throws IOException {
- // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
- if (openOffice_HOME.charAt(openOffice_HOME.length() - 1) != '\\') {
- openOffice_HOME += "\\";
- }
- // 启动OpenOffice的服务
- String command = openOffice_HOME
- + "program\\soffice.exe -headless -accept=\"socket,host=" + host + ",port=" + port + ";urp;\"";
- Process pro = Runtime.getRuntime().exec(command);
- return pro;
- }
- public String getOpenOffice_HOME() {
- return openOffice_HOME;
- }
- public void setOpenOffice_HOME(String openOffice_HOME) {
- this.openOffice_HOME = openOffice_HOME;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public String getPort() {
- return port;
- }
- public void setPort(String port) {
- this.port = port;
- }
-
-
- /**
- * 转换文件至PDF且加水印
- * @param inputFile 输入文件地址
- * @param outputFile 输出文件地址
- * @param waterMarkPath 水印图片地址
- * @param rotation 旋转角度 -90---90度
- * @param percent 大小百分比 100为原始大小
- * @param fillOpacity 透明度 0f - 1f
- * @return
- */
- public boolean ConvertToPdfAddWaterMark(String inputFile, String outputFile,
- String waterMarkPath, float rotation, int percent,
- float fillOpacity) {
- boolean result = false;
- PdfFile pdfFile = null;
- if(PdfUtil.getExtension(inputFile, "").equalsIgnoreCase("PDF")){
- pdfFile = new PdfFile(inputFile,inputFile);
- } else {
- pdfFile = this.toPdf(inputFile);
- }
- if(pdfFile!=null){
- List<Watermark> watermarks = new ArrayList<Watermark>();
- Set<Integer> pages = new HashSet<Integer>();
- pages.add(1);
- watermarks.add(new WatermarkImage(10,0,0,rotation,0,null,waterMarkPath,percent));
- result = this.getPdfUtil().addWatermark(pdfFile.getPdfFile(), outputFile, fillOpacity, watermarks);
- }
- return result;
- }
-
- /**
- * 得到文件扩展名
- * @param filename 文件名
- * @param defExt 默认扩展名
- * @return
- */
- public static String getExtension(String filename, String defExt) {
- if ((filename != null) && (filename.length() > 0)) {
- int i = filename.lastIndexOf('.');
- if ((i > -1) && (i < (filename.length() - 1))) {
- return filename.substring(i + 1);
- }
- }
- return defExt;
- }
-
- public static void main(String[]strs){
- OfficeToPdfUtil o = new OfficeToPdfUtil();
- o.ConvertToPdfAddWaterMark("f:/2.txt","f:/2.txt.all.pdf", "f:/P1000983.JPG", 0, 80, 0.5f);
- System.out.println(((PdfFile)o.getPdfchcheSorte().first()).getUserNum());
- }
- }
复制代码 |