//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #import #import //--------------------------------------------------------------------------------------- // The following sample illustrates how to reduce PDF file size using the // Optimizer class. // // 'pdftron.PDF.Optimizer' is an optional PDFNet Add-On utility class that can be // used to optimize PDF documents by reducing the file size, removing redundant // information, and compressing data streams using the latest in image compression // technology. PDF Optimizer can compress and shrink PDF file size with the // following operations: // - Remove duplicated fonts, images, ICC profiles, and any other data stream. // - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF. // - Optionally down-sample large images to a given resolution. // - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats. // - Compress uncompressed streams and remove unused PDF objects. //--------------------------------------------------------------------------------------- int main(int argc, char * argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int ret = 0; // The first step in every application using PDFNet is to initialize the // library and set the path to common PDF resources. The library is usually // initialized only once, but calling Initialize() multiple times is also fine. [PDFNet Initialize: 0]; //-------------------------------------------------------------------------------- // Example 1) Simple optimization of a pdf with default settings. // @try { PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"] autorelease]; [doc InitSecurityHandler]; [Optimizer Optimize: doc settings: [[[OptimizerSettings alloc] init] autorelease]]; [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt1.pdf" flags: e_linearized]; } @catch (NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } //-------------------------------------------------------------------------------- // Example 2) Reduce image quality and use jpeg compression for // non monochrome images. @try { PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"] autorelease]; [doc InitSecurityHandler]; ImageSettings *image_settings = [[[ImageSettings alloc] init] autorelease]; // low quality jpeg compression [image_settings SetCompressionMode: e_jpeg]; [image_settings SetQuality: 1]; // Set the output dpi to be standard screen resolution [image_settings SetImageDPI: 144 resampling: 96]; // this option will recompress images not compressed with // jpeg compression and use the result if the new image // is smaller. [image_settings ForceRecompression: YES]; // this option is not commonly used since it can // potentially lead to larger files. It should be enabled // only if the output compression specified should be applied // to every image of a given type regardless of the output image size //image_settings.ForceChanges(true); OptimizerSettings *opt_settings = [[[OptimizerSettings alloc] init] autorelease]; [opt_settings SetColorImageSettings: image_settings]; [opt_settings SetGrayscaleImageSettings: image_settings]; // use the same settings for both color and grayscale images [Optimizer Optimize: doc settings: opt_settings]; [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt2.pdf" flags: e_linearized]; } @catch (NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } //-------------------------------------------------------------------------------- // Example 3) Use monochrome image settings and default settings // for color and grayscale images. @try { PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"] autorelease]; [doc InitSecurityHandler]; MonoImageSettings *mono_image_settings = [[[MonoImageSettings alloc] init] autorelease]; [mono_image_settings SetCompressionMode: e_jbig2]; [mono_image_settings ForceRecompression: true]; OptimizerSettings *opt_settings = [[[OptimizerSettings alloc] init] autorelease]; [opt_settings SetMonoImageSettings: mono_image_settings]; [Optimizer Optimize: doc settings: opt_settings]; [doc SaveToFile: @"../../TestFiles/Output/newsletter_opt3.pdf" flags: e_linearized]; } @catch (NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } NSLog(@"Done"); [pool release]; return ret; }