//--------------------------------------------------------------------------------------- // 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 create, extract, and manipulate PDF Portfolios /// (a.k.a. PDF Packages) using PDFNet SDK. //----------------------------------------------------------------------------------- // Relative path to the folder containing test files. NSString *input_path = @"../../TestFiles/"; NSString *output_path = @"../../TestFiles/Output/"; void AddPackage(PDFDoc *doc, NSString *file, NSString* desc) { NameTree *files = [NameTree Create: [doc GetSDFDoc] name: @"EmbeddedFiles"]; FileSpec *fs = [FileSpec Create: [doc GetSDFDoc] path: file embed: true]; [files Put: [file dataUsingEncoding: NSUTF8StringEncoding] key_sz: [file length] value: [fs GetSDFObj]]; [fs SetDesc: desc]; Obj *collection = [[doc GetRoot] FindObj: @"Collection"]; if (!collection) collection = [[doc GetRoot] PutDict: @"Collection"]; // You could here manipulate any entry in the Collection dictionary. // For example, the following line sets the tile mode for initial view mode // Please refer to section '2.3.5 Collections' in PDF Reference for details. [collection PutName: @"View" name: @"T"]; } void AddCoverPage(PDFDoc *doc) { // Here we dynamically generate cover page (please see ElementBuilder // sample for more extensive coverage of PDF creation API). PDFRect *rect = [[PDFRect alloc] init]; [rect Set: 0 y1: 0 x2: 200 y2: 200]; Page *page = [doc PageCreate: rect]; ElementBuilder *b = [[ElementBuilder alloc] init]; ElementWriter *w = [[ElementWriter alloc] init]; [w WriterBeginWithPage: page placement: e_overlay page_coord_sys: YES compress: YES]; Font *font = [Font Create: [doc GetSDFDoc] type: e_helvetica embed: NO]; [w WriteElement: [b CreateTextBeginWithFont: font font_sz: 12]]; Element *e = [b CreateTextRun: @"My PDF Collection"]; Matrix2D *mtx = [[Matrix2D alloc] initWithA: 1 b: 0 c: 0 d: 1 h: 50 v: 96]; [e SetTextMatrixWithMatrix2D: mtx]; [[e GetGState] SetFillColorSpace: [ColorSpace CreateDeviceRGB]]; [[e GetGState] SetFillColorWithColorPt: [[ColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0]]; [w WriteElement: e]; [w WriteElement: [b CreateTextEnd]]; [w End]; [doc PagePushBack: page]; // Alternatively we could import a PDF page from a template PDF document // (for an example please see PDFPage sample project). // ... } int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int ret = 0; [PDFNet Initialize: 0]; // Create a PDF Package. @try { PDFDoc *doc = [[PDFDoc alloc] init]; AddPackage(doc, @"../../TestFiles/numbered.pdf", @"My File 1"); AddPackage(doc, @"../../TestFiles/newsletter.pdf", @"My Newsletter..."); AddPackage(doc, @"../../TestFiles/peppers.jpg", @"An image"); AddCoverPage(doc); [doc SaveToFile: @"../../TestFiles/Output/package.pdf" flags: e_linearized]; NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } // Extract parts from a PDF Package. @try { PDFDoc *doc = [[PDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/package.pdf"]; [doc InitSecurityHandler]; NameTree *files = [NameTree Find: [doc GetSDFDoc] name: @"EmbeddedFiles"]; if([files IsValid]) { // Traverse the list of embedded files. DictIterator *i = [files GetIterator]; int counter = 0; for (; [i HasNext]; [i Next], ++counter) { NSString *entry_name = [[i Key] GetAsPDFText]; NSLog(@"Part: %@", entry_name); FileSpec *file_spec = [[FileSpec alloc] initWithF: [i Value]]; Filter *stm = [file_spec GetFileData]; if (stm) { FilterReader *reader = [[FilterReader alloc] initWithFilter: stm]; NSString *str = [NSString stringWithFormat: @"../../TestFiles/Output/extract_%d", counter]; StdFile *f = [[StdFile alloc] initWithFilename: str open_mode: e_write_mode buf_sz: 1024]; FilterWriter *writer = [[FilterWriter alloc] initWithFilter: f]; [writer WriteFilter: reader]; [writer Flush]; } } } NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } [pool release]; return ret; }