org.artofsolving.jodconverter.OfficeDocumentConverter Java Examples

The following examples show how to use org.artofsolving.jodconverter.OfficeDocumentConverter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseTests.java    From kbase-doc with Apache License 2.0 8 votes vote down vote up
protected void convert(File inputFile, File outputFile){
		DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
		configuration.setPortNumber(8100);
		configuration.setOfficeHome(new File(libreOfficeDirPath));
//		configuration.setOfficeHome(new File("D:/Program Files/OpenOffice"));

		OfficeManager officeManager = configuration.buildOfficeManager();
        officeManager.start();
        DocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry();
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, formatRegistry);
        
        try {
        	 converter.convert(inputFile, outputFile);
        } catch (Exception e){
        	e.printStackTrace();
		} finally {
            officeManager.stop();
        }
	}
 
Example #2
Source File: ViewOfficeTools.java    From csustRepo with MIT License 6 votes vote down vote up
public  boolean office2Pdf(String spath,String dpath){
	// 转换源文件
	File sourceFile = new File(spath);
	// PDF目标文件
	File pdfFile = new File(dpath);

	// swf文件名不能有中文或者特殊字符,否则会出现找不到文件路径的问题,并且文件名不能存在特殊字符(如%),否则转换会失败
	//	System.out.println("第一步:生成文件对象,准备转换");
	// 转换成pdf文件
	if (sourceFile.exists()) {
        OfficeDocumentConverter converter = 
        		new OfficeDocumentConverter(getOfficeManager());
        converter.convert(sourceFile,pdfFile);
	} else {
		//System.out.println("要转换的文件不存在");
		return false;
	}
	return true;		
}
 
Example #3
Source File: OfficeConService.java    From wenku with MIT License 6 votes vote down vote up
/**
 * 
 * 创建一个新的实例 OfficeConService.
 * @param officePort
 * @param officeHome
 */
public OfficeConService(String officePort, String officeHome) {
    this.officePort = officePort;
    this.officeHome = officeHome;

    DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();

    //设置转换端口,默认为8100
    if (StringUtils.isNotBlank(this.officePort)) {
        configuration.setPortNumber(Integer.parseInt(this.officePort));
        logger.info("office转换服务监听端口设置为:" + this.officePort);
    }

    //设置office安装目录
    if (StringUtils.isNotBlank(this.officeHome)) {
        configuration.setOfficeHome(new File(this.officeHome));
        logger.info("设置office安装目录为:" + this.officeHome);
    }

    officeManager = configuration.buildOfficeManager();
    documentConverter = new OfficeDocumentConverter(officeManager);
}
 
Example #4
Source File: OfficeConService.java    From wenku with MIT License 6 votes vote down vote up
/**
 * 
 * 创建一个新的实例 OfficeConService.
 */
public OfficeConService() {

    DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();

    //设置转换端口,默认为2002
    if (StringUtils.isNotBlank(this.officePort)) {
        configuration.setPortNumber(Integer.parseInt(this.officePort));
        logger.info("office转换服务监听端口设置为:" + this.officePort);
    }

    //设置office安装目录
    if (StringUtils.isNotBlank(officeHome)) {
        configuration.setOfficeHome(new File(this.officeHome));
        logger.info("设置office安装目录为:" + this.officeHome);
    }

    officeManager = configuration.buildOfficeManager();
    documentConverter = new OfficeDocumentConverter(this.officeManager);
}
 
Example #5
Source File: DocConverter.java    From document-management-system with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert a document format to another one.
 */
public void convert(File inputFile, String mimeType, File outputFile) throws ConversionException {
	log.debug("convert({}, {}, {})", new Object[]{inputFile, mimeType, outputFile});

	if (Config.SYSTEM_OPENOFFICE_PATH.equals("") && Config.SYSTEM_OPENOFFICE_SERVER.equals("")) {
		throw new ConversionException(Config.PROPERTY_SYSTEM_OPENOFFICE_PATH + " or " + Config.PROPERTY_SYSTEM_OPENOFFICE_SERVER
				+ " not configured");
	}

	if (!validOpenOffice.contains(mimeType)) {
		throw new ConversionException("Invalid document conversion MIME type: " + mimeType);
	}

	try {
		if (!Config.SYSTEM_OPENOFFICE_PATH.equals("")) {
			// Document conversion managed by local OO instance
			OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
			converter.convert(inputFile, outputFile);
		} else if (!Config.SYSTEM_OPENOFFICE_SERVER.equals("")) {
			// Document conversion managed by remote conversion server
			remoteConvert(Config.SYSTEM_OPENOFFICE_SERVER, inputFile, mimeType, outputFile, MimeTypeConfig.MIME_PDF);
		}
	} catch (OfficeException e) {
		throw new ConversionException("Error converting document: " + e.getMessage());
	}
}
 
Example #6
Source File: BaseTests.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
protected void convert(File inputFile, File outputFile, String password){
		DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
		configuration.setPortNumber(8100);
		configuration.setOfficeHome(new File(libreOfficeDirPath));
//		configuration.setOfficeHome(new File("D:/Program Files/OpenOffice"));

		OfficeManager officeManager = configuration.buildOfficeManager();
        officeManager.start();
        DocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry();
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, formatRegistry);
        Map<String,?> defaultLoadProperties = createDefaultLoadProperties(password);
        converter.setDefaultLoadProperties(defaultLoadProperties);
        try {
        	 converter.convert(inputFile, outputFile);
        } catch (Exception e){
        	e.printStackTrace();
		} finally {
            officeManager.stop();
        }
	}
 
Example #7
Source File: ConvertTests.java    From kbase-doc with Apache License 2.0 6 votes vote down vote up
@Test
	public void testConvert() throws IOException {
//		File inputFile = new File("D:/Workspace/kbase-doc/target/classes/static/DATAS/1512561737109/1.doc");
		File inputFile = new File("D:/Workspace/kbase-doc/target/classes/static/DATAS/1512561737109/1512561737109.html");
		File outputFile = new File("D:/Workspace/kbase-doc/target/classes/static/DATAS/1512561737109/" + Calendar.getInstance().getTimeInMillis() + ".docx");
//		if (!outputFile.exists()){
//			outputFile.createNewFile();
//		}
		DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
		configuration.setPortNumber(8100);
		configuration.setOfficeHome(new File("D:/Program Files/LibreOffice"));

		OfficeManager officeManager = configuration.buildOfficeManager();
        officeManager.start();
        DocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry();
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, formatRegistry);
        
        try {
        	 converter.convert(inputFile, outputFile);
        } catch (Exception e){
        	e.printStackTrace();
		} finally {
            officeManager.stop();
        }
	}
 
Example #8
Source File: OfficePDFConverter.java    From sun-wordtable-read with Apache License 2.0 6 votes vote down vote up
public static void doc2Docx(String inputFile,String outputFile) {
	File pdfFile = new File(outputFile);  
    if (pdfFile.exists()) {  
        pdfFile.delete();  
    }  
    try{  
        long startTime = System.currentTimeMillis();  
        //打开服务  
        startService();          
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
        DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
        docx.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2007 XML"));
        //开始转换  
        converter.convert(new File(inputFile),new File(outputFile), docx);  
        //关闭  
        stopService();  
        System.out.println("运行结束");  
    }catch (Exception e) {  
        // TODO: handle exception  
        e.printStackTrace();  
    }  
}
 
Example #9
Source File: Doc2DocxUtil.java    From sun-wordtable-read with Apache License 2.0 6 votes vote down vote up
public static void doc2Docx(String inputFile,String outputFile) {
	File pdfFile = new File(outputFile);  
    if (pdfFile.exists()) {  
        pdfFile.delete();  
    }  
    try{  
        //打开服务  
        startService();          
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
        DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
        docx.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2007 XML"));
        //开始转换  
        converter.convert(new File(inputFile),new File(outputFile), docx);  
        //关闭  
        stopService();  
        System.out.println("运行结束");  
    }catch (Exception e) {  
        // TODO: handle exception  
        e.printStackTrace();  
    }  
}
 
Example #10
Source File: OfficeToPdf.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public void office2pdf(String inputFilePath, String outputFilePath) {
    OfficeDocumentConverter converter = converterUtils.getDocumentConverter();
    if (null != inputFilePath) {
        File inputFile = new File(inputFilePath);
        // 判断目标文件路径是否为空
        if (null == outputFilePath) {
            // 转换后的文件路径
            String outputFilePath_end = getOutputFilePath(inputFilePath);
            if (inputFile.exists()) {
                // 找不到源文件, 则返回
                converterFile(inputFile, outputFilePath_end,converter);
            }
        } else {
            if (inputFile.exists()) {
                // 找不到源文件, 则返回
                converterFile(inputFile, outputFilePath, converter);
            }
        }
    }
}
 
Example #11
Source File: Doc2DocxUtil.java    From sun-wordtable-read with Apache License 2.0 5 votes vote down vote up
private static void transformBinaryWordDocToW2003Xml(File in, File out) {
    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);;
    DocumentFormat w2003xml = new DocumentFormat("Microsoft Word 2003 XML", "xml", "text/xml");
    w2003xml.setInputFamily(DocumentFamily.TEXT);
    w2003xml.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2003 XML"));
    converter.convert(in, out, w2003xml);
}
 
Example #12
Source File: OpenOfficePDFConverter.java    From jeecg with Apache License 2.0 5 votes vote down vote up
public void convert2PDF(String inputFile, String pdfFile, String extend) {
	startService();
	 log.info("进行文档转换转换:" + inputFile + " --> " + pdfFile);
	OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
	try {
		converter.convert(new File(inputFile), new File(pdfFile));
	} catch (Exception e) {
		e.printStackTrace();
		log.info(e.getMessage());
	}
	
	stopService();
    log.info("进行文档转换转换---- 结束----");
}
 
Example #13
Source File: OfficeToPdf.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
public static void converterFile(File inputFile, String outputFilePath_end,
                                 OfficeDocumentConverter converter) {
    File outputFile = new File(outputFilePath_end);
    // 假如目标路径不存在,则新建该路径
    if (!outputFile.getParentFile().exists()) {
        outputFile.getParentFile().mkdirs();
    }
    converter.convert(inputFile, outputFile);
}
 
Example #14
Source File: JodContentTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void convert(File tempFromFile, DocumentFormat sourceFormat, File tempToFile,
        DocumentFormat targetFormat)
{
    OfficeDocumentConverter converter = new OfficeDocumentConverter(jodconverter.getOfficeManager());
    converter.convert(tempFromFile, tempToFile);
}
 
Example #15
Source File: CommonDocumentConverter.java    From sun-wordtable-read with Apache License 2.0 5 votes vote down vote up
@Override
public void convert() {
	OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
	
	System.out.println("转换前处理...");  
	before();
	
       //开始转换 
	System.out.println("转换开始执行,["+inputFile+"]转换为["+outputFile+"]...");  
	if(StringUtils.isNotBlank(extraOutputFormatToNeed)) {
		DocumentFormat extraFormat = converter.getFormatRegistry().getFormatByExtension(extraOutputFormatToNeed);
		extraFormat.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", extraOutputFormatMap.get(extraOutputFormatToNeed)));
	
           if(needTempFile) {
			converter.convert(new File(tempFile),new File(outputFile), extraFormat);  
		} else {
			converter.convert(new File(inputFile),new File(outputFile), extraFormat);  
		}
           
	} else {
		if(needTempFile) {
			converter.convert(new File(tempFile),new File(outputFile));  
		} else {
			converter.convert(new File(inputFile),new File(outputFile));  
		}
	}
	
	System.out.println("转换后处理...");  
	after();
	
	System.out.println("转换完成");  
}
 
Example #16
Source File: OfficeToPdf.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
/**
 * 转换文件
 *
 * @param inputFile
 * @param outputFilePath_end
 * @param inputFilePath
 * @param outputFilePath
 * @param converter
 */
public static void converterFile(File inputFile, String outputFilePath_end,
                                 String inputFilePath, String outputFilePath,
                                 OfficeDocumentConverter converter) {
    File outputFile = new File(outputFilePath_end);
    // 假如目标路径不存在,则新建该路径
    if (!outputFile.getParentFile().exists()) {
        outputFile.getParentFile().mkdirs();
    }
    converter.convert(inputFile, outputFile);
}
 
Example #17
Source File: Doc2DocxUtil.java    From sun-wordtable-read with Apache License 2.0 5 votes vote down vote up
private static void transformBinaryWordDocToDocX(File in, File out) {
    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
    DocumentFormat docx = converter.getFormatRegistry().getFormatByExtension("docx");
    docx.setStoreProperties(DocumentFamily.TEXT, Collections.singletonMap("FilterName", "MS Word 2007 XML"));

    converter.convert(in, out, docx);
}
 
Example #18
Source File: WebappContext.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
public WebappContext(ServletContext servletContext, OpenOffice openOffice) {
	DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
	fileUpload = new ServletFileUpload(fileItemFactory);
	if (openOffice.getFileSizeMax() != null) {
		fileUpload.setFileSizeMax(Integer.parseInt(openOffice.getFileSizeMax()));
		logger.info("max file upload size set to " + openOffice.getFileSizeMax());
	} else {
		logger.warn("max file upload size not set");
	}
	
	DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
	if (openOffice.getPort() != null) {
		String ports = openOffice.getPort();
		String[] portArr = ports.split(",");
		int[] portArrInt = new int[portArr.length];
		int i = 0;
		for (String port : portArr){
			portArrInt[i++] = Integer.parseInt(port);
		}
	    configuration.setPortNumbers(portArrInt);
	}
	if (openOffice.getHome() != null) {
	    configuration.setOfficeHome(new File(openOffice.getHome()));
	}
	if (StringUtils.isNotBlank(openOffice.getProfile())) {
	    configuration.setTemplateProfileDir(new File(openOffice.getProfile()));
	}

	officeManager = configuration.buildOfficeManager();
	documentConverter = new OfficeDocumentConverter(officeManager);
}
 
Example #19
Source File: BaseController.java    From kbase-doc with Apache License 2.0 5 votes vote down vote up
/**
 * 转换文件
 * @author eko.zhan at 2018年9月2日 下午2:16:14
 * @param originFile
 * @param targetFile
 */
protected void convert(File originFile, File targetFile) {
	WebappContext webappContext = WebappContext.get(servletContext);
	OfficeDocumentConverter converter = webappContext.getDocumentConverter();
	try {
       	long startTime = System.currentTimeMillis();
       	converter.convert(originFile, targetFile);
       	long conversionTime = System.currentTimeMillis() - startTime;
       	log.info(String.format("successful conversion: %s [%db] to %s in %dms", FilenameUtils.getExtension(originFile.getName()), originFile.length(), FilenameUtils.getExtension(targetFile.getName()), conversionTime));
       } catch (Exception e) {
       	e.printStackTrace();
           log.error(String.format("failed conversion: %s [%db] to %s; %s; input file: %s", FilenameUtils.getExtension(originFile.getName()), originFile.length(), FilenameUtils.getExtension(targetFile.getName()), e, targetFile.getName()));
       }
}
 
Example #20
Source File: OfficeToPdf.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
/**
     * 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
     *
     * @param inputFilePath
     *            源文件路径,如:"e:/test.docx"
     * @param outputFilePath
     *            目标文件路径,如:"e:/test_docx.pdf"
     * @return
     */
    public  boolean office2pdf(String inputFilePath, String outputFilePath) {
        boolean flag = false;
        OfficeDocumentConverter converter = converterUtils.getDocumentConverter();
        if (null != inputFilePath) {
            File inputFile = new File(inputFilePath);
            // 判断目标文件路径是否为空
            if (null == outputFilePath) {
                // 转换后的文件路径
                String outputFilePath_end = getOutputFilePath(inputFilePath);
                if (inputFile.exists()) {// 找不到源文件, 则返回
                    converterFile(inputFile, outputFilePath_end, inputFilePath,
                            outputFilePath, converter);
                    flag = true;
                }
            } else {
                if (inputFile.exists()) {// 找不到源文件, 则返回
                    converterFile(inputFile, outputFilePath, inputFilePath,
                            outputFilePath, converter);
                    flag = true;
                }
            }
//            officeManager.stop();
        } else {
            flag = false;
        }
        return flag;
    }
 
Example #21
Source File: ConverterUtils.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
public OfficeDocumentConverter getDocumentConverter() {
    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, new ControlDocumentFormatRegistry());
    converter.setDefaultLoadProperties(getLoadProperties());
    return converter;
}
 
Example #22
Source File: WebappContext.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
public OfficeDocumentConverter getDocumentConverter() {
    return documentConverter;
}
 
Example #23
Source File: OfficeConService.java    From wenku with MIT License 4 votes vote down vote up
public OfficeDocumentConverter getDocumentConverter() {
    return documentConverter;
}
 
Example #24
Source File: ConverterUtils.java    From kkFileViewOfficeEdit with Apache License 2.0 4 votes vote down vote up
public OfficeDocumentConverter getDocumentConverter() {
	OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager,
			new ControlDocumentFormatRegistry());
	converter.setDefaultLoadProperties(getLoadProperties());
	return converter;
}