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

React Native
import { FileAccess, JsonObject, Pdf } from '@chilkat/react-native'

function chilkatExample() {
  // 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

  const fac = new FileAccess();
  const json = new JsonObject();
  const jsonOut = new JsonObject();

  // generate: pack the objects (the default).
  const pdf1 = new Pdf();
  try {
    pdf1.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } catch {
    console.log(pdf1.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'generate');
  try {
    pdf1.optimize('optimize', json, jsonOut);
  } catch {
    console.log(pdf1.lastErrorText);
    return;
  }

  try {
    pdf1.writeFile('qa_output/grand_canyon_objstm_generate.pdf');
  } catch {
    console.log(pdf1.lastErrorText);
    return;
  }

  console.log(`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.
  const pdf2 = new Pdf();
  try {
    pdf2.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } catch {
    console.log(pdf2.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'disable');
  try {
    pdf2.optimize('optimize', json, jsonOut);
  } catch {
    console.log(pdf2.lastErrorText);
    return;
  }

  try {
    pdf2.writeFile('qa_output/grand_canyon_objstm_disable.pdf');
  } catch {
    console.log(pdf2.lastErrorText);
    return;
  }

  console.log(`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.
  const pdf3 = new Pdf();
  try {
    pdf3.loadFile('qa_data/pdf/grand_canyon_hiking_guide.pdf');
  } catch {
    console.log(pdf3.lastErrorText);
    return;
  }

  json.updateString('objectStreams', 'preserve');
  try {
    pdf3.optimize('optimize', json, jsonOut);
  } catch {
    console.log(pdf3.lastErrorText);
    return;
  }

  try {
    pdf3.writeFile('qa_output/grand_canyon_objstm_preserve.pdf');
  } catch {
    console.log(pdf3.lastErrorText);
    return;
  }

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