Convert to Image

GroupDocs.Conversioncan convert any source document to the following image formats: Tiff, Tif, Jpg, Jpeg, Png, Gig, Bmp, Ico, Psd, Wmf, Emf, Dcm, Webp, Dng, Svg, Jp2, Odg, J2c, J2k, Jpx, Jpf, Jpm, Eps, Cgm, Cdr, Cmx, Dib, Jpc, Jls, DjVu. When just instantiate the ImageConvertOptions class without specifying the target format explicitly, Jpg will be used as a default format.

Conversion to image format could be triggered by following below steps:

  • Create new instance of Converter class and pass source document path as a constructor parameter
  • Instantiate ImageConvertOptions class.
  • Call setFormatmethod of the ImageConvertOptions instance to specify the desired image format
  • Declare output stream each document page will be stored. This delegate will be called for each page during conversion.
  • Call Convert method of Converter class instance and pass the declared output stream and the instance of ImageConvertOptions from the previous two steps

Conversion to JPG

The following code show how to convert any document to JPG. 

String outputFileTemplate = new File(outputFolder, "converted-page-%d.jpg").getPath();

try (FileOutputStream getPageStream = new FileOutputStream(String.format(outputFileTemplate, 1))) {

    Converter converter = new Converter("sample.pdf");

    ImageConvertOptions options = new ImageConvertOptions();
    options.setFormat(ImageFileType.Jpg);
    options.setPagesCount(1);

    converter.convert(getPageStream, options);
} catch (IOException e) {
    System.out.println(e.getMessage());
}

Conversion to PNG

The following code show how to convert any document to PNG. 

String outputFileTemplate = new File(outputFolder, "converted-page-%d.png").getPath();

try(FileOutputStream getPageStream = new FileOutputStream(String.format(outputFileTemplate, 1))) {

    Converter converter = new Converter("sample.pdf");
    ImageConvertOptions options = new ImageConvertOptions();
    options.setFormat(ImageFileType.Png);
    options.setPagesCount(1);
    converter.convert(getPageStream, options);
} catch (IOException e) {
    System.out.println(e.getMessage());
}

Conversion to PSD

The following code show how to convert any document to PSD. 

String outputFileTemplate = new File(outputFolder, "converted-page-%d.psd").getPath();

try(FileOutputStream getPageStream = new FileOutputStream(String.format(outputFileTemplate, 1))) {

    Converter converter = new Converter("sample.pdf");
    ImageConvertOptions options = new ImageConvertOptions();
    options.setFormat(ImageFileType.Psd);
    options.setPagesCount(1);
    converter.convert(getPageStream, options);
} catch (IOException e ){
    System.out.println(e.getMessage());
}

More resources

Advanced Usage Topics

To learn more about document viewing features, please refer to the advanced usage section.

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Document Converter Apps

Along with full-featured Java library we provide free Apps and free services for document conversion.
In order to see a full potential of GroupDocs.Conversion, you are welcome to convert DOC to PDF, DOC to XLSX, PDF to DOC, PDF to XLSX, PPT to DOC and more with Free Online Document Converter App or get a full advantage of Free Online Document Converter Suite with advanced conversion settings and many more enterprise built-in features.
Please note that more premium features, advanced options and enhanced document management experience is available for signed-in users at conholdate.app for FREE.
If you don’t own an account yet, register it now for free! No credit card is required!