Programmatically turning OneNote pages into picture files is a common need when building preview generators or mobile viewers. The Conholdate.Total for Java SDK makes it easy to convert Onenote to image in Java without requiring Microsoft Office on the server. In this guide you will see a complete, thread‑safe implementation, options for PNG and JPEG output, and tips for in‑memory processing.

Convert Onenote to Image in Java - Step‑by‑Step Guide

  1. Initialize the conversion workflow: Load the OneNote file into an InputStream and create a Converter instance - the core of the convert Onenote to image in Java workflow.

    InputStream fileStream = new BufferedInputStream(new FileInputStream("sample.one"));
    Converter converter = new Converter(fileStream);
    
  2. Determine how many pages need conversion: Retrieve the total page count from the Converter.

    int pageCount = converter.getPageCount();
    
  3. Configure image options for each page: Set the output format, quality, and the zero‑based page index using ImageConvertOptions.

    ImageConvertOptions options = new ImageConvertOptions();
    options.setFormat(ImageSaveOptions.ImageFormat.PNG);
    options.setQuality(90);
    options.setPageNumber(pageIndex);
    
  4. Convert pages in parallel: Use an ExecutorService to run conversions concurrently, writing each page to a separate PNG file.

    ExecutorService executor = Executors.newFixedThreadPool(
            Runtime.getRuntime().availableProcessors());
    executor.submit(() -> {
        // conversion logic per page
    });
    
  5. Optional in‑memory conversion: For scenarios where you need the image bytes directly (e.g., sending over a network), convert the first page to a ByteArrayOutputStream.

    ImageConvertOptions memOptions = new ImageConvertOptions();
    memOptions.setFormat(ImageSaveOptions.ImageFormat.JPEG);
    memOptions.setPageNumber(0);
    ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
    converter.convert(memoryStream, memOptions);
    

For more details on the Converter class and its methods, see the official API reference.

Full Working Example for Convert Onenote to Image in Java - Parallel Page Processing

The following code shows a full implementation of how to convert Onenote to image in Java using Conholdate.Total.

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.ImageConvertOptions;
import com.groupdocs.conversion.options.convert.ImageSaveOptions;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConvertOneNoteToImage {
    public static void main(String[] args) {
        String inputPath = "sample.one";
        String outputPattern = "output_page_%d.png";
        int imageQuality = 90; // 0-100

        // Thread pool for parallel page conversion
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        try (InputStream fileStream = new BufferedInputStream(new FileInputStream(inputPath));
             Converter converter = new Converter(fileStream)) {

            int pageCount = converter.getPageCount();

            for (int i = 0; i < pageCount; i++) {
                final int pageIndex = i;
                executor.submit(() -> {
                    ImageConvertOptions options = new ImageConvertOptions();
                    options.setFormat(ImageSaveOptions.ImageFormat.PNG);
                    options.setQuality(imageQuality);
                    options.setPageNumber(pageIndex); // zero‑based page index

                    String outputPath = String.format(outputPattern, pageIndex + 1);
                    try (OutputStream outStream = new BufferedOutputStream(new FileOutputStream(outputPath))) {
                        converter.convert(outStream, options);
                        System.out.println("Page " + (pageIndex + 1) + " saved to " + outputPath);
                    } catch (Exception e) {
                        System.err.println("Failed to convert page " + (pageIndex + 1) + ": " + e.getMessage());
                    }
                });
            }

            // Example of in‑memory conversion for the first page
            ImageConvertOptions memOptions = new ImageConvertOptions();
            memOptions.setFormat(ImageSaveOptions.ImageFormat.JPEG);
            memOptions.setQuality(imageQuality);
            memOptions.setPageNumber(0);
            try (ByteArrayOutputStream memoryStream = new ByteArrayOutputStream()) {
                converter.convert(memoryStream, memOptions);
                byte[] imageBytes = memoryStream.toByteArray();
                System.out.println("In‑memory conversion produced " + imageBytes.length + " bytes for page 1.");
                // imageBytes can now be sent over network, stored in DB, etc.
            } catch (Exception e) {
                System.err.println("In‑memory conversion error: " + e.getMessage());
            }

        } catch (IOException e) {
            System.err.println("File access error: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("Conversion initialization error: " + e.getMessage());
        } finally {
            executor.shutdown();
            try {
                if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
                    executor.shutdownNow();
                }
            } catch (InterruptedException ie) {
                executor.shutdownNow();
                Thread.currentThread().interrupt();
            }
        }
    }
}

Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (sample.one, output_page_%d.png, etc.) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.

Installing and Configuring Conholdate.Total for Java

Add the Conholdate Maven repository and the SDK dependency to your pom.xml:

<repositories>
    <repository>
        <id>conholdate-repo</id>
        <name>Conholdate Maven Repository</name>
        <url>https://repository.conholdate.com/repo/</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.conholdate</groupId>
    <artifactId>conholdate-total</artifactId>
    <version>24.9</version>
    <type>pom</type>
</dependency>

Download the latest binary package from the download page. The SDK requires Java 8 or higher and runs on any standard JVM. For production use, obtain a license from the pricing page and activate it with a temporary key from the temporary license page.

Conclusion

By following this guide you now have a solid foundation for converting OneNote to image in Java using Conholdate.Total for Java. The example demonstrates page‑by‑page PNG output, parallel processing for speed, and an in‑memory option for flexible integration. Remember to configure the ImageConvertOptions to match your required image format and quality, and to handle resources with try‑with‑resources blocks as shown. For commercial projects, secure a proper license via the pricing page and test the conversion with real OneNote files to ensure all content renders correctly. The SDK’s extensive API and documentation make it straightforward to extend this solution to batch processing or cloud‑based services.

FAQs

  • Can I convert Onenote to image in Java without installing Microsoft Office?
    Yes. The Conholdate.Total SDK performs all rendering internally, so no Office installation is needed on the server.

  • Which image formats does the conversion support?
    You can export to PNG, JPEG, BMP, GIF, and TIFF by setting the ImageSaveOptions.ImageFormat property.

  • How do I improve performance for large notebooks?
    Use the built‑in thread pool as demonstrated, and prefer in‑memory conversion when you only need the image bytes, avoiding disk I/O.

  • Where can I find more examples and API details?
    The official documentation provides extensive guides, and the API reference lists all classes and methods.

Read More