Skip to content
Guides

Compress & Optimize a PDF File

Control the compression accuracy of images and observe the compression progress in real-time by setting the CPDFDocumentOptimizeOption parameter.

Below is an example code for configuring the compression and optimization of PDF files:

swift
 // Compress & Optimize a PDF File
document?.writeOptimize(to: url, withOptions: [CPDFDocumentOptimizeOption.imageQualityOption : 60], progressHandler: { pageIndex, pageCount in
   // Monitor the current compression progress of the page index value and the total number of pages in the document             
}, cancelHandler: {
  // Whether to cancel the current compression
    return NO
}, completionHandler: { isSuccess in
	// Compression success
   }
})
objective-c
// Compress & Optimize a PDF File
[document writeOptimizeToURL:[NSURL fileURLWithPath:targetPath]
                           withOptions:@{CPDFDocumentImageQualityOption:compressType}
                       progressHandler:^(float cPageIndex, float totalPageIndex) {
      // Monitor the current compression progress of the page index value and the total number of pages in the document  
  }
                         cancelHandler:^BOOL{
      // Whether to cancel the current compression
    	return NO;
  }
                     completionHandler:^(BOOL finished) {
        
      // Compression success
  }];