Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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

  final fac = CkFileAccess();
  final json = CkJsonObject();
  final jsonOut = CkJsonObject();

  // generate: pack the objects (the default).
  final pdf1 = CkPdf();
  try {
    pdf1.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'generate');
  try {
    pdf1.optimize('optimize', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    pdf1.writeFile('qa_output/grand_canyon_objstm_generate.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('generate: ${fac.fileSize('qa_output/grand_canyon_objstm_generate.pdf')} bytes, objects packed: ${jsonOut.intOf('actions.objectStreams.count')}');

  // disable: every object on its own, with a classic cross-reference table.
  final pdf2 = CkPdf();
  try {
    pdf2.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'disable');
  try {
    pdf2.optimize('optimize', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    pdf2.writeFile('qa_output/grand_canyon_objstm_disable.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('disable:  ${fac.fileSize('qa_output/grand_canyon_objstm_disable.pdf')} bytes');

  // preserve: leave the file's own structure alone.  Use it when the output must stay readable by
  // whatever reads the input today.
  final pdf3 = CkPdf();
  try {
    pdf3.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'preserve');
  try {
    pdf3.optimize('optimize', json, jsonOut);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    pdf3.writeFile('qa_output/grand_canyon_objstm_preserve.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  print('preserve: ${fac.fileSize('qa_output/grand_canyon_objstm_preserve.pdf')} bytes');
}