Quick Start Guide

This guide provides a quick overview of how to set up and start using GroupDocs.Conversion for Java. This library enables developers to convert between various file formats (e.g., DOCX, PDF, PNG) with minimal configuration.

Prerequisites

To proceed, make sure you have:

Set Up Your Development Environment

Install GroupDocs.Conversion for Java

  1. Download the latest GroupDocs.Conversion for Java from the official website or include it in your project via Maven:
<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-conversion</artifactId>
    <version>latest-version</version>
</dependency>
  1. If using a Gradle project, add this dependency to build.gradle:
dependencies {
    implementation 'com.groupdocs:groupdocs-conversion:latest-version'
}

Configure Your Java Environment

Ensure that you are using:

  • Java 8 or later
  • A compatible build tool (Maven/Gradle/Ant)
  • An IDE such as IntelliJ IDEA, Eclipse, or Visual Studio Code

If you are using a Java web application, configure the dependency in the respective WAR/JAR structure and ensure all dependencies are resolved.

Example 1: Convert DOCX to PDF

Java Implementation

To quickly test the library, let’s convert a DOCX file to PDF.

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import java.io.File;

public class ConvertDocxToPdf {
    public static void main(String[] args) {
        String inputFilePath = "./business-plan.docx";
        String outputFilePath = "./business-plan.pdf";
        
        try (Converter converter = new Converter(inputFilePath)) {
            PdfConvertOptions options = new PdfConvertOptions();
            converter.convert(outputFilePath, options);
            System.out.println("Conversion completed successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

business-plan.docx is sample file used in this example. Click here to download it.

business-plan.pdf is expected output PDF file. Click here to download it.

Explanation

  • Converter("./business-plan.docx"): Initializes the converter with the DOCX file.
  • PdfConvertOptions(): Specifies the output format as PDF.
  • converter.convert("./business-plan.pdf", options): Converts the DOCX file to PDF and saves it.

Example 2: Convert PDF Pages to PNG

Java Implementation

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.contracts.SavePageStream;
import com.groupdocs.conversion.filetypes.ImageFileType;
import com.groupdocs.conversion.options.convert.ImageConvertOptions;
import java.io.FileOutputStream;
import java.io.IOException;

public class ConvertPdfPagesToPng {
    public static void main(String[] args) {
        String inputFilePath = "./annual-review.pdf";
        String outputFile = "./converted-pages/pdf-converted-page-%d.png";

        SavePageStream getPageStream = page -> {
            try {
                return new FileOutputStream(String.format(outputFile, page));
            } catch (IOException e) {
                e.printStackTrace();
                return null; // Return null to handle it later
            }
        };

        try (Converter converter = new Converter(inputFilePath)) {
            ImageConvertOptions options = new ImageConvertOptions();
            options.setFormat(ImageFileType.PNG);
            
            converter.convert(getPageStream, options);
            System.out.println("PDF pages converted to PNG successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

annual-review.pdf is sample file used in this example. Click here to download it.

converted-pages is output folder where converted pages are stored. Click here to download the output PNG files.

Explanation

  • Converter("./annual-review.pdf"): Initializes the converter with the PDF file.
  • SavePageStream: Implements a lambda function that provides an output stream for each page.
  • ImageConvertOptions(): Specifies the output format as image.
  • ImageFileType.PNG: Sets the output image format to PNG.
  • converter.convert(getPageStream, png_convert_options): Converts the PDF file pages to PNG and saves output file in converted-pages folder.

Next Steps

After completing the basics, explore additional resources to enhance your usage:

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.