Convert ODT to PDF in Java

·

1 min read

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>

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);

    }
}

ODTtoPDF.jpg