PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
With CPDFOptimizeOption, you can convert the color space of a document (RGB to CMYK or CMYK to RGB) during optimization, and use optimizeAndSaveAsCopyWithPath:stepsMicroseconds:saveMicroseconds: to optimize and save the document in a single call while getting the time spent in each phase.
| Property/Method | Description |
|---|---|
initWithDocument: | Creates an option object for the document to optimize. The object comes with default image compression parameters (col_imageOption) and optimization flags (optimizeflag), which you can change as needed. |
iccProfileData | The ICC profile data used for the color space conversion. nil (the default) or empty data disables the conversion. |
rgbToCMYK | The conversion direction. YES (the default) converts RGB to CMYK, NO converts CMYK to RGB. Only takes effect when iccProfileData is not empty. |
optimizeAndSaveAsCopyWithPath:stepsMicroseconds:saveMicroseconds: | Optimizes the document and saves the result to the specified path. Returns YES on success. stepsMicroseconds and saveMicroseconds return the time (in microseconds) spent in the optimization phase and the save phase. Pass NULL (nil in Swift) if not needed. |
This example shows how to convert RGB to CMYK while optimizing a document and save the result:
guard let option = CPDFOptimizeOption(document: document) else { return }
// Set the ICC profile data to enable color space conversion
option.iccProfileData = try? Data(contentsOf: URL(fileURLWithPath: iccPath))
// true (default): RGB to CMYK; false: CMYK to RGB
option.rgbToCMYK = true
var stepsMicroseconds: Int64 = 0
var saveMicroseconds: Int64 = 0
// Call it directly; do not call startCompress first
let success = option.optimizeAndSaveAsCopy(withPath: savePath, stepsMicroseconds: &stepsMicroseconds, saveMicroseconds: &saveMicroseconds)
print("Success: \(success), optimize: \(stepsMicroseconds) us, save: \(saveMicroseconds) us")CPDFOptimizeOption *option = [[CPDFOptimizeOption alloc] initWithDocument:document];
// Set the ICC profile data to enable color space conversion
option.iccProfileData = [NSData dataWithContentsOfFile:iccPath];
// YES (default): RGB to CMYK; NO: CMYK to RGB
option.rgbToCMYK = YES;
int64_t stepsMicroseconds = 0;
int64_t saveMicroseconds = 0;
// Call it directly; do not call startCompress first
BOOL success = [option optimizeAndSaveAsCopyWithPath:savePath
stepsMicroseconds:&stepsMicroseconds
saveMicroseconds:&saveMicroseconds];
NSLog(@"Success: %d, optimize: %lld us, save: %lld us", success, stepsMicroseconds, saveMicroseconds);Note: optimizeAndSaveAsCopyWithPath:stepsMicroseconds:saveMicroseconds: performs the optimization itself. Do not call startCompress before it, or the document is optimized twice and images are recompressed a second time, losing quality. It is also not a variant of saveAsCopyWithPath:, which only saves a document that startCompress has already optimized. The method runs synchronously and can take a long time for large documents, so we recommend calling it on a background thread and saving the result to a path different from the source file.