Setting temporary directory and cleaning image resources

Skip to end of metadata
Go to start of metadata
In this article, we’ll see how to get or set a directory to save temporary images. We’ll also understand the process of cleaning up the temporary images once the image extraction process is finished.
Explanation

PdfExtractor class creates temporary images while extracting images from a PDF file. Aspose.Pdf.Kit for Java has introduced a class named Settings which allows you to get or set the temporary directory. In order to get the temporary directory you can use getTemporaryDirectory method, while setTemporaryDirectory method allows you to specify the temporary directory. However, you first need to create the temporary directory.
In order to cleanup images after the image extraction process is finished, you only need to call the close method of PdfExtractor class.

The following code snippet shows you how to get or set temporary directory and clean up temporary images.

[Java]
//create Settings object
Settings setting = new Settings();
//set temporary directory to keep temporary images
setting.setTemporaryDirectory("temp\\");
//get temporary directory
System.out.println(setting.getTemporaryDirectory());

//extract images using PdfExtractor class
PdfExtractor extractor = new PdfExtractor();
extractor.bindPdf("input.pdf");
extractor.extractImage();
String suffix = ".png";

String path = "out\\";

nt imageCount = 1;
while (extractor.hasNextImage())
{
   String strImagePath  = path + imageCount + suffix;
   extractor.getNextImage(strImagePath, ImageType.PNG);
   imageCount++;
}

//call close method to clean up temporary images
extractor.close();
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.