Samples Overview

Optimizing | Removing | Features and Configuration | In Memory

Optimizing

Optimize PDF document for archiving

Apply the optimization profile for archiving to reduce the file size while following the archive-typical imperative of preserving all information. Redundant objects are removed, embedded font files are merged and reduced to the subset of used glyphs. Resources are optimized and images are compressed depending on their type.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Choose the optimization profile for archiving
    optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileArchive;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Choose the optimization profile for archiving
optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileArchive);

// Open input file
if (!optimizer.open(inputPath, "")) 
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption)) 
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Choose the optimization profile for archiving
PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileArchive);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input amd output files
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Optimize PDF document with Mixed Raster Content (MRC)

Reduce the file size for documents containing large images, e.g. scanned pages, while maintaining the readability of text. This is accomplished by separating the images into a foreground, background and a mask layer. The foreground and background layers are heavily down-sampled and compressed. The textual information is stored in the mask with a lossless compression type. Additionally, redundant objects are removed, resources are optimized and embedded fonts are merged.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Choose the optimization profile for MRC
    optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileMRC;

    // Set MRC foreground and background layer resolution
    optimizer.MrcLayerResolutionDPI = resolution;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Choose the optimization profile for MRC
optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileMRC);

// Set MRC foreground and background layer resolution
optimizer.setMrcLayerResolutionDPI(resolution);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Choose the optimization profile for MRC
PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileMRC);

// Set MRC foreground and background layer resolution
PdfOptimizeSetMrcLayerResolutionDPI(pOptimizer, fResolution);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Merge and subset fonts from PDF document

Merge multiple embedded font programs and remove all unused glyphs. Additionally, Type 1 font programs are converted to CFF (compact font format).

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Choose subset and merge embedded fonts
    optimizer.SubsetFonts = true;
    optimizer.MergeEmbeddedFonts = true;

    // Convert Type1 font programs to CFF (compact font format)
    optimizer.ConvertToCFF = true;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Choose subset and merge embedded fonts
optimizer.setSubsetFonts(true);
optimizer.setMergeEmbeddedFonts(true);

// Convert Type1 font programs to CFF (compact font format)
optimizer.setConvertToCFF(true);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Choose subset and merge embedded fonts
PdfOptimizeSetSubsetFonts(pOptimizer, 1);
PdfOptimizeSetMergeEmbeddedFonts(pOptimizer, 1);

// Convert Type1 font programs to CFF (compact font format)
PdfOptimizeSetConvertToCFF(pOptimizer, 1);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Optimize images of PDF document

Reduce the size of a PDF file by down-sampling and re-compressing embedded images. Set a target resolution and define a set of compression types for the optimizer to intelligently choose from. Optionally set the compression quality for lossy compression types.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Set candidate compression types
    optimizer.BitonalCompressions = PDFComprAttempt.eComprAttemptGroup4 | 
        PDFComprAttempt.eComprAttemptJBIG2 | PDFComprAttempt.eComprAttemptSource;
    optimizer.ContinuousCompressions = PDFComprAttempt.eComprAttemptJPEG | 
        PDFComprAttempt.eComprAttemptJPEG2000 | PDFComprAttempt.eComprAttemptSource;
    optimizer.IndexedCompressions = PDFComprAttempt.eComprAttemptFlate | 
        PDFComprAttempt.eComprAttemptLZW | PDFComprAttempt.eComprAttemptSource;

    // Set target resolution for down-sampling
    optimizer.ResolutionDPI = resolution;

    // Down-sample images if their resolution is greater than 130% of the target resolution
    optimizer.ThresholdDPI = (float)1.3 * optimizer.ResolutionDPI;

    // Apply a higher resolution to bi-tonal images
    optimizer.BitonalResolutionDPI = (float)1.2 * resolution;
    optimizer.BitonalThresholdDPI = (float)1.3 * optimizer.BitonalResolutionDPI;

    // Set image quality (for lossy compression types)
    optimizer.ImageQuality = quality;

    // Clip invisible parts of images
    optimizer.ClipImages = true;

    // Convert images' color spaces to the least complex possible
    optimizer.ReduceColorComplexity = true;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Set candidate compression types
optimizer.setBitonalCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptGroup4 | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJBIG2 | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);
optimizer.setContinuousCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJPEG | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptJPEG2000 | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);
optimizer.setIndexedCompressions(NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptFlate | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptLZW | 
    NativeLibrary.COMPRESSIONATTEMPT.eComprAttemptSource);

// Set target resolution for down-sampling
optimizer.setResolutionDPI(resolution);

// Down-sample images if their resolution is greater than 130% of the target resolution 
optimizer.setThresholdDPI((float)1.3 * resolution);

// Apply a higher resolution to bitonal images
optimizer.setBitonalResolutionDPI((float)1.2 * resolution);
optimizer.setBitonalThresholdDPI((float)1.3 * optimizer.getBitonalResolutionDPI());

// Set image quality (for lossy compression type)
optimizer.setImageQuality(quality);

// Clip invisible parts of images
optimizer.setClipImages(true);

// Convert images' color spaces to the least complex possible
optimizer.setReduceColorComplexity(true);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Set candidate compression type
PdfOptimizeSetBitonalCompressions(pOptimizer, eComprAttemptGroup4 | eComprAttemptJBIG2 | 
    eComprAttemptSource);
PdfOptimizeSetContinuousCompressions(pOptimizer, eComprAttemptJPEG | eComprAttemptJPEG2000 | 
    eComprAttemptSource);
PdfOptimizeSetIndexedCompressions(pOptimizer, eComprAttemptFlate | eComprAttemptLZW | 
    eComprAttemptSource);

// Set target resolution for down-sampling
PdfOptimizeSetResolutionDPI(pOptimizer, fResolution);

// Down-sample images if their resolution exceeds 130% of the target resolution
PdfOptimizeSetThresholdDPI(pOptimizer, 1.3f * PdfOptimizeGetResolutionDPI(pOptimizer));

// Apply a 20% higher resolution to bi-tonal images
PdfOptimizeSetBitonalResolutionDPI(pOptimizer, 1.2f * fResolution);
PdfOptimizeSetBitonalThresholdDPI(pOptimizer, 
    1.3f * PdfOptimizeGetBitonalResolutionDPI(pOptimizer));

// Set image quality (for lossy compression types)
PdfOptimizeSetImageQuality(pOptimizer, iQuality);

// Clip invisible parts of images
PdfOptimizeSetClipImages(pOptimizer, 1);

// Convert images' color spaces to the least complex possible
PdfOptimizeSetReduceColorComplexity(pOptimizer, 1);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Optimize PDF document for printing

Avoid printing-related challenges with PDF documents. Reduce the overall file size and merge and subset fonts in order to create smaller spool files and reduce the printer’s memory usage. Convert colors to CMYK. Do not remove or alter any information, such as image resolution, that is needed for a well-defined reproduction.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Choose the optimization profile for printing
    optimizer.Profile = PDFOptimizationProfile.eOptimizationProfilePrint;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. {1} " + 
                "(ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. {1} " + 
                "(ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Choose the optimization profile for printing 
optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfilePrint);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Choose the optimization profile for printing
PdfOptimizeSetProfile(pOptimizer, eOptimizationProfilePrint);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Optimize PDF document for web

Decrease the file size of a PDF to reduce storage requirements on a webserver and download times. This optimization may result in minor visual losses. The output PDF is linearized, which means appropriate viewers can display content before the whole document is downloaded.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Choose the optimization profile for web/e-mail
    optimizer.Profile = PDFOptimizationProfile.eOptimizationProfileWeb;

    // Disable auto-linearization
    optimizer.AutoLinearize = false;

    // Set linearization
    optimizer.Linearize = linearize;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Choose the optimization profile for web/e-mail
optimizer.setProfile(PdfOptimize.OPTIMIZATIONPROFILE.eOptimizationProfileWeb);

// Disable auto-linearization
optimizer.setAutoLinearize(false);

// Set linearization
optimizer.setLinearize(linearize);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Choose the optimization profile for web/e-mail
PdfOptimizeSetProfile(pOptimizer, eOptimizationProfileWeb);

// Disable auto-linearization
PdfOptimizeSetAutoLinearize(pOptimizer, 0);

// Set linearization
PdfOptimizeSetLinearize(pOptimizer, bLinearize);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Removing

Flatten annotations of PDF document

Remove the interactivity of all annotations. For this purpose, annotations and form fields are removed from the internal PDF data structure and an unalterable visual appearance is put at the corresponding location as a replacement.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Flatten and remove annotations and form fields
    optimizer.Strip = PDFStripType.eStripAnnots | PDFStripType.eStripFormsAnnots;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Flatten and remove annotations and form fields 
optimizer.setStrip(NativeLibrary.STRIPTYPE.eStripAnnots | 
    NativeLibrary.STRIPTYPE.eStripFormsAnnots);

// Open input file
if (!optimizer.open(inputPath, "")) 
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption)) 
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Flatten and remove annotations and form fields
PdfOptimizeSetStrip(pOptimizer, eStripAnnots | eStripFormsAnnots);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Remove additional information of PDF

Strip supplementary information of a PDF that is not necessary for many purposes. This includes alternate images, metadata, the output intent, the document structure tree and thumbnails.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Configure which additional information to remove
    optimizer.Strip = PDFStripType.eStripAlternates | PDFStripType.eStripMetadata | 
        PDFStripType.eStripOutputIntents | PDFStripType.eStripStructTree | PDFStripType.eStripThumb;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Configure which additional information to remove
optimizer.setStrip(NativeLibrary.STRIPTYPE.eStripAlternates | 
    NativeLibrary.STRIPTYPE.eStripMetadata | NativeLibrary.STRIPTYPE.eStripOutputIntents | 
    NativeLibrary.STRIPTYPE.eStripStructTree | NativeLibrary.STRIPTYPE.eStripThumb);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Remove additional information at one's option
PdfOptimizeSetStrip(pOptimizer, eStripAlternates | eStripMetadata | eStripOutputIntents | 
    eStripStructTree | eStripThumb);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Features and Configuration

Encrypt document with user or owner password

Create an encrypted document, which requires a password to open. The user is permitted to create annotations when providing the user password. On the other hand, the user is not allowed to print, copy or assemble the document. The full range of permissions is only granted with the owner password.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file, provide user and owner password
    if (!optimizer.SaveAs(outputPath, userPassword, ownerPassword, PDFPermission.ePermAnnotate))
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Open input file
if (!optimizer.open(inputPath, "")) 
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file, provide user and owner password
if (!optimizer.saveAs(outputPath, userPassword, ownerPassword,
    NativeLibrary.PERMISSION.ePermAnnotate)) throw new IOException(String.format("Output file %s " +
        "cannot be created. %s (ErrorCode: 0x%08x).", outputPath, optimizer.getErrorMessage(),
        optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file, provide user and owner password
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(szUserPassword), _T(szOwnerPassword),
    ePermAnnotate))
{
    _tprintf(_T("Output file %s cannot be created.  %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Extract images and font files from PDF

Extract all embedded images and font files and save them as files. Depending on the font type and the compression type of an image, the extracted files have different formats.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Set extract fonts and images
    optimizer.ExtractFonts = true;
    optimizer.ExtractImages = true;

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Extract fonts and images
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Fonts and images could not be extracted. " +
            "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Set extract fonts and images
optimizer.setExtractFonts(true);
optimizer.setExtractImages(true);

// Open input file
if (!optimizer.open(inputPath, "")) 
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Extract fonts and images
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption)) 
    throw new IOException(String.format("Fonts and images could not be extracted. %s " +
        "(ErrorCode: 0x%08x).", outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Set extract fonts and images
PdfOptimizeSetExtractFonts(pOptimizer, 1);
PdfOptimizeSetExtractImages(pOptimizer, 1);

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Extract fonts and images 
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Fonts and images cannot be extracted. %s (ErrorCode: 0x%08x).\n"), 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath, 
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Set document info of a PDF document

Set the document info metadata of a PDF document, such as title, author, subject, creator and producer.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Set author, title, subject, creator and producer of PDF
    optimizer.SetInfoEntry("Author", author);
    optimizer.SetInfoEntry("Title", title);
    optimizer.SetInfoEntry("Subject", subject);
    optimizer.SetInfoEntry("Creator", creator);
    optimizer.SetInfoEntry("Producer", producer);

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Set author, title, creator and producer of PDF 
optimizer.setInfoEntry("Author", author);
optimizer.setInfoEntry("Title", title);
optimizer.setInfoEntry("Subject", subject);
optimizer.setInfoEntry("Creator", creator);
optimizer.setInfoEntry("Producer", producer);

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Set author, title, subject, creator and producer of PDF
PdfOptimizeSetInfoEntry(pOptimizer, _T("Author"), _T(szAuthor));
PdfOptimizeSetInfoEntry(pOptimizer, _T("Title"), _T(szTitle));
PdfOptimizeSetInfoEntry(pOptimizer, _T("Subject"), _T(szSubject));
PdfOptimizeSetInfoEntry(pOptimizer, _T("Creator"), _T(szCreator));
PdfOptimizeSetInfoEntry(pOptimizer, _T("Producer"), _T(szProducer));

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    printf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

Upgrade the PDF version

Set the minimum PDF version for the output document. Supported values range from '1.1' to '1.7'. If the PDF version of the input document is higher than the minimum, the version of the input document is adopted in the output document.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Set minimum version of output file
    if (!optimizer.SetVersion(version))
        throw new Exception(String.Format("The version {0} of the output file {1} cannot be set. " +
            "{2} (ErrorCode: 0x{3:x}).", version, outputPath, optimizer.ErrorMessage, 
            optimizer.ErrorCode));

    // Open input file
    if (!optimizer.Open(inputPath, ""))
        throw new Exception(String.Format("Input file {0} cannot be opened. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file
    if (!optimizer.SaveAs(outputPath, "", "", PDFPermission.ePermNoEncryption))
        throw new Exception(String.Format("Output file {0} cannot be created. " + 
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " + 
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Set minimum version of output file 
if (!optimizer.setVersion(version))
    throw new IOException(String.format("The version %s of the output file %s cannot be set. " + 
        "%s (ErrorCode: 0x%08x).", version, outputPath, optimizer.getErrorMessage(), 
        optimizer.getErrorCode()));

// Open input file
if (!optimizer.open(inputPath, ""))
    throw new IOException(String.format("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file 
if (!optimizer.saveAs(outputPath, "", "", NativeLibrary.PERMISSION.ePermNoEncryption))
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Set minimum version 
if (!PdfOptimizeSetVersion(pOptimizer, szVersion))
{
    _tprintf(_T("The version %s of the output file %s cannot be set. %s (ErrorCode: 0x%08x).\n"),
        szVersion, szOutputPath, PdfOptimizeGetErrorMessage(pOptimizer),
        PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Open input file
if (!PdfOptimizeOpen(pOptimizer, szInputPath, _T("")))
{
    _tprintf(_T("Input file %s cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file
if (!PdfOptimizeSaveAs(pOptimizer, szOutputPath, _T(""), _T(""), ePermNoEncryption))
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

In Memory

Optimize PDF in-memory

Read a PDF from a byte stream, optimize it and return the result as byte stream. For demonstration purpose, the PDF byte stream is created from file and the resulting byte stream is written back to a file.

C# sample:
// Create the optimizer
using (Optimizer optimizer = new Optimizer())
{
    // Open input buffer in-memory
    if (!optimizer.OpenMem(inputBuffer, ""))
        throw new Exception(String.Format("Input buffer cannot be opened in-memory. " +
            "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));

    // Save output file in-memory
    if (!optimizer.SaveInMemory())
        throw new Exception(String.Format("Output document cannot be saved in-memory. " +
            "{0} (ErrorCode: 0x{1:x}).", optimizer.ErrorMessage, optimizer.ErrorCode));

    // Close input file
    if (!optimizer.Close())
        throw new Exception(String.Format("Input file {0} cannot be closed. " +
            "{1} (ErrorCode: 0x{2:x}).", inputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Get PDF as buffer
    byte[] outputBuffer = optimizer.GetPdf();
    if (outputBuffer == null)
        throw new Exception(String.Format("Output file {0} cannot be created. " +
            "{1} (ErrorCode: 0x{2:x}).", outputPath, optimizer.ErrorMessage, optimizer.ErrorCode));

    // Write bytes to output file.
    File.WriteAllBytes(outputPath, outputBuffer);
}
Java sample:
// Create the optimizer
optimizer = new PdfOptimize();

// Open input buffer in-memory
if (!optimizer.openMem(inputBuffer, ""))
    throw new IOException(String.format("Input buffer cannot be opened in-memory. " +
        "%s (ErrorCode: 0x%08x).", optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Save output file in-memory
if (!optimizer.saveInMemory())
    throw new IOException(String.format("Output document cannot be saved in-memory. " + 
        "%s (ErrorCode: 0x%08x).", optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Close input file
if (!optimizer.close())
    throw new IOException(String.format("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).",
        inputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Get PDF as buffer
byte[] outputBuffer = optimizer.getPdf();
if (outputBuffer == null)
    throw new IOException(String.format("Output file %s cannot be created. %s (ErrorCode: 0x%08x).",
        outputPath, optimizer.getErrorMessage(), optimizer.getErrorCode()));

// Write bytes to output file
Files.write(Paths.get(outputPath), outputBuffer, StandardOpenOption.CREATE_NEW);
C sample:
// Create the optimizer
pOptimizer = PdfOptimizeCreateObject();

// Open input PDF from memory buffer
if (!PdfOptimizeOpenMem(pOptimizer, pInputBuffer, nLength, _T("")))
{
    _tprintf(_T("Input buffer cannot be opened in-memory. %s (ErrorCode: 0x%08x).\n"),
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Save output file in-memory
if (!PdfOptimizeSaveInMemory(pOptimizer))
{
    _tprintf(_T("Output document cannot be saved in-memory. %s (ErrorCode: 0x%08x).\n"),
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Close input file
if (!PdfOptimizeClose(pOptimizer))
{
    _tprintf(_T("Input file %s cannot be closed. %s (ErrorCode: 0x%08x).\n"), szInputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Create output file
if ((pOutputFile = _tfopen(szOutputPath, _T("wb"))) == NULL)
{
    _tprintf(_T("Output file %s cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutputPath,
        PdfOptimizeGetErrorMessage(pOptimizer), PdfOptimizeGetErrorCode(pOptimizer));
    iReturnValue = 1;
    goto cleanup;
}

// Get the PDF as a memory buffer
pOutputBuffer = PdfOptimizeGetPdf(pOptimizer);
// Write bytes to output file
if (fwrite(pOutputBuffer->m_pData, 1, pOutputBuffer->m_nLength, pOutputFile) != pOutputBuffer->m_nLength)
{
    _tprintf(_T("Failed to write data to output file %s.\n"), szOutputPath);
    iReturnValue = 1;
    goto cleanup;
}
fclose(pOutputFile);
pOutputFile = NULL;