Some test text!

Search
Hamburger Icon

Ruby / Guides / Optimize

Optimize & compress PDFs in Ruby

To optimize a PDF with default settings.

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

Compress & optimize PDF files
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.

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

PDF image JBIG2 compression
Full sample code which illustrates how to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression.

Get the answers you need: Chat with us