Support for DjVu File Format with Aspose.Imaging for Java 3.0.0

Share on FacebookTweet about this on TwitterShare on LinkedIn

Aspose.Imaging for Java logoWe are pleased to announce the September’s release of Aspose.Imaging for Java. This month’s release contains many useful improvements including the long awaited support for DjVu file format. Please refer to the release notes of Aspose.Imaging for Java 3.0.0 for a full list of bug fixes and improvements along with sample code snippets for newly added features. If you are planning to upgrade the API to the latest revision, we strongly suggest you to check the Public API Change section to know what has been changed in the public API since your current version.

While you are downloading the latest build, here is a look at just a few of the showcased features of this release.

Support for DjVu File Format

DjVu file format is becoming popular as an alternative to PDF because it has smaller file size as compared to PDF. DjVu format can also be considered as a superior alternative to PDF for digital documents, to TIFF for scanned bitonal documents, to JPEG and JPEG2000 for photographs and pictures, and to GIF for large palatalized images.

With the release of Aspose.Imaging for Java 3.0.0, the API has provided the support for loading DjVu files for possible conversion to raster image formats as well as PDF. In order to provide this support, the API has exposed DjvuImage class along with many new helping classes, methods & properties listed in the Public API Changes in Aspose.Imaging for Java 3.0.0.

Conversion of DjVu to Multi Frame Raster Formats

Like PDF & TIFF, the DjVu files can have multiple pages/frames therefore while converting the DjVu files to raster formats such as TIFF & GIF, all pages of DjVu are rendered to the resultant image by default. Here is the code to convert all pages of DjVu to TIFF format.

//Load a DjVu image
DjvuImage image = (DjvuImage)Image.load(sourceFilename);     

//Create an instance of TiffOptions & use preset options for Black n While with Deflate compression
TiffOptions exportOptions = new TiffOptions(TiffExpectedFormat.TiffDeflateBW);

//Initialize the DjvuMultiPageOptions
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions());

//Call Save method while passing instance of TiffOptions
image.save(destinationFilename, exportOptions);

Conversion of DjVu to PDF Format

Aspose.Imaging for Java 3.0.0 can also be used to convert the DjVu files to PDF format while using the existing PdfOptions class. It is up to the user requirement if all DjVu pages or a subset of the pages are to be exported in PDF format.

Following code snippet demonstrates the usage of Aspose.Imaging for Java 3.0.0 to convert a subset of DjVu pages to PDF format.

//Load a DjVu image
DjvuImage image = (DjvuImage)Image.load(sourceFilename);     

//Create an instance of PdfOptions
PdfOptions exportOptions = new PdfOptions();

//Initialize the metadata for Pdf document
exportOptions.setPdfDocumentInfo(new PdfDocumentInfo());

//Create an instance of IntRange and initialize it with the range of DjVu pages to be exported
IntRange range = new IntRange(0, 5); //Export first 5 pages

//Initialize an instance of DjvuMultiPageOptions with range of DjVu pages to be exported 
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range));

//Save the result in PDF format
image.save(destinationFilename, exportOptions);

Conversion of Specific DjVu Pages to Multi Frame Raster Formats

Aspose.Imaging for Java 3.0.0 has exposed a new class IntRange that can be used to specify a range that in turn can be passed to an instance of DjvuMultiPageOptions for the conversion of specific range of DjVu pages to TIFF or GIF.

Following piece of code uses the IntRange & DjvuMultiPageOptions classes to render a subset of DjVu pages to TIFF.

//Load a DjVu image
DjvuImage image = (DjvuImage)Image.load(sourceFilename);     

//Create an instance of TiffOptions with preset options
TiffOptions exportOptions = new TiffOptions(TiffExpectedFormat.TiffDeflateBW);

//Create an instance of IntRange and initialize it with range of pages to be exported
IntRange range = new IntRange(0, 2); //Export first 2 pages

//Initialize an instance of DjvuMultiPageOptions while passing instance of IntRange
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range));

//Call Save method while passing instance of TiffOptions
image.save(destinationFilename, exportOptions);

Conversion of DjVu Pages to Separate Images

It is also possible to store each DjVu page to separate raster image. In order to achieve this goal, the latest version of Aspose.Imaging for Java 3.0.0 has exposed an overload constructor of DjvuMultiPageOptions that could accept an integer value to export the specific page. This mechanism can be extended to render individual DjVu pages to raster formats.

Following piece of code uses the DjvuMultiPageOptions class to export 2 DjVu pages to separate BMP images.

//Load a DjVu image
DjvuImage image = (DjvuImage)Image.load(sourceFilename);     

//Create an instance of BmpOptions
BmpOptions exportOptions = new BmpOptions();

//Set BitsPerPixel for resultant images
exportOptions.setBitsPerPixel(32);

//Create an instance of IntRange and initialize it with range of pages to be exported
IntRange range = new IntRange(0, 2); //Export first 2 pages

int counter = 0;
for (int i : range.getRange())
{
    //Save each page in separate file, as BMP do not support layering
    exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(range.getArrayOneItemFromIndex(counter)));
    image.save(String.format("{0}.bmp", counter++), exportOptions);
}

Conversion of Specific Portion of DjVu Page to Image

Another overload constructor of DjvuMultiPageOptions class can accept an integer value as well as an instance of Rectangle to export a specific portion of DjVu page to raster image format.

Following piece of code creates an instance of Rectange to specify the location and size of the desired area to be exported in PNG format.

//Load a DjVu image
DjvuImage image = (DjvuImage)Image.load(sourceFilename);     

//Create an instance of PngOptions
PngOptions exportOptions = new PngOptions();

//Set ColorType to Grayscale
exportOptions.setColorType(PngColorType.Grayscale);

//Create an instance of Rectangle and specify the portion on DjVu page
Rectangle exportArea = new Rectangle(0, 0, 500, 500);

//Specify the DjVu page index
int exportPageIndex = 2;

//Initialize an instance of DjvuMultiPageOptions
//while passing index of DjVu page index and instance of Rectangle covering the area to be exported
exportOptions.setMultiPageOptions(new DjvuMultiPageOptions(exportPageIndex, exportArea));

//Save the image
image.save(destinationFilename, exportOptions);

As always we appreciate your feedback so if you ever have anything to tell us about this release or anything else, please head to the Aspose.Imaging forum for a chat.