Hyper-compress PDF documents

With a Professional or higher license, Nutrient Document Converter Services (DCS) enables hyper-compression during PDF conversions.

Hyper-compression includes:

  • JPEG2000 compression

  • JBIG2 compression

  • Color detection

  • Mixed raster content (MRC)

  • Content optimization by removing unnecessary elements

This guide explains how to apply hyper-compression settings using the DCS simple object access protocol (SOAP)-based API. The sample code below uses the OpenService and CloseService methods from the DocumentConverterServiceClient.

Sample code for applying hyper-compression using the API

To apply hyper-compression, add a CompressionSettings instance to the ConversionSettings object before initiating the conversion as shown in the code sample below:

/// <summary>
            /// PDF conversion, including compression.
            /// </summary>
            /// <param name="sourceFileName">Source file path</param>
            /// <param name="targetFolder">Path to target folder</param>
            static void PDFConversionCompression(string sourceFileName, string targetFolder)
        {
            DocumentConverterServiceClient client = null;
            try
            {
                // If the target folder does not exist, create it.
                if (!Directory.Exists(targetFolder))
                {
                    Directory.CreateDirectory(targetFolder);
                }
                // Determine the source file and read it into a byte array.
                byte[] sourceFile = File.ReadAllBytes(sourceFileName);

                // Open the service and configure the bindings.
                client = OpenService(ServiceURL);

                // Set the absolute minimum open options.
                OpenOptions openOptions = new OpenOptions();
                openOptions.OriginalFileName = Path.GetFileName(sourceFileName);
                openOptions.FileExtension = Path.GetExtension(sourceFileName);

                // Set the absolute minimum conversion settings.
                ConversionSettings conversionSettings = new ConversionSettings();
                conversionSettings.Fidelity = ConversionFidelities.Full;
                conversionSettings.Quality = ConversionQuality.OptimizeForPrint;

                // Set compression settings.
                CompressionSettings compressionSettings = new CompressionSettings();

                // Enable JPEG2000 compression.
                compressionSettings.EnableJPEG2000 = BooleanEnum.True;

                // Enable JBIG2 compression.
                compressionSettings.EnableJBIG2 = BooleanEnum.True;
                compressionSettings.JBIG2PMSThreshold = 85;

                // Enable color detection.
                compressionSettings.EnableColorDetection = BooleanEnum.True;

                // Mixed raster compression.
                compressionSettings.EnableMRC = BooleanEnum.True;

                // Remove unneeded content.
                compressionSettings.RemoveAnnotations = BooleanEnum.True;
                compressionSettings.RemoveBlankPages = BooleanEnum.True;
                compressionSettings.RemoveBookmarks = BooleanEnum.True;
                compressionSettings.RemoveEmbeddedFiles = BooleanEnum.True;
                compressionSettings.RemoveFormFields = BooleanEnum.True;
                compressionSettings.RemoveHyperlinks = BooleanEnum.True;
                compressionSettings.RemoveJavaScript = BooleanEnum.True;
                compressionSettings.RemoveMetadata = BooleanEnum.True;
                compressionSettings.RemovePageThumbnails = BooleanEnum.True;

                // Carry out the conversion.
                byte[] convFile = client.Convert(sourceFile, openOptions, conversionSettings);

                // Write the converted file back to the file system with a PDF extension.
                string destinationFileName = Path.Combine(targetFolder, Path.GetFileNameWithoutExtension(sourceFileName) + "." + conversionSettings.Format);
                using (FileStream fs = File.Create(destinationFileName))
                {
                    fs.Write(convFile, 0, convFile.Length);
                    fs.Close();
                }
                Console.WriteLine("File converted to " + destinationFileName);
            }
            catch (FaultException<WebServiceFaultException> ex)
            {
                Console.WriteLine($"FaultException occurred: ExceptionType: {ex.Detail.ExceptionType.ToString()}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                if (client != null)
                {
                    CloseService(client);
                }
            }
        }