DEV Community

carlwils
carlwils

Posted on

 

Convert ODT to PDF in Java

An ODT file is a text document containing formatted text and may also include images, drawn objects and tables. ODT files are similar to MS Word files, and sometimes you may need to convert the ODT files to PDFs. This article will share how to do the conversion programmatically using Free Spire.Doc for Java.

Install the Library

Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Sample Code

Free Spire.Doc for Java allows you to load an ODT file using Document.loadFromFile() method, and then you can convert it to PDF using Document.saveToFile(String fileName, FileFormat fileFormat) method. The complete sample code is shown as below.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertOdtToPdf {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load an ODT file
        doc.loadFromFile("Test.odt");

        //Save the ODT file to PDF
        doc.saveToFile("OdtToPDF.pdf", FileFormat.PDF);

    }
}
Enter fullscreen mode Exit fullscreen mode

OdtToPDF

Latest comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.