//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #import #import //--------------------------------------------------------------------------------------- // PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract // forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality // available in PDFNet. //--------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int ret = 0; [PDFNet Initialize: 0]; // Example 1) // Iterate over all form fields in the document. Display all field names. @try { PDFDoc *doc = [[PDFDoc alloc] initWithFilepath: @"../../TestFiles/form1.pdf"]; [doc InitSecurityHandler]; FieldIterator *itr; for(itr = [doc GetFieldIterator]; [itr HasNext]; [itr Next]) { NSLog(@"Field name: %@", [[itr Current] GetName]); NSLog(@"Field partial name: %@", [[itr Current] GetPartialName]); NSLog(@"Field type: "); int type = [[itr Current] GetType]; switch(type) { case e_button: NSLog(@"Button"); break; case e_text: NSLog(@"Text"); break; case e_choice: NSLog(@"Choice"); break; case e_signature: NSLog(@"Signature"); break; } NSLog(@"------------------------------"); } NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } // Example 2) Merge data from FDF @try { NSLog(@"Merge data from FDF."); PDFDoc *doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/form1.pdf"] autorelease]; [doc InitSecurityHandler]; FDFDoc *fdf_doc = [[[FDFDoc alloc] initWithFilepath: @"../../TestFiles/form1_data.fdf"] autorelease]; [doc FDFMerge: fdf_doc]; // To use PDFNet form field appearance generation instead of relying on // Acrobat, uncomment the following two lines: // doc.RefreshFieldAppearances(); // doc.GetAcroForm()->Put("NeedAppearances", new Bool(false)); [doc SaveToFile: @"../../TestFiles/Output/form1_filled.pdf" flags: e_linearized]; // Now merge data from XFDF. First create a FDF document from a XFDF file, then // merge data from FDF. FDFDoc *xfdf_doc = [FDFDoc CreateFromXFDF: @"../../TestFiles/form1_data.xfdf"]; [doc FDFMerge: xfdf_doc]; [doc SaveToFile: @"../../TestFiles/Output/form1_filled_xfdf.pdf" flags: e_linearized]; NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } // Example 3) Extract data to FDF @try { NSLog(@"Extract data to FDF."); PDFDoc *in_doc = [[[PDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/form1_filled.pdf"] autorelease]; [in_doc InitSecurityHandler]; FDFDoc *doc = [in_doc FDFExtract]; [doc SetPDFFileName: @"../form1.pdf"]; [doc SaveFDFDocToFile: @"../../TestFiles/Output/form1_filled_data.fdf"]; // Now save the FDF document in XFDF format [doc SaveAsXFDF: @"../../TestFiles/Output/form1_filled_data.xfdf"]; NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } // Example 4) Read FDF files directly @try { FDFDoc *doc = [[[FDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/form1_filled_data.fdf"] autorelease]; FDFFieldIterator *itr; for(itr = [doc GetFieldIterator]; [itr HasNext]; [itr Next]) { NSLog(@"Field name: %@", [[itr Current] GetName]); NSLog(@"Field partial name: %@", [[itr Current] GetPartialName]); NSLog(@"------------------------------"); } NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } // Example 5) Direct generation of FDF. @try { FDFDoc *doc = [[FDFDoc alloc] init]; // Create new fields (i.e. key/value pairs). [doc FieldCreateWithString: @"Company" type: e_text field_value: @"PDFTron Systems"]; [doc FieldCreateWithString: @"First Name" type: e_text field_value: @"John"]; [doc FieldCreateWithString: @"Last Name" type: e_text field_value: @"Doe"]; // ... // [doc SetPdfFileName: @"mydoc.pdf"]; [doc SaveFDFDocToFile: @"../../TestFiles/Output/sample_output.fdf"]; NSLog(@"Done."); } @catch(NSException *e) { NSLog(@"%@", [e reason]); ret = 1; } [pool release]; return ret; }