org.artofsolving.jodconverter.office.OfficeException Java Examples

The following examples show how to use org.artofsolving.jodconverter.office.OfficeException. 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: AbstractConversionTask.java    From wenku with MIT License 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #2
Source File: OfficeDocumentUtils.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
public static DocumentFamily getDocumentFamily(XComponent document) throws OfficeException {
    XServiceInfo serviceInfo = cast(XServiceInfo.class, document);
    if (serviceInfo.supportsService("com.sun.star.text.GenericTextDocument")) {
        // NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
        // but this further distinction doesn't seem to matter for conversions
        return DocumentFamily.TEXT;
    } else if (serviceInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")) {
        return DocumentFamily.SPREADSHEET;
    } else if (serviceInfo.supportsService("com.sun.star.presentation.PresentationDocument")) {
        return DocumentFamily.PRESENTATION;
    } else if (serviceInfo.supportsService("com.sun.star.drawing.DrawingDocument")) {
        return DocumentFamily.DRAWING;
    } else {
        throw new OfficeException("document of unknown family: " + serviceInfo.getImplementationName());
    }
}
 
Example #3
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #4
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #5
Source File: AbstractConversionTask.java    From wenku with MIT License 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #6
Source File: OfficeDocumentUtils.java    From wenku with MIT License 6 votes vote down vote up
public static DocumentFamily getDocumentFamily(XComponent document) throws OfficeException {
    XServiceInfo serviceInfo = cast(XServiceInfo.class, document);
    if (serviceInfo.supportsService("com.sun.star.text.GenericTextDocument")) {
        // NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
        // but this further distinction doesn't seem to matter for conversions
        return DocumentFamily.TEXT;
    } else if (serviceInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")) {
        return DocumentFamily.SPREADSHEET;
    } else if (serviceInfo.supportsService("com.sun.star.presentation.PresentationDocument")) {
        return DocumentFamily.PRESENTATION;
    } else if (serviceInfo.supportsService("com.sun.star.drawing.DrawingDocument")) {
        return DocumentFamily.DRAWING;
    } else {
        throw new OfficeException("document of unknown family: " + serviceInfo.getImplementationName());
    }
}
 
Example #7
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 #8
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #9
Source File: OfficeDocumentUtils.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public static DocumentFamily getDocumentFamily(XComponent document) throws OfficeException {
    XServiceInfo serviceInfo = cast(XServiceInfo.class, document);
    if (serviceInfo.supportsService("com.sun.star.text.GenericTextDocument")) {
        // NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
        // but this further distinction doesn't seem to matter for conversions
        return DocumentFamily.TEXT;
    } else if (serviceInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")) {
        return DocumentFamily.SPREADSHEET;
    } else if (serviceInfo.supportsService("com.sun.star.presentation.PresentationDocument")) {
        return DocumentFamily.PRESENTATION;
    } else if (serviceInfo.supportsService("com.sun.star.drawing.DrawingDocument")) {
        return DocumentFamily.DRAWING;
    } else {
        throw new OfficeException("document of unknown family: " + serviceInfo.getImplementationName());
    }
}
 
Example #10
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #11
Source File: AbstractConversionTask.java    From wenku with MIT License 5 votes vote down vote up
private void storeDocument(XComponent document, File outputFile) throws OfficeException {
    Map<String,?> storeProperties = getStoreProperties(outputFile, document);
    if (storeProperties == null) {
        throw new OfficeException("unsupported conversion");
    }
    try {
        cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties));
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not store document: " + outputFile.getName(), ioException);
    }
}
 
Example #12
Source File: StandardConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
@Override
protected void modifyDocument(XComponent document) throws OfficeException {
    XRefreshable refreshable = cast(XRefreshable.class, document);
    if (refreshable != null) {
        refreshable.refresh();
    }
}
 
Example #13
Source File: OfficeDocumentConverter.java    From wenku with MIT License 5 votes vote down vote up
public void convert(File inputFile, File outputFile, DocumentFormat outputFormat) throws OfficeException {
    String inputExtension = FilenameUtils.getExtension(inputFile.getName());
    DocumentFormat inputFormat = formatRegistry.getFormatByExtension(inputExtension);
    StandardConversionTask conversionTask = new StandardConversionTask(inputFile, outputFile, outputFormat);
    conversionTask.setDefaultLoadProperties(defaultLoadProperties);
    conversionTask.setInputFormat(inputFormat);
    officeManager.execute(conversionTask);
}
 
Example #14
Source File: StandardConversionTask.java    From wenku with MIT License 5 votes vote down vote up
@Override
protected void modifyDocument(XComponent document) throws OfficeException {
    XRefreshable refreshable = cast(XRefreshable.class, document);
    if (refreshable != null) {
        refreshable.refresh();
    }
}
 
Example #15
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
private void storeDocument(XComponent document, File outputFile) throws OfficeException {
    Map<String,?> storeProperties = getStoreProperties(outputFile, document);
    if (storeProperties == null) {
        throw new OfficeException("unsupported conversion");
    }
    try {
        cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties));
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not store document: " + outputFile.getName(), ioException);
    }
}
 
Example #16
Source File: OfficeDocumentConverter.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
public void convert(File inputFile, File outputFile, DocumentFormat outputFormat) throws OfficeException {
    String inputExtension = FilenameUtils.getExtension(inputFile.getName());
    DocumentFormat inputFormat = formatRegistry.getFormatByExtension(inputExtension);
    StandardConversionTask conversionTask = new StandardConversionTask(inputFile, outputFile, outputFormat);
    conversionTask.setDefaultLoadProperties(defaultLoadProperties);
    conversionTask.setInputFormat(inputFormat);
    officeManager.execute(conversionTask);
}
 
Example #17
Source File: StandardConversionTask.java    From kkFileView with Apache License 2.0 5 votes vote down vote up
@Override
protected void modifyDocument(XComponent document) throws OfficeException {
    XRefreshable refreshable = cast(XRefreshable.class, document);
    if (refreshable != null) {
        refreshable.refresh();
    }
}
 
Example #18
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
private void storeDocument(XComponent document, File outputFile) throws OfficeException {
    Map<String,?> storeProperties = getStoreProperties(outputFile, document);
    if (storeProperties == null) {
        throw new OfficeException("unsupported conversion");
    }
    try {
        cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties));
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not store document: " + outputFile.getName(), ioException);
    }
}
 
Example #19
Source File: OfficeDocumentConverter.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
public void convert(File inputFile, File outputFile, DocumentFormat outputFormat) throws OfficeException {
    String inputExtension = FilenameUtils.getExtension(inputFile.getName());
    DocumentFormat inputFormat = formatRegistry.getFormatByExtension(inputExtension);
    StandardConversionTask conversionTask = new StandardConversionTask(inputFile, outputFile, outputFormat);
    conversionTask.setDefaultLoadProperties(defaultLoadProperties);
    conversionTask.setInputFormat(inputFormat);
    officeManager.execute(conversionTask);
}
 
Example #20
Source File: OfficeDocumentConverter.java    From kkFileView with Apache License 2.0 4 votes vote down vote up
public void convert(File inputFile, File outputFile) throws OfficeException {
    String outputExtension = FilenameUtils.getExtension(outputFile.getName());
    DocumentFormat outputFormat = formatRegistry.getFormatByExtension(outputExtension);
    convert(inputFile, outputFile, outputFormat);
}
 
Example #21
Source File: JodConverterMetadataExtracterWorker.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void execute(OfficeContext context)
{
    if (logger.isDebugEnabled())
    {
        logger.debug("Extracting metadata from file " + inputFile);
    }

    XComponent document = null;
    try
    {
        if (!inputFile.exists())
        {
            throw new OfficeException("input document not found");
        }
        XComponentLoader loader = cast(XComponentLoader.class, context
                .getService(SERVICE_DESKTOP));
        
        // Need to set the Hidden property to ensure that OOo GUI does not appear.
        PropertyValue hiddenOOo = new PropertyValue();
        hiddenOOo.Name = "Hidden";
        hiddenOOo.Value = Boolean.TRUE;
        PropertyValue readOnly = new PropertyValue();
        readOnly.Name = "ReadOnly";
        readOnly.Value = Boolean.TRUE;

        try
        {
            document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0,
                    new PropertyValue[]{hiddenOOo, readOnly});
        } catch (IllegalArgumentException illegalArgumentException)
        {
            throw new OfficeException("could not load document: "
                    + inputFile.getName(), illegalArgumentException);
        } catch (ErrorCodeIOException errorCodeIOException)
        {
            throw new OfficeException("could not load document: "
                    + inputFile.getName() + "; errorCode: "
                    + errorCodeIOException.ErrCode, errorCodeIOException);
        } catch (IOException ioException)
        {
            throw new OfficeException("could not load document: "
                    + inputFile.getName(), ioException);
        }
        if (document == null)
        {
            throw new OfficeException("could not load document: "
                    + inputFile.getName());
        }
        XRefreshable refreshable = cast(XRefreshable.class, document);
        if (refreshable != null)
        {
            refreshable.refresh();
        }

        XDocumentInfoSupplier docInfoSupplier = cast(XDocumentInfoSupplier.class, document);
        XPropertySet propSet = cast(XPropertySet.class, docInfoSupplier.getDocumentInfo());

        // The strings below are property names as used by OOo. They need upper-case
        // initial letters.
        Object author = getPropertyValueIfAvailable(propSet, "Author");
        Object description = getPropertyValueIfAvailable(propSet, "Subject");
        Object title = getPropertyValueIfAvailable(propSet, "Title");
        
        Map<String, Serializable> results = new HashMap<String, Serializable>(3);
        results.put(KEY_AUTHOR, author == null ? null : author.toString());
        results.put(KEY_DESCRIPTION, description == null ? null : description.toString());
        results.put(KEY_TITLE, title == null ? null : title.toString());
        callback.setResults(results);
    } catch (OfficeException officeException)
    {
        throw officeException;
    } catch (Exception exception)
    {
        throw new OfficeException("conversion failed", exception);
    } finally
    {
        if (document != null)
        {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null)
            {
                try
                {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException)
                {
                    // whoever raised the veto should close the document
                }
            } else
            {
                document.dispose();
            }
        }
    }
}
 
Example #22
Source File: OfficeDocumentConverter.java    From wenku with MIT License 4 votes vote down vote up
public void convert(File inputFile, File outputFile) throws OfficeException {
    String outputExtension = FilenameUtils.getExtension(outputFile.getName());
    DocumentFormat outputFormat = formatRegistry.getFormatByExtension(outputExtension);
    convert(inputFile, outputFile, outputFormat);
}
 
Example #23
Source File: OOoContentTransformerHelper.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void transformLocal(ContentReader reader, ContentWriter writer, TransformationOptions options,
                              String sourceMimetype, String sourceExtension, String targetExtension,
                              DocumentFormat sourceFormat, DocumentFormat targetFormat)
{
    // create temporary files to convert from and to
    File tempFromFile = TempFileProvider.createTempFile(
            getTempFilePrefix()+"-source-",
            "." + sourceExtension);
    File tempToFile = TempFileProvider.createTempFile(
            getTempFilePrefix()+"-target-",
            "." + targetExtension);

    // download the content from the source reader
    saveContentInFile(sourceMimetype, reader, tempFromFile);

    try
    {
        convert(tempFromFile, sourceFormat, tempToFile, targetFormat);
        writer.putContent(tempToFile);
    }
    catch (OfficeException e)
    {
        throw new ContentIOException("OpenOffice server conversion failed: \n" +
                "   reader: " + reader + "\n" +
                "   writer: " + writer + "\n" +
                "   from file: " + tempFromFile + "\n" +
                "   to file: " + tempToFile,
                e);
    }
    catch (Throwable throwable)
    {
        // Because of the known bug with empty Spreadsheets in JodConverter try to catch exception and produce empty pdf file
        if (throwable.getCause() instanceof ErrorCodeIOException &&
                ((ErrorCodeIOException) throwable.getCause()).ErrCode == JODCONVERTER_TRANSFORMATION_ERROR_CODE)
        {
            getLogger().warn("Transformation failed: \n" +
                    "from file: " + tempFromFile + "\n" +
                    "to file: " + tempToFile +
                    "Source file " + tempFromFile + " has no content");
            produceEmptyPdfFile(tempToFile);
        }
        else
        {
            throw throwable;
        }
    }
}
 
Example #24
Source File: OfficeDocumentConverter.java    From kkFileViewOfficeEdit with Apache License 2.0 4 votes vote down vote up
public void convert(File inputFile, File outputFile) throws OfficeException {
    String outputExtension = FilenameUtils.getExtension(outputFile.getName());
    DocumentFormat outputFormat = formatRegistry.getFormatByExtension(outputExtension);
    convert(inputFile, outputFile, outputFormat);
}
 
Example #25
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 2 votes vote down vote up
/**
 * Override to modify the document after it has been loaded and before it gets
 * saved in the new format.
 * <p>
 * Does nothing by default.
 * 
 * @param document
 * @throws OfficeException
 */
protected void modifyDocument(XComponent document) throws OfficeException {
	// noop
}
 
Example #26
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 2 votes vote down vote up
/**
 * Override to modify the document after it has been loaded and before it gets
 * saved in the new format.
 * <p>
 * Does nothing by default.
 * 
 * @param document
 * @throws OfficeException
 */
protected void modifyDocument(XComponent document) throws OfficeException {
	// noop
}
 
Example #27
Source File: AbstractConversionTask.java    From wenku with MIT License 2 votes vote down vote up
/**
 * Override to modify the document after it has been loaded and before it gets
 * saved in the new format.
 * <p>
 * Does nothing by default.
 * 
 * @param document
 * @throws OfficeException
 */
protected void modifyDocument(XComponent document) throws OfficeException {
	// noop
}