Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.7.0+

PDF Object Streams: generate, preserve or disable

See more PDF Optimization Examples

Demonstrates the objectStreams option. An object stream packs many small objects (dictionaries, not stream data) into one compressed stream, which is usually a worthwhile saving in a document with many pages or tags. "generate" (the default) packs them and writes a cross-reference stream; "preserve" keeps the file as it is -- packed if it already was, not packed if it was not; "disable" writes every object on its own with a classic cross-reference table, which is what a PDF 1.4 reader needs. Object streams require PDF 1.5 and are skipped for PDF/A-1.

Chilkat Objective-C Downloads

Objective-C
#import <CkoFileAccess.h>
#import <CkoJsonObject.h>
#import <CkoPdf.h>

BOOL success = NO;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  The PDF used in this example is available at grand_canyon_hiking_guide.pdf

CkoFileAccess *fac = [[CkoFileAccess alloc] init];
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];

//  generate: pack the objects (the default).
CkoPdf *pdf1 = [[CkoPdf alloc] init];
success = [pdf1 LoadFile: @"qa_data/pdf/grand_canyon_hiking_guide.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf1.LastErrorText);
    return;
}

[json UpdateString: @"objectStreams" value: @"generate"];
success = [pdf1 Optimize: @"optimize" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf1.LastErrorText);
    return;
}

success = [pdf1 WriteFile: @"qa_output/grand_canyon_objstm_generate.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf1.LastErrorText);
    return;
}

NSLog(@"%@%d%@%d",@"generate: ",[[fac FileSize: @"qa_output/grand_canyon_objstm_generate.pdf"] intValue],@" bytes, objects packed: ",[[jsonOut IntOf: @"actions.objectStreams.count"] intValue]);

//  disable: every object on its own, with a classic cross-reference table.
CkoPdf *pdf2 = [[CkoPdf alloc] init];
success = [pdf2 LoadFile: @"qa_data/pdf/grand_canyon_hiking_guide.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf2.LastErrorText);
    return;
}

[json UpdateString: @"objectStreams" value: @"disable"];
success = [pdf2 Optimize: @"optimize" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf2.LastErrorText);
    return;
}

success = [pdf2 WriteFile: @"qa_output/grand_canyon_objstm_disable.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf2.LastErrorText);
    return;
}

NSLog(@"%@%d%@",@"disable:  ",[[fac FileSize: @"qa_output/grand_canyon_objstm_disable.pdf"] intValue],@" bytes");

//  preserve: leave the file's own structure alone.  Use it when the output must stay readable by
//  whatever reads the input today.
CkoPdf *pdf3 = [[CkoPdf alloc] init];
success = [pdf3 LoadFile: @"qa_data/pdf/grand_canyon_hiking_guide.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf3.LastErrorText);
    return;
}

[json UpdateString: @"objectStreams" value: @"preserve"];
success = [pdf3 Optimize: @"optimize" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
    NSLog(@"%@",pdf3.LastErrorText);
    return;
}

success = [pdf3 WriteFile: @"qa_output/grand_canyon_objstm_preserve.pdf"];
if (success == NO) {
    NSLog(@"%@",pdf3.LastErrorText);
    return;
}

NSLog(@"%@%d%@",@"preserve: ",[[fac FileSize: @"qa_output/grand_canyon_objstm_preserve.pdf"] intValue],@" bytes");