> 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/core/optimization/optimize.md).

# Optimize and Compress PDFs on Server/Desktop

Learn how to optimize and compress PDF files with the latest image compression technology. Reduce file size by removing redundant information and compressing data streams. Get full code samples for us

To optimize a PDF with default settings is quick once you are set up.

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

```csharp
PDFDoc doc = new PDFDoc(filename);
Optimizer.Optimize(doc);
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);
Optimizer::Optimize(doc);
```

{% endcode %}
{% endtab %}

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

```go
doc := NewPDFDoc(filename)
OptimizerOptimize(doc)
```

{% endcode %}
{% endtab %}

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

```java
PDFDoc doc = new PDFDoc(filename);
Optimizer.optimize(doc);
```

{% endcode %}
{% endtab %}

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

```js
async function main() {
	const doc = await PDFNet.PDFDoc.createFromFilePath(filename);
	await PDFNet.Optimizer.optimize(doc);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc(filename)
Optimizer.optimize(doc)
```

{% endcode %}
{% endtab %}

{% 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 %}

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

```php
$doc = new PDFDoc($filename);
Optimizer::Optimize($doc);
```

{% endcode %}
{% endtab %}

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

```python
doc = PDFDoc(filename)
Optimizer.Optimize(doc)
```

{% endcode %}
{% endtab %}

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

```ruby
doc = PDFDoc.new(filename)
Optimizer.Optimize(doc)
```

{% endcode %}
{% endtab %}

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

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
Optimizer.Optimize(doc)
```

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

[Compress & optimize PDF files - Full Sample](/core/get-started/samples/optimizertest.md) 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. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.

## 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="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc pdf_doc = new PDFDoc(filename);
SDFDoc cos_doc = pdf_doc.GetSDFDoc();
int num_objs = cos_doc.XRefSize();
for (int i=1; i<num_objs; ++i)
{
	Obj obj = cos_doc.GetObj(i);
	if (obj==null || obj.IsFree() || !obj.IsStream())
		continue;

	// Process only images
	DictIterator itr = obj.Find("Subtype");
	if (!itr.HasNext() || itr.Value().GetName() != "Image")
		continue;

	pdftron.PDF.Image input_image = new pdftron.PDF.Image(obj);

	// Process only gray-scale images
	if (input_image.GetComponentNum() != 1) 
		continue; 
	
	int bpc = input_image.GetBitsPerComponent();
	if (bpc != 1) // Recompress 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;
	
	FilterReader reader = new FilterReader(obj.GetDecodedStream());

	ObjSet hint_set = new ObjSet();
	Obj hint = hint_set.CreateArray();
	hint.PushBackName("JBIG2");
	hint.PushBackName("Lossless");
	hint.PushBackName("Threshold");
	hint.PushBackNumber(0.4);
	hint.PushBackName("SharePages");
	hint.PushBackNumber(10000);

	pdftron.PDF.Image new_image = pdftron.PDF.Image.Create(
		cos_doc,
		reader, 							
		input_image.GetImageWidth(),
		input_image.GetImageHeight(),
		1,
		ColorSpace.CreateDeviceGray(),
		hint  // A hint to image encoder to use JBIG2 compression
	);
	cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum());
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);
SDFDoc& cos_doc = pdf_doc.GetSDFDoc();
int num_objs = cos_doc.XRefSize();
for(int i=1; i<num_objs; ++i) 
{
	Obj obj = cos_doc.GetObj(i);
	if(!obj || obj.IsFree() || !obj.IsStream())
		continue; 

	// Process only images
	DictIterator itr = obj.Find("Subtype");
	if(!itr.HasNext() || strcmp(itr.Value().GetName(), "Image"))
		continue;
	
	Image input_image(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() && !strcmp(itr.Value().GetName(), "JBIG2Decode")) 
		continue; 

	Filter filter=obj.GetDecodedStream();
	FilterReader reader(filter);

	ObjSet hint_set; 	// A hint to image encoder to use JBIG2 compression
	Obj hint=hint_set.CreateArray();

	hint.PushBackName("JBIG2");
	hint.PushBackName("Lossless");

	Image new_image = Image::Create(
		cos_doc, 
		reader, 
		input_image.GetImageWidth(), 
		input_image.GetImageHeight(), 
		1, 
		ColorSpace::CreateDeviceGray(), 
		hint
	);
	cos_doc.Swap(i, new_img.GetSDFDoc().GetObjNum());
}
```

{% endcode %}
{% endtab %}

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

```go
cosDoc := pdfDoc.GetSDFDoc()
numObjs := cosDoc.XRefSize()

i := uint(1)
for i < numObjs{
	obj := cosDoc.GetObj(i)
	if obj != nil && ! obj.IsFree() && obj.IsStream(){
		// Process only images
		itr := obj.Find("Subtype")
		//if not itr.HasNext() or not itr.Value().GetName() == "Image":
		if !itr.HasNext() || !(itr.Value().GetName() == "Image"){
			i = i + 1
			continue
		}
		inputImage := NewImage(obj)
		// Process only gray-scale images
		if inputImage.GetComponentNum() != 1{
			i = i + 1
			continue
		}
		// Skip images that are already compressed using JBIG2
		itr = obj.Find("Filter")
		if (itr.HasNext() && itr.Value().IsName() && itr.Value().GetName() == "JBIG2Decode"){
			i = i + 1
			continue
		}

		filter := obj.GetDecodedStream()
		reader := NewFilterReader(filter)
		
		hintSet := NewObjSet()     // hint to image encoder to use JBIG2 compression
		hint := hintSet.CreateArray()
		
		hint.PushBackName("JBIG2")
		hint.PushBackName("Lossless")
		
		newImage := (ImageCreate(cosDoc, reader, 
											inputImage.GetImageWidth(), 
											inputImage.GetImageHeight(), 
											1, 
											ColorSpaceCreateDeviceGray(), 
											hint))
		
		newImgObj := newImage.GetSDFObj()
		itr = obj.Find("Decode")
		
		if itr.HasNext(){
			newImgObj.Put("Decode", itr.Value())
		}
		itr = obj.Find("ImageMask")
		if itr.HasNext(){
			newImgObj.Put("ImageMask", itr.Value())
		}
		itr = obj.Find("Mask")
		if itr.HasNext(){
			newImgObj.Put("Mask", itr.Value())
		}

		cosDoc.Swap(i, newImgObj.GetObjNum())
	}
	i = i + 1
}
```

{% endcode %}
{% endtab %}

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

```java
PDFDoc doc = new PDFDoc(filename);
SDFDoc cos_doc = pdf_doc.getSDFDoc();
int num_objs = (int) cos_doc.xRefSize();
for (int i = 1; i < num_objs; ++i) {
	Obj obj = cos_doc.getObj(i);
	if (obj == null || obj.isFree() || !obj.isStream())
		continue;

	// Process only images
	DictIterator itr = obj.find("Subtype");
	if (!itr.hasNext() || !itr.value().getName().equals("Image"))
		continue;

	Image input_image = new Image(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().equals("JBIG2Decode")) 
		continue;

	Filter filter = obj.getDecodedStream();
	FilterReader reader = new FilterReader(filter);

	ObjSet hint_set = new ObjSet();
	Obj hint = hint_set.createArray(); // A hint to image encoder to use JBIG2 compression
	hint.pushBackName("JBIG2");
	hint.pushBackName("Lossless");

	Image new_image = Image.create(
		cos_doc, reader,
		input_image.getImageWidth(),
		input_image.getImageHeight(), 
		1, 
		ColorSpace.createDeviceGray(), 
		hint
	);
	cos_doc.swap(i, new_img.getSDFDoc().getObjNum());
}
```

{% endcode %}
{% endtab %}

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

```js
async function main() {
	const pdf_doc = await PDFNet.PDFDoc.createFromURL(filename);
	pdf_doc.initSecurityHandler();

	const cos_doc = await pdf_doc.getSDFDoc();
	const num_objs = await cos_doc.xRefSize();
	for (let i = 1; i < num_objs; ++i) {
		const obj = await cos_doc.getObj(i);
		if (obj && !(await obj.isFree()) && await obj.isStream()) {
			// Process only images
			var itr = await obj.find("Subtype");
			if (!(await itr.hasNext()) || await (await itr.value()).getName() !== "Image")
				continue;
			const input_image = await PDFNet.Image.createFromObj(obj);
			// Process only gray-scale images
			if (await input_image.getComponentNum() != 1)
				continue;
			if (await input_image.getBitsPerComponent() != 1) // Recompress only 1 BPC images
				continue;

			// Skip images that are already compressed using JBIG2
			itr = await obj.find("Filter");
			if (await itr.hasNext()) {
				const value = await itr.value();
				if (await value.isName() && await value.getName() === "JBIG2Decode") continue;
			}

			const filter = await obj.getDecodedStream();
			const reader = await PDFNet.FilterReader.create(filter);

			const hint_set = await PDFNet.ObjSet.create();
			const hint = await hint_set.createArray();

			hint.pushBackName("JBIG2");
			hint.pushBackName("Lossless");

			const new_image = await PDFNet.Image.createFromStream(cos_doc, reader, await input_image.getImageWidth(),
				await input_image.getImageHeight(), 1, await PDFNet.ColorSpace.createDeviceGray(), hint);

			const new_img_obj = await new_image.getSDFObj();
			itr = await obj.find("Decode");
			if (await itr.hasNext())
				new_img_obj.put("Decode", await itr.value());
			itr = await obj.find("ImageMask");
			if (await itr.hasNext())
				new_img_obj.put("ImageMask", await itr.value());
			itr = await obj.find("Mask");
			if (await itr.hasNext())
				new_img_obj.put("Mask", await itr.value());

			await cos_doc.swap(i, await new_img_obj.getObjNum());
		}
	}
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

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

```kotlin
val doc = PDFDoc(filename)
val cos_doc = pdf_doc.sdfDoc
val num_objs = cos_doc.xRefSize().toInt()
for (i in 1 until num_objs) {
	val obj = cos_doc.getObj(i.toLong())
	if (obj == null || obj.isFree || !obj.isStream) 
		continue;

	// Process only images
	var itr = obj.find("Subtype")
	if (!itr.hasNext() || itr.value().name != "Image")
		continue

	val input_image = Image(obj)

	// Process only gray-scale images
	if (input_image.componentNum != 1)
		continue

	val bpc = input_image.bitsPerComponent
	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().name != "JBIG2Decode")
		continue

	val filter = obj.decodedStream
	val reader = FilterReader(filter)

	val hint_set = ObjSet()
	val hint = hint_set.createArray() // A hint to image encoder to use JBIG2 compression
	hint.pushBackName("JBIG2")
	hint.pushBackName("Lossless")

	val new_image = Image.create(
		cos_doc, reader,
		input_image.imageWidth,
		input_image.imageHeight, 
		1, 
		ColorSpace.createDeviceGray(), 
		hint
	)
	cos_doc.swap(i.toLong(), new_img.sdfDoc.objNum)
}
```

{% endcode %}
{% endtab %}

{% 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 %}

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

```php
$doc = new PDFDoc($filename);
$cos_doc = $pdf_doc->GetSDFDoc();
$num_objs = $cos_doc->XRefSize();
for($i = 1; $i < $num_objs; ++$i) 
{
	$obj = $cos_doc->GetObj($i);
	if(!$obj || $obj->IsFree() || !$obj->IsStream())
		continue; 

	// Process only images
	$itr = $obj->Find("Subtype");
	if(!$itr->HasNext() || $itr->Value()->GetName() != "Image")
		continue;
	
	$input_image = new Image($obj);

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

	$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() == "JBIG2Decode") continue; 

	$filter=$obj->GetDecodedStream();
	$reader = new FilterReader($filter);

	$hint_set = new ObjSet(); 	// A hint to image encoder to use JBIG2 compression
	$hint=$hint_set->CreateArray();
	
	$hint->PushBackName("JBIG2");
	$hint->PushBackName("Lossless");

	$new_image = Image::Create(
		$cos_doc, 
		$reader, 
		$input_image->GetImageWidth(), 
		$input_image->GetImageHeight(), 
		1, 
		ColorSpace::CreateDeviceGray(), 
		$hint
	);
	$cos_doc->Swap($i, $new_img->GetSDFDoc()->GetObjNum());
}
```

{% endcode %}
{% endtab %}

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

```python
doc = PDFDoc(filename)
cos_doc = pdf_doc.GetSDFDoc()
num_objs = cos_doc.XRefSize()

i = 1
while i < num_objs:
	obj = cos_doc.GetObj(i)
	if obj is None or obj.IsFree() or not obj.IsStream():
		i = i + 1
		continue

	# Process only images
	itr = obj.Find("Subtype")
	if not itr.HasNext() or not itr.Value().GetName() == "Image":
		i = i + 1
		continue
		
	input_image = Image(obj)

	# Process only gray-scale images
	if input_image.GetComponentNum() != 1:
		i = i + 1
		continue
		
	# Skip images that are already compressed using JBIG2
	itr = obj.Find("Filter")
	if (itr.HasNext() and itr.Value().IsName() and itr.Value().GetName() == "JBIG2Decode"):
		i = i + 1
		continue
		
	filter = obj.GetDecodedStream()
	reader = FilterReader(filter)
	
	hint_set = ObjSet()     # hint to image encoder to use JBIG2 compression
	hint = hint_set.CreateArray()
	
	hint.PushBackName("JBIG2")
	hint.PushBackName("Lossless")
	
	new_image = Image.Create(
		cos_doc, 
		reader, 
		input_image.GetImageWidth(), 
		input_image.GetImageHeight(), 
		1, 
		ColorSpace.CreateDeviceGray(), 
		hint
	)
	cos_doc.Swap(i, new_img.GetSDFDoc().GetObjNum())
	i = i + 1
```

{% endcode %}
{% endtab %}

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

```ruby
doc = PDFDoc.new(filename)
cos_doc = pdf_doc.GetSDFDoc
num_objs = cos_doc.XRefSize
i = 1
while i < num_objs do
	obj = cos_doc.GetObj(i)
	if obj.nil? or obj.IsFree or !obj.IsStream
		i = i + 1
		next
	end

	# Process only images
	itr = obj.Find("Subtype")
	if !itr.HasNext or !itr.Value.GetName == "Image"
		i = i + 1
		next
	end
	
	input_image = Image.new(obj)

	# Process only gray-scale images
	if input_image.GetComponentNum != 1
		i = i + 1
		next
	end			

	# Skip images that are already compressed using JBIG2
	itr = obj.Find("Filter")
	if itr.HasNext and itr.Value.IsName and itr.Value.GetName == "JBIG2Decode"
		i = i + 1
		next
	end
	
	filter = obj.GetDecodedStream
	reader = FilterReader.new(filter)
	
	hint_set = ObjSet.new	 # hint to image encoder to use JBIG2 compression
	hint = hint_set.CreateArray
	
	hint.PushBackName("JBIG2")
	hint.PushBackName("Lossless")
	
	new_image = Image.Create(
		cos_doc, 
		reader, 
		input_image.GetImageWidth, 
		input_image.GetImageHeight, 
		1, 
		ColorSpace.CreateDeviceGray, 
		hint
	)	
	cos_doc.Swap(i, new_img.GetSDFDoc.GetObjNum)
	i = i + 1
end
```

{% endcode %}
{% endtab %}

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

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
Dim cos_doc As SDFDoc = pdfdoc.GetSDFDoc()
Dim num_objs As Integer = cos_doc.XRefSize()

For i As Integer = 1 To num_objs - 1
	Dim obj As Obj = cos_doc.GetObj(i)
	If Not (obj Is Nothing Or obj.IsFree()) Then
		' Process only images
		If obj.IsStream() Then
			Dim itr As DictIterator = obj.Find("Subtype")
			If itr.HasNext() Then
				If itr.Value().GetName() = "Image" Then
					Dim input_image As pdftron.PDF.Image = New pdftron.PDF.Image(obj)
					Dim new_image As pdftron.PDF.Image = Nothing

					' Process only gray-scale images
					If input_image.GetComponentNum() = 1 Then
						Dim bpc As Integer = input_image.GetBitsPerComponent()
						If bpc = 1 Then
							Dim reader As FilterReader = New FilterReader(obj.GetDecodedStream())

							Dim hint_set As ObjSet = New ObjSet
							Dim hint As Obj = hint_set.CreateArray()
							hint.PushBackName("JBIG2")
							
							' hint.PushBackName("Lossless")
							hint.PushBackName("Threshold")
							hint.PushBackNumber(0.4)
							hint.PushBackName("SharePages")
							hint.PushBackNumber(10000)

							new_image = pdftron.PDF.Image.Create(
								cos_doc, 
								reader, 
								input_image.GetImageWidth(), 
								input_image.GetImageHeight(), 
								1, 
								ColorSpace.CreateDeviceGray(), 
								hint
							)
							cos_doc.Swap(i, new_image.GetSDFObj().GetObjNum())
						End If
					End If
				End If
			End If
		End If
	End If
Next
```

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

[PDF image JBIG2 compression - Full Sample](/core/get-started/samples/jbig2test.md) Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression. Samples available in Python, C# (.Net), C++, Go, Java, Node.js (JavaScript), PHP, Ruby, VB.


---

# 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/core/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.
