Compress PDF: Taking more time

Hi All , We are trying to compress PDF document using ASPOSE Library. PDF size is approx 550 KB but its is taking 6 to 7 second to compress. Is this a expected behavior or we can do anything to enhance the performance.
Below is the code snippet
public String compressFile(
@DisplayName(“Base64 Encoded PDF File”) @Summary(“PDF file in Base64 encoded format”) String base64File)
throws Exception {
String compressedBase64File = “”;
int recursionCounter = 0;
InputStream licenseStream = null;

	try {
		LOGGER.info("PDF Compression Starts");
		byte[] origFileInBytes = Base64.getDecoder().decode(base64File);
		int currentSizeOfFile = origFileInBytes.length;
		// If the File is larger than 200KB then only compressing
		if (currentSizeOfFile > 204800) {
			LOGGER.debug("Compression Continues! Size of File is more than 200Kb::" + currentSizeOfFile);
			// Loading the Aspose License
			License pdfLicense = new License();
			licenseStream = HtmlpdfformatterOperations.class.getClassLoader()
					.getResourceAsStream("License/Aspose.PDF.Java.lic");
			// If License file fails to load
			if (licenseStream == null) {
				throw new FileNotFoundException("License File not found!");
			}
			LOGGER.debug("Loading the License");
			pdfLicense.setLicense(licenseStream);

			// Doing the compression
			compressedBase64File = compressionImpl(base64File);
			// Decoding the base64 file into byte array
			byte[] fileInBytes = Base64.getDecoder().decode(compressedBase64File);
			// Compressed File Size
			int compressedSize = fileInBytes.length;
			// POST Compression, if the file is still more than 200 KB , 204800 Bytes
			// and recursionCounter is less than or equal to 3
			while(compressedSize > 204800 && recursionCounter <3) {
				// Compress it again
				LOGGER.debug("recursionCounter value is: " + recursionCounter);
				LOGGER.debug("Re-Compressing, Size of previously compressed file: " + compressedSize);
				compressedBase64File = compressionImpl(base64File);
				recursionCounter++;
			}
			return compressedBase64File;
		} // Returning the file as is, its less than 200 KB
		else {
			compressedBase64File = base64File;
			LOGGER.info("Compression skipped! Size of File is less than 200Kb: " + currentSizeOfFile);
			return compressedBase64File;
		}
	} catch (FileNotFoundException exc) {
		LOGGER.error("FileNotFoundException occured: " + exc);
		throw new FileNotFoundException(exc.getMessage());
	} catch (IOException exc) {
		LOGGER.error("IOException occured: " + exc.getMessage());
		throw new IOException();
	} catch (InvalidPasswordException exc) {
		LOGGER.error("InvalidPasswordException occured: " + exc.getMessage());
		throw new InvalidPasswordException(exc.getMessage());
	} catch (PdfException exc) {
		LOGGER.error("PdfException occured: " + exc.getMessage());
		throw new PdfException(exc.getMessage());
	} catch (Exception exc) {
		LOGGER.error("Exception occured: " + exc);
		throw new Exception(exc.getMessage());
	} finally {
		if (licenseStream != null)
			licenseStream.close();
		recursionCounter=0;
	}
}

/*
 * Actual Compression Implementation
 */
private String compressionImpl(String base64File) throws Exception {
	InputStream fileInputStream = null;
	ByteArrayOutputStream compressedPdfBytesStream = null;
	com.aspose.pdf.Document pdfDocument = null;
	try {
		String outputFile = "";
		// Decoding the base64 file into byte array
		byte[] fileInBytes = Base64.getDecoder().decode(base64File);

		// Creating input stream from the byte array
		fileInputStream = new ByteArrayInputStream(fileInBytes);
		// Creating the output Stream to Store the compressed PDF
		compressedPdfBytesStream = new ByteArrayOutputStream();

		// Creating the Aspose Document Object for PDF Operations
		pdfDocument = new com.aspose.pdf.Document(fileInputStream);
		LOGGER.debug("Pages in the document is: " + pdfDocument.getPages().size());

		// Init OptimizationOptions
		OptimizationOptions opt = new OptimizationOptions();
		// Set ImageCompression option
		opt.getImageCompressionOptions().setCompressImages(true);
		// Set ImageQuality option
		opt.getImageCompressionOptions().setImageQuality(10);
		// Set ResizeImage option
		opt.getImageCompressionOptions().setResizeImages(true);
		// Set MaxResolution option
		opt.getImageCompressionOptions().setMaxResolution(100);
		// Performing the compression on original doc
		pdfDocument.optimizeResources(opt);

		// Saving the compressed PDF in Output Stream
		pdfDocument.save(compressedPdfBytesStream);
		// Size of compressed file
		LOGGER.debug("New Size of compressed file is: " + compressedPdfBytesStream.toByteArray().length);
		outputFile = Base64.getEncoder().encodeToString(compressedPdfBytesStream.toByteArray());

		return outputFile;

	} finally {
		//Closing all the open streams and objects
		if (pdfDocument != null)
			pdfDocument.close();
		if (fileInputStream != null)
			fileInputStream.close();
		if (compressedPdfBytesStream != null)
			compressedPdfBytesStream.close();
	}
}

Aspose version: 21.8

@monychoudhary

The time taken to compress a PDF document can vary based on several factors, including the size of the document, the complexity of its content, and the optimization settings used. In your case, a 550 KB PDF taking 6 to 7 seconds to compress may seem longer than expected, but it can be influenced by the specific operations being performed during compression.

Here are some suggestions to potentially enhance the performance of your PDF compression process:

  1. Optimization Options: Ensure that you are using the most efficient optimization settings. For instance, you can adjust the image quality and compression settings to find a balance between file size and processing time. In your code, you are currently setting the image quality to 10, which is quite low. You might want to experiment with higher values to see if it affects the compression time positively.

  2. Reduce Recursion: Your current implementation attempts to compress the file multiple times (up to three times) if the size remains above 200 KB. This could be adding to the processing time. Consider limiting the number of recursive compressions or adjusting the threshold for when to compress again.

  3. Profile Your Code: Use logging or profiling tools to identify which parts of your compression process are taking the most time. This can help you pinpoint any bottlenecks in your implementation.

  4. Use Flate Decode Compression: If you are not already using it, consider implementing Flate Decode Compression, which is known for efficiently reducing file sizes without significantly impacting quality. This method can be particularly effective for images within PDFs.

  5. Upgrade Aspose Version: If possible, consider upgrading to a newer version of the Aspose library. Performance improvements and optimizations are often included in newer releases.

Here is a simplified example of how you might set up the optimization options for better performance:

OptimizationOptions opt = new OptimizationOptions();
opt.getImageCompressionOptions().setCompressImages(true);
opt.getImageCompressionOptions().setImageQuality(75); // Adjust quality as needed
opt.getImageCompressionOptions().setMaxResolution(150); // Adjust resolution
pdfDocument.optimizeResources(opt);

By adjusting these settings and reviewing your implementation, you may be able to reduce the time taken for PDF compression.

If you continue to experience performance issues, consider reaching out to the Aspose support forum for more tailored assistance.

Sources:
[1]: Flate Decode Compression | Aspose.PDF for .NET API Reference
[2]: Optimize File Size In PDF File | Aspose.PDF for .NET API Reference