Convert to Image

GroupDocs.Conversion can 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());
}