Backend Development 6 min read

Implementing Online Office Document Preview in Java Using OpenOffice and JODConverter

This article explains how to build a free Java-based online preview service for Word, Excel, and PowerPoint files by installing Apache OpenOffice, adding JODConverter dependencies, creating a conversion utility class, and exposing the functionality through SpringBoot service and controller layers.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Implementing Online Office Document Preview in Java Using OpenOffice and JODConverter

Java developers often need to preview office documents (Word, Excel, PowerPoint) directly in a browser without paying for third‑party services; using Apache OpenOffice together with JODConverter provides a free solution that converts these files to PDF streams.

Step 1: Download and install Apache OpenOffice from the official website (installation method varies by operating system).

Step 2: Add the JODConverter dependency to your project's pom.xml :

<!--openoffice-->
<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>

Step 3: Implement a utility class FileConvertUtil that provides methods to convert local or remote office files to PDF streams using OpenOffice connections.

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * File format conversion utility class
 */
public class FileConvertUtil {
    private static final String DEFAULT_SUFFIX = "pdf";
    private static final Integer OPENOFFICE_PORT = 8100;
    // methods convertLocaleFile, convertNetFile, covertCommonByStream, outputStreamConvertInputStream ...
}

Step 4 (Service layer): Use the utility to create an onlinePreview method that validates the file extension, obtains an InputStream of the converted PDF, and writes it to the HTTP response.

public void onlinePreview(String url, HttpServletResponse response) throws Exception {
    String[] str = SmartStringUtil.split(url, "\\.");
    if (str.length == 0) { throw new Exception("文件格式不正确"); }
    String suffix = str[str.length - 1];
    if (!suffix.matches("txt|doc|docx|xls|xlsx|ppt|pptx")) {
        throw new Exception("文件格式不支持预览");
    }
    InputStream in = FileConvertUtil.convertNetFile(url, suffix);
    OutputStream out = response.getOutputStream();
    byte[] buff = new byte[1024];
    int n;
    while ((n = in.read(buff)) != -1) {
        out.write(buff, 0, n);
    }
    out.flush();
    out.close();
    in.close();
}

Step 5 (Controller layer): Expose the preview endpoint via a SpringBoot REST controller.

@ApiOperation(value = "系统文件在线预览接口 by tarzan")
@PostMapping("/api/file/onlinePreview")
public void onlinePreview(@RequestParam("url") String url, HttpServletResponse response) throws Exception {
    fileService.onlinePreview(url, response);
}

By following these steps, you can integrate a free, OpenOffice‑based online document preview feature into any Java web application.

backendJavaSpringBootpdfFileConversionOpenOffice
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.