Reputation: 67
I am using JodConverter to convert my .docx (Microsoft Office) files to pdf but somehow it does not covert and gives me the error.When If I tried to convert .doc documents to pdf then it works fine.I am using maven JodConverter plugin version 2.2.0 .
My question is that Is it possible to convert .docx files to pdf using JODCoverter ?
Upvotes: 2
Views: 5968
Reputation: 11
if you are still using the old JodConverter 2.2.1 and don't want to upgrade it, you can just add the docx document format to the registry. The code will look like this:
DefaultDocumentFormatRegistry documentFormatRegistry= new DefaultDocumentFormatRegistry();
DocumentFormat docx = new DocumentFormat("Microsoft Word 2007 XML", "application/vnd.openxmlformats-officedocument.wordprocessingml.document","docx");
docx.setExportFilter(DocumentFamily.TEXT,"MS Word 2007 XML");
documentFormatRegistry.addDocumentFormat(docx);
OpenOfficeDocumentConverter converter = new OpenOfficeDocumentConverter(connection,documentFormatRegistry);
Upvotes: 0
Reputation: 465
Disclaimer: I own the jodconverter.org domain and forked the original project on GitHub.
Little late to the party but you can use a fork of the original project, available on Maven:
It supports the docx and xlsx formats.
Upvotes: 4
Reputation: 11
JodConverter version 2.2.1 couldn't guess DocumentFormat for docx. Though you could pass document format for input file as an argument.
final DocumentFormat docx = new DocumentFormat("Microsoft Word 2007 XML", DocumentFamily.TEXT, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
converter.convert(inputFile, docx, outputFile, null);
Upvotes: 1
Reputation: 4254
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;
}
}
Upvotes: 0