> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/optimization/optimize.md).

# Optimize & compress PDFs on iOS

Learn how to optimize and compress PDF files using the latest technology. Reduce file size by removing redundant information and compressing data streams with pdftron.PDF.Optimizer. Explore advanced c

To optimize a PDF with default settings.

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
[PTOptimizer Optimize: doc settings: [[PTOptimizerSettings alloc] init]];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
PTOptimizer.optimize(doc, settings: PTOptimizerSettings())
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Compress & optimize PDF files](/ios/get-started/samples.md#optimizer) Full code sample which shows how to use 'pdftron.PDF.Optimizer' to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology.

## About optimize and compress

Compression as a subset of optimization represents encoding specific data using fewer bits than the original content by reducing the size of the data. This is distinct from optimize (which modifies all images) because you have the ability to choose individual images and to selectively choose the compression type for each.

Apryse SDK supports all basic and advanced compression filters allowed in PDF including:

* JPEG2000
* JBIG2
* CCITT Fax
* Flate/PNG
* JPEG/DCT
* Crypt Filters

## Compress images in a PDF document

To compress images using JBIG2 compression inside a PDF.

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTSDFDoc *cos_doc = [pdf_doc GetSDFDoc];
int num_objs = [cos_doc XRefSize];
for(int i=1; i<num_objs; ++i) 
{
	PTObj * obj = [cos_doc GetObj: i];
	if(!obj || [obj IsFree] || ![obj IsStream]) 
		continue;
	
	// Process only images
	PTDictIterator *itr = [obj Find: @"Subtype"];
	if(![itr HasNext] || !([[[itr Value] GetName] isEqualToString:@"Image"]))
		continue;
	
	PTImage *input_image = [[PTImage alloc] initWithImage_xobject: obj];

	// Process only gray-scale images
	if([input_image GetComponentNum] != 1)
		continue;

	int bpc = [input_image GetBitsPerComponent];
	if(bpc != 1)	// Recompress only 1 BPC images
		continue;

	// Skip images that are already compressed using JBIG2
	itr = [obj Find: @"Filter"];
	if ([itr HasNext] && [[itr Value] IsName] && [[[itr Value] GetName] isEqualToString:@"JBIG2Decode"]) 
		continue;

	PTFilter *filter=[obj GetDecodedStream];
	PTFilterReader *reader = [[PTFilterReader alloc] initWithFilter: filter];

	PTObjSet *hint_set = [[PTObjSet alloc] init]; 	// A hint to image encoder to use JBIG2 compression
	PTObj * hint=[hint_set CreateArray];

	[hint PushBackName: @"JBIG2"];
	[hint PushBackName: @"Lossless"];

	PTImage *new_image = [PTImage CreateWithFilterData: cos_doc 
											image_data: reader 
												 width: [input_image GetImageWidth] 
												height: [input_image GetImageHeight] 
												   bpc: 1 
										   color_space: [PTColorSpace CreateDeviceGray] 
										 encoder_hints: hint];
	[cos_doc Swap: i obj_num2: [[pdf_doc GetSDFDoc] GetObjNum]];
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
let cos_doc: PTSDFDoc = pdf_doc.getSDFDoc()
let num_objs = cos_doc.xRefSize()
for i in 1..<num_objs {
	guard let obj: PTObj = cos_doc.getObj(UInt32(i)) else {
		continue
	}
	if obj.isFree() || !obj.isStream() {
		continue
	}

	// Process only images
	var itr: PTDictIterator = obj.find("Subtype")
	if !itr.hasNext() || !(itr.value().getName() == "Image") {
		continue
	}
	
	let input_image: PTImage = PTImage(image_xobject: obj)

	// Process only gray-scale images
	if input_image.getComponentNum() != 1 {
		continue
	}
	let bpc: Int32 = input_image.getBitsPerComponent()
	if bpc != 1 {
		// Recompress only 1 BPC images
		continue
	}
		
	// Skip images that are already compressed using JBIG2
	itr = obj.find("Filter")
	if itr.hasNext() && itr.value().isName() && (itr.value().getName() == "JBIG2Decode") {
		continue
	}
	
	let filter: PTFilter = obj.getDecodedStream()
	let reader = PTFilterReader(filter: filter)
	
	let hint_set: PTObjSet = PTObjSet()   // A hint to image encoder to use JBIG2 compression
	let hint: PTObj = hint_set.createArray()
	
	hint.pushBackName("JBIG2")
	hint.pushBackName("Lossless")
	
	let new_image: PTImage = PTImage.create(
		withFilterData: cos_doc, 
			image_data: reader, 
				 width: input_image.getWidth(), 
				height: input_image.getHeight(), 
				   bpc: 1, 
		   color_space: PTColorSpace.createDeviceGray(), 
		 encoder_hints: hint
	)
	cos_doc.swap(UInt32(i), obj_num2: new_img.getSDFDoc().getNum())
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

[PDF image JBIG2 compression](/ios/get-started/samples.md#jbigtest) Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/ios/optimization/optimize.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
