//--------------------------------------------------------------------------------------- // 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 convert PDF documents to various raster image // formats (such as PNG, JPEG, BMP, TIFF, etc), as well as how to convert a PDF page to // GDI+ Bitmap for further manipulation and/or display in WinForms applications. //--------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @try { // 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]; // Optional: Set ICC color profiles to fine tune color conversion // for PDF 'device' color spaces... // PDFNet::SetResourcesPath("../../../resources"); // PDFNet::SetColorManagement(); // PDFNet::SetDefaultDeviceCMYKProfile("D:/Misc/ICC/USWebCoatedSWOP.icc"); // PDFNet::SetDefaultDeviceRGBProfile("AdobeRGB1998.icc"); // will search in PDFNet resource folder. // ---------------------------------------------------- // Optional: Set predefined font mappings to override default font // substitution for documents with missing fonts... // PDFNet::AddFontSubst("StoneSans-Semibold", "C:/WINDOWS/Fonts/comic.ttf"); // PDFNet::AddFontSubst("StoneSans", "comic.ttf"); // search for 'comic.ttf' in PDFNet resource folder. // PDFNet::AddFontSubst(PDFNet::e_Identity, "C:/WINDOWS/Fonts/arialuni.ttf"); // PDFNet::AddFontSubst(PDFNet::e_Japan1, "C:/Program Files/Adobe/Acrobat 7.0/Resource/CIDFont/KozMinProVI-Regular.otf"); // PDFNet::AddFontSubst(PDFNet::e_Japan2, "c:/myfonts/KozMinProVI-Regular.otf"); // PDFNet::AddFontSubst(PDFNet::e_Korea1, "AdobeMyungjoStd-Medium.otf"); // PDFNet::AddFontSubst(PDFNet::e_CNS1, "AdobeSongStd-Light.otf"); // PDFNet::AddFontSubst(PDFNet::e_GB1, "AdobeMingStd-Light.otf"); PDFDraw *draw = [[[PDFDraw alloc] initWithDpi: 92] autorelease]; // PDFDraw class is used to rasterize PDF pages. //-------------------------------------------------------------------------------- // Example 1) Convert the first page to PNG and TIFF at 92 DPI. // A three step tutorial to convert PDF page to an image. @try { // A) Open the PDF document. PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/tiger.pdf"] autorelease]; // Initialize the security handler, in case the PDF is encrypted. [doc InitSecurityHandler]; // B) The output resolution is set to 92 DPI. [draw SetDPI: 92]; // C) Rasterize the first page in the document and save the result as PNG. [draw Export: [[doc GetPageIterator: 1] Current] filename: @"../../TestFiles/Output/tiger_92dpi.png" format: @"PNG"]; NSLog(@"Example 1: ../../TestFiles/Output/tiger_92dpi.png. Done."); // Export the same page as TIFF [draw Export: [[doc GetPageIterator: 1] Current] filename: @"../../TestFiles/Output/tiger_92dpi.tif" format: @"TIFF"]; } @catch(NSException *e) { NSLog(@"%@", [e reason]); } //-------------------------------------------------------------------------------- // Example 2) Convert the all pages in a given document to JPEG at 72 DPI. NSLog(@"Example 2:"); ObjSet *hint_set = [[[ObjSet alloc] init] autorelease]; // A collection of rendering 'hits'. @try { PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"] autorelease]; // Initialize the security handler, in case the PDF is encrypted. [doc InitSecurityHandler]; [draw SetDPI: 72]; // Set the output resolution is to 72 DPI. // Use optional encoder parameter to specify JPEG quality. Obj *encoder_param=[hint_set CreateDict]; [encoder_param PutNumber: @"Quality" value: 80]; // Traverse all pages in the document. PageIterator *itr; for (itr=[doc GetPageIterator: 1]; [itr HasNext]; [itr Next]) { NSString *filename = [@"../../TestFiles/Output/" stringByAppendingFormat: @"newsletter%d.jpg", [[itr Current] GetIndex]]; NSLog(@"%@", filename); //[draw Export: [itr Current] filename: filename format: @"JPEG" encoder_params: encoder_param]; } NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); } // Examples 3-5 @try { // Common code for remaining samples. PDFDoc *tiger_doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/tiger.pdf"] autorelease]; // Initialize the security handler, in case the PDF is encrypted. [tiger_doc InitSecurityHandler]; Page *page = [tiger_doc GetPage: 1]; //-------------------------------------------------------------------------------- // Example 3) Convert the first page to raw bitmap. Also, rotate the // page 90 degrees and save the result as RAW. [draw SetDPI: 100]; // Set the output resolution is to 100 DPI. [draw SetRotate: e_90]; // Rotate all pages 90 degrees clockwise. BitmapInfo* buf = [draw GetBitmap: page pix_fmt: e_rgb demult: NO]; // Save the raw RGB data to disk. StdFile *file = [[[StdFile alloc] initWithFilename: @"../../TestFiles/Output/tiger_100dpi_rot90.raw" open_mode: e_write_mode buf_sz: 1024] autorelease]; FilterWriter *writer = [[[FilterWriter alloc] initWithFilter: file] autorelease]; [writer WriteBuffer: [buf GetBuffer]]; [writer Flush]; NSLog(@"Example 3: ../../TestFiles/Output/tiger_100dpi_rot90.raw. Done."); [draw SetRotate: e_0]; // Disable image rotation for remaining samples. //-------------------------------------------------------------------------------- // Example 4) Convert PDF page to a fixed image size. Also illustrates some // other features in PDFDraw class such as rotation, image stretching, exporting // to grayscale, or monochrome. // Initialize render 'gray_hint' parameter, that is used to control the // rendering process. In this case we tell the rasterizer to export the image as // 1 Bit Per Component (BPC) image. Obj *mono_hint=[hint_set CreateDict]; [mono_hint PutNumber: @"BPC" value: 1]; // SetImageSize can be used instead of SetDPI() to adjust page scaling // dynamically so that given image fits into a buffer of given dimensions. [draw SetImageSize: 1000 height: 1000 preserve_aspect_ratio: YES]; // Set the output image to be 1000 wide and 1000 pixels tall [draw ExportWithObj: page filename: @"../../TestFiles/Output/tiger_1000x1000.png" format: @"PNG" encoder_params: mono_hint]; NSLog(@"Example 4: ../../TestFiles/Output/tiger_1000x1000.png. Done."); [draw SetImageSize: 200 height: 400 preserve_aspect_ratio: YES]; // Set the output image to be 200 wide and 300 pixels tall [draw SetRotate: e_180]; // Rotate all pages 90 degrees clockwise. // 'gray_hint' tells the rasterizer to export the image as grayscale. Obj *gray_hint=[hint_set CreateDict]; [gray_hint PutName: @"ColorSpace" name: @"Gray"]; [draw ExportWithObj: page filename: @"../../TestFiles/Output/tiger_200x400_rot180.png" format: @"PNG" encoder_params: gray_hint]; NSLog(@"Example 4: ../../TestFiles/Output/tiger_200x400_rot180.png. Done."); [draw SetImageSize: 400 height: 200 preserve_aspect_ratio: NO]; // The third parameter sets 'preserve-aspect-ratio' to false. [draw SetRotate: e_0]; // Disable image rotation. [draw Export: page filename: @"../../TestFiles/Output/tiger_400x200_stretch.jpg" format: @"JPEG"]; NSLog(@"Example 4: ../../TestFiles/Output/tiger_400x200_stretch.jpg. Done."); //-------------------------------------------------------------------------------- // Example 5) Zoom into a specific region of the page and rasterize the // area at 200 DPI and as a thumbnail (i.e. a 50x50 pixel image). PDFRect *zoom_rect = [[[PDFRect alloc] initWithX1: 216 y1: 522 x2: 330 y2: 600] autorelease]; [page SetCropBox: zoom_rect]; // Set the page crop box. // Select the crop region to be used for drawing. [draw SetPageBox: e_crop]; [draw SetDPI: 900]; // Set the output image resolution to 900 DPI. [draw Export: page filename: @"../../TestFiles/Output/tiger_zoom_900dpi.png" format: @"PNG"]; NSLog(@"Example 5: ../../TestFiles/Output/tiger_zoom_900dpi.png. Done."); [draw SetImageSize: 50 height: 50 preserve_aspect_ratio: YES]; // Set the thumbnail to be 50x50 pixel image. [draw Export: page filename: @"../../TestFiles/Output/tiger_zoom_50x50.png" format: @"PNG"]; NSLog(@"Example 5: ../../TestFiles/Output/tiger_zoom_50x50.png. Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); } Obj *cmyk_hint = [hint_set CreateDict]; [cmyk_hint PutName: @"ColorSpace" name: @"CMYK"]; //-------------------------------------------------------------------------------- // Example 7) Convert the frist PdF page to CMYK TIFF at 92 DPI. // A three stemp tutorial to convert PDF page to an image NSLog(@"Example 7:"); @try { // A) Open the PDF document. PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/tiger.pdf"] autorelease]; // Initialize the security handler, in case the PDF is encrypted. [doc InitSecurityHandler]; // B) The output resolution is set to 92 DPI. [draw SetDPI: 92]; // C) Rasterize the first page in the document and save the result as TIFF. Page *pg = [doc GetPage: 1]; [draw ExportWithObj: pg filename: @"../../TestFiles/Output/out1.tif" format: @"TIFF" encoder_params: cmyk_hint]; NSLog(@"Example 7: Result saved in ../../TestFiles/Output/out1.tif"); } @catch(NSException *e) { NSLog(@"%@", [e reason]); } } @catch(NSException *e) { NSLog(@"%@", [e reason]); } [pool release]; return 1; }