DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert PDF to Text in Java

Trending

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • How AI Is Changing the Way Developers Write Code
  • How to Build Local LLM RAG Apps With Ollama, DeepSeek-R1, and SingleStore
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Coding
  3. Java
  4. How to Convert ODT Files to PDF in Java

How to Convert ODT Files to PDF in Java

Convert Office Open Document Text File (ODT) to standard PDF, DOCX, PNG, and JPG using Java.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Nov. 17, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.5K Views

Join the DZone community and get the full member experience.

Join For Free

Microsoft has maintained its position in the spotlight for formatted document creation and editing for good reason. Its extreme ease of use and lack of a learning curve has transformed the Microsoft Office Suite into a household name for most computer users in the United States as well as globally. This is propagated further through its almost ubiquitous use in education, as students are raised and taught using these applications. 

The issue that arises with these programs, however, is their operation costs. For Apple and other non-Windows-based Operating Systems, the purchasing fees for Office can be steep. This, then, creates a paywall separating potential users from programs to which they are already accustomed. As an answer this problem, Microsoft created the OpenOffice application, which is a free, opensource version of the classic Office Suite. Within this application, you can perform almost all of the same functions as Office Suite, including creating text documents like one would with Microsoft Word. These text documents can be made using OpenOffice Writer, and are formatted using the .ODT file type. While this file type can be opened and saved using OpenOffice Writer and Word, in order to convert the file to a different format such as PDF you will need to run it through a conversion process. 

The following APIs will allow you to convert your ODT documents to PDF, DOCX, PNG, and JPG for use in whatever way you need. The goal of this tutorial is to provide a simple and efficient means for instantly converting your ODT files without needing to find or download any extraneous programming.

To run any of these functions, you will first need to install the SDK library. To install with Maven, you can add a Jitpack reference to the repository in pom.xml:

XML
xxxxxxxxxx
1
 
1
<repositories>
2
    <repository>
3
        <id>jitpack.io</id>
4
        <url>https://jitpack.io</url>
5
    </repository>
6
</repositories>


Then, you can add a reference to the dependency:

XML
xxxxxxxxxx
1
 
1
<dependencies>
2
<dependency>
3
    <groupId>com.github.Cloudmersive</groupId>
4
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
5
    <version>v3.54</version>
6
</dependency>
7
</dependencies>


To install with Gradle, you can add the reference in your root build.gradle at the end of repositories:

Java
xxxxxxxxxx
1
 
1
allprojects {
2
    repositories {
3
        ...
4
        maven { url 'https://jitpack.io' }
5
    }
6
}


Then, you can add the dependency in build.gradle:

Java
xxxxxxxxxx
1
 
1
dependencies {
2
        implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'
3
}


The first function listed will convert an ODT to standard PDF instantly. Once you have installed the SDK as shown above, you can call the function:

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    byte[] result = apiInstance.convertDocumentOdtToPdf(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdtToPdf");
23
    e.printStackTrace();
24
}


This will return a downloadable PDF file that can then be used however you need it. To ensure that this API works properly, you need to ensure certain requirements are met:  

  • The ODT file is valid and input correctly.
  • You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.

As stated previously, Word can already be used to open ODT files. However, this will not convert the file to DOCX; it will remain an ODT file unless saved as different file format. You can shorten this process entirely, though, by removing the extra steps of opening the document in Word and manually saving it as a DOCX. The second API shown here will convert your ODT files to DOCX automatically without any extra work.

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    byte[] result = apiInstance.convertDocumentOdtToDocx(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdtToDocx");
23
    e.printStackTrace();
24
}


Once again, this will return a downloadable file that can then be edited and shared as a DOCX document.

If you need to use your ODT document in a more stable format, converting it to an image array may be your ideal option. The following two APIs will convert your ODT document to PNG and JPG image files, respectively. 

A PNG file would be most effective when creating images for online use, due to its lossless format and background transparency capabilities. Note, these files tend to be somewhat larger than the lossy JPG file format. To convert your ODT file to a PNG image array, install the SDK as shown above and call the function as shown below:

Java
xxxxxxxxxx
1
24
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
try {
19
    OdtToPngResult result = apiInstance.convertDocumentOdtToPng(inputFile);
20
    System.out.println(result);
21
} catch (ApiException e) {
22
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdtToPng");
23
    e.printStackTrace();
24
}


This will return a URL containing the PNG image or images for download.

If you would rather use your document in a similar way to a photographic image, with better capabilities for scaling and defining image quality. As a lossy format, you can compress your JPG file to a more functional file size according to your needs. Furthermore, with this API, you can specify the JPG quality level with the lowest quality and highest compression at 1 and the highest quality and lowest compression at 100. The recommended and default value for this parameter is 75. 

You can call the ODT to JPG function as shown below:

Java
x
25
 
1
// Import classes:
2
//import com.cloudmersive.client.invoker.ApiClient;
3
//import com.cloudmersive.client.invoker.ApiException;
4
//import com.cloudmersive.client.invoker.Configuration;
5
//import com.cloudmersive.client.invoker.auth.*;
6
//import com.cloudmersive.client.ConvertDocumentApi;
7
8
ApiClient defaultClient = Configuration.getDefaultApiClient();
9
10
// Configure API key authorization: Apikey
11
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
12
Apikey.setApiKey("YOUR API KEY");
13
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
14
//Apikey.setApiKeyPrefix("Token");
15
16
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
17
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
18
Integer quality = 56; // Integer | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.
19
try {
20
    OdtToJpgResult result = apiInstance.convertDocumentOdtToJpg(inputFile, quality);
21
    System.out.println(result);
22
} catch (ApiException e) {
23
    System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdtToJpg");
24
    e.printStackTrace();
25
}


This will return a downloadable file containing your image or image array.

With these functions, you can easily create high quality documents without the cost associated with Microsoft Office Suite. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need.

file IO PDF Convert (command) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Convert ODF Files to PDF in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert PDF to Text in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!