//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #import #import //----------------------------------------------------------------------------------- // This sample illustrates how to embed various raster image formats // (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document. // // Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to // be present in the system path. //----------------------------------------------------------------------------------- int main (int argc, const char * argv[]) { int ret = 0; [PDFNet Initialize: 0]; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; PDFDoc* doc = [[PDFDoc alloc] init]; ElementBuilder* builder = [[[ElementBuilder alloc] init] autorelease]; // Used to build new Element objects ElementWriter* writer = [[[ElementWriter alloc] init] autorelease]; // Used to write Elements to the page PDFRect *rect = [[[PDFRect alloc] init] autorelease]; [rect Set: 0 y1: 0 x2: 612 y2: 792]; Page* page = [doc PageCreate: rect]; // Start a new page [writer WriterBeginWithPage: page placement: e_overlay page_coord_sys: YES compress: YES]; // Begin writing to this page // ---------------------------------------------------------- // Add JPEG image to the output file Image* img = [Image Create: [doc GetSDFDoc] filename: @"../../TestFiles/peppers.jpg"]; Element* element = [builder CreateImageWithCornerAndScale: img x: 50 y: 500 hscale: [img GetImageWidth]/2 vscale: [img GetImageHeight]/2]; [writer WritePlacedElement: element]; // ---------------------------------------------------------- // Add a PNG image to the output file img = [Image Create: [doc GetSDFDoc] filename: @"../../TestFiles/butterfly.png"]; element = [builder CreateImageWithMatrix: img mtx: [[[Matrix2D alloc] initWithA: 100 b: 0 c: 0 d: 100 h: 300 v: 500] autorelease]]; [writer WritePlacedElement: element]; // ---------------------------------------------------------- // Add a TIFF image to the output file img = [Image Create: [doc GetSDFDoc] filename: @"../../TestFiles/grayscale.tif"]; element = [builder CreateImageWithMatrix: img mtx: [[[Matrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 10 v: 50] autorelease]]; [writer WritePlacedElement: element]; [writer End]; // Save the page [doc PagePushBack: page]; // Add the page to the document page sequence // ---------------------------------------------------------- // Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter. page = [doc PageCreate: [[[PDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794] autorelease]]; [writer WriterBeginWithPage: page placement: e_overlay page_coord_sys: YES compress: YES]; // begin writing to this page // Note: encoder hints can be used to select between different compression methods. // For example to instruct PDFNet to compress a monochrome image using JBIG2 compression. ObjSet* hint_set = [[[ObjSet alloc] init] autorelease]; Obj* enc=[hint_set CreateArray]; // Initialize encoder 'hint' parameter [enc PushBackName: @"JBIG2"]; [enc PushBackName: @"Lossy"]; img = [Image CreateWithFile: [doc GetSDFDoc] filename: @"../../TestFiles/multipage.tif" encoder_hints: enc]; element = [builder CreateImageWithMatrix: img mtx: [[[Matrix2D alloc] initWithA: 612 b: 0 c: 0 d: 794 h: 0 v: 0] autorelease]]; [writer WritePlacedElement: element]; [writer End]; // Save the page [doc PagePushBack: page]; // Add the page to the document page sequence*/ // ---------------------------------------------------------- // Add a JPEG2000 (JP2) image to the output file // Create a new page page = [doc PageCreate: [[[PDFRect alloc] initWithX1: 0 y1: 0 x2: 612 y2: 794] autorelease]]; [writer WriterBeginWithPage: page placement: e_overlay page_coord_sys: YES compress: YES]; // Begin writing to the page // Embed the image. img = [Image Create: [doc GetSDFDoc] filename: @"../../TestFiles/palm.jp2"]; // Position the image on the page. element = [builder CreateImageWithMatrix: img mtx: [[[Matrix2D alloc] initWithA: [img GetImageWidth] b: 0 c: 0 d: [img GetImageHeight] h: 96 v: 80] autorelease]]; [writer WritePlacedElement: element]; // Write 'JPEG2000 Sample' text string under the image. [writer WriteElement: [builder CreateTextBeginWithFont: [Font Create: [doc GetSDFDoc] type: e_times_roman embed: NO] font_sz: 32]]; element = [builder CreateTextRun: @"JPEG2000 Sample"]; [element SetTextMatrix: 1 b: 0 c: 0 d: 1 h: 190 v: 30]; [writer WriteElement: element]; [writer WriteElement: [builder CreateTextEnd]]; [writer End]; // Finish writing to the page [doc PagePushBack: page]; [doc SaveToFile: @"../../TestFiles/Output/AddImageTest.pdf" flags: e_linearized]; [pool release]; NSLog(@"Done."); return ret; }