首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JodConverter不支持java中的docx文件。

JodConverter不支持java中的docx文件。
EN

Stack Overflow用户
提问于 2016-06-19 15:39:48
回答 4查看 4.9K关注 0票数 2

我使用JodConverter将我的.docx (Microsoft )文件转换成pdf格式,但不知怎么它并不隐蔽,如果我试图将.doc文档转换为pdf,它就会给我error.When,那么它就能正常工作。我使用的是maven JodConverter插件2.2.0版本。

我的问题是,可以使用.docx将JODCoverter文件转换为pdf吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-07-13 06:29:38

免责声明:我拥有jodconverter.org域名,并在GitHub上分叉了原来的项目。

聚会稍微晚了一点,但是您可以使用原始项目的分叉,可以在Maven上找到:

JODConverter

它支持docx和xlsx格式。

票数 5
EN

Stack Overflow用户

发布于 2016-10-15 17:33:44

JodConverter版本2.2.1不能猜到DocumentFormat用于docx。尽管您可以将输入文件的文档格式作为参数传递。

代码语言:javascript
复制
final DocumentFormat docx = new DocumentFormat("Microsoft Word 2007 XML", DocumentFamily.TEXT, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
converter.convert(inputFile, docx, outputFile, null);
票数 1
EN

Stack Overflow用户

发布于 2016-08-25 14:16:04

代码语言:javascript
复制
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.DocumentFormatRegistry;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XRefreshable;

public abstract class AbstractOpenOfficeDocumentConverter implements DocumentConverter {

    private final Map/*<String,Object>*/ defaultLoadProperties;

    protected OpenOfficeConnection openOfficeConnection;
    private DocumentFormatRegistry documentFormatRegistry;

    public AbstractOpenOfficeDocumentConverter(OpenOfficeConnection connection) {
        this(connection, new DefaultDocumentFormatRegistry());
    }

    public AbstractOpenOfficeDocumentConverter(OpenOfficeConnection openOfficeConnection, DocumentFormatRegistry documentFormatRegistry) {
        this.openOfficeConnection = openOfficeConnection;
        this.documentFormatRegistry = documentFormatRegistry;

        defaultLoadProperties = new HashMap();
        defaultLoadProperties.put("Hidden", Boolean.TRUE);
        defaultLoadProperties.put("ReadOnly", Boolean.TRUE);
    }

    public void setDefaultLoadProperty(String name, Object value) {
        defaultLoadProperties.put(name, value);
    }

    protected Map getDefaultLoadProperties() {
        return defaultLoadProperties;
    }

    protected DocumentFormatRegistry getDocumentFormatRegistry() {
        return documentFormatRegistry;
    }

    public void convert(File inputFile, File outputFile) {
        convert(inputFile, outputFile, null);
    }

    public void convert(File inputFile, File outputFile, DocumentFormat outputFormat) {
        convert(inputFile, null, outputFile, outputFormat);
    }

    public void convert(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat) {
        ensureNotNull("inputStream", inputStream);
        ensureNotNull("inputFormat", inputFormat);
        ensureNotNull("outputStream", outputStream);
        ensureNotNull("outputFormat", outputFormat);
        convertInternal(inputStream, inputFormat, outputStream, outputFormat);
    }

    public void convert(File inputFile, DocumentFormat inputFormat, File outputFile, DocumentFormat outputFormat) {
        ensureNotNull("inputFile", inputFile);
        ensureNotNull("outputFile", outputFile);

        if (!inputFile.exists()) {
            throw new IllegalArgumentException("inputFile doesn't exist: " + inputFile);
        }
        if (inputFormat == null) {
            inputFormat = guessDocumentFormat(inputFile);
        }
        if (outputFormat == null) {
            outputFormat = guessDocumentFormat(outputFile);
        }
        if (!inputFormat.isImportable()) {
            throw new IllegalArgumentException("unsupported input format: " + inputFormat.getName());
        }
        if (!inputFormat.isExportableTo(outputFormat)) {
            throw new IllegalArgumentException("unsupported conversion: from " + inputFormat.getName() + " to " + outputFormat.getName());
        }
        convertInternal(inputFile, inputFormat, outputFile, outputFormat);
    }

    protected abstract void convertInternal(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat);

    protected abstract void convertInternal(File inputFile, DocumentFormat inputFormat, File outputFile, DocumentFormat outputFormat);

    private void ensureNotNull(String argumentName, Object argumentValue) {
        if (argumentValue == null) {
            throw new IllegalArgumentException(argumentName + " is null");
        }
    }

    private DocumentFormat guessDocumentFormat(File file) {
        String extension = FilenameUtils.getExtension(file.getName());
        DocumentFormat format = getDocumentFormatRegistry().getFormatByFileExtension(extension);
        if (format == null) {
            throw new IllegalArgumentException("unknown document format for file: " + file);
        }
        return format;
    }

    protected void refreshDocument(XComponent document) {
        XRefreshable refreshable = (XRefreshable) UnoRuntime.queryInterface(XRefreshable.class, document);
        if (refreshable != null) {
            refreshable.refresh();
        }
    }

    protected static PropertyValue property(String name, Object value) {
        PropertyValue property = new PropertyValue();
        property.Name = name;
        property.Value = value;
        return property;
    }

    protected static PropertyValue[] toPropertyValues(Map/*<String,Object>*/ properties) {
        PropertyValue[] propertyValues = new PropertyValue[properties.size()];
        int i = 0;
        for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object value = entry.getValue();
            if (value instanceof Map) {
                // recursively convert nested Map to PropertyValue[]
                Map subProperties = (Map) value;
                value = toPropertyValues(subProperties);
            }
            propertyValues[i++] = property((String) entry.getKey(), value);
        }
        return propertyValues;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37908940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档