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

Top comments (0)

Top Heroku Alternatives (For Free!)

Recently Heroku shut down free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis on November 28th, 2022. So Meshv Patel put together some free alternatives in this classic DEV post.