//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include #include #include using namespace std; using namespace pdftron; using namespace SDF; using namespace FDF; using namespace PDF; //--------------------------------------------------------------------------------------- // 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[]) { int ret = 0; PDFNet::Initialize(); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Example 1) // Iterate over all form fields in the document. Display all field names. try { PDFDoc doc((input_path + "form1.pdf").c_str()); doc.InitSecurityHandler(); for(FieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) { cout << "Field name: " << itr.Current().GetName() << endl; cout << "Field partial name: " << itr.Current().GetPartialName() << endl; cout << "Field type: "; Field::Type type = itr.Current().GetType(); switch(type) { case Field::e_button: cout << "Button" << endl; break; case Field::e_text: cout << "Text" << endl; break; case Field::e_choice: cout << "Choice" << endl; break; case Field::e_signature: cout << "Signature" << endl; break; } cout << "------------------------------" << endl; } cout << "Done." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Example 2) Merge data from FDF try { cout << "Merge data from FDF." << endl; PDFDoc doc((input_path + "form1.pdf").c_str()); doc.InitSecurityHandler(); FDFDoc fdf_doc((input_path + "form1_data.fdf").c_str()); 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.Save((output_path + "form1_filled.pdf").c_str(), SDFDoc::e_linearized, 0); // Now merge data from XFDF. First create a FDF document from a XFDF file, then // merge data from FDF. FDFDoc xfdf_doc(FDFDoc::CreateFromXFDF((input_path + "form1_data.xfdf").c_str())); doc.FDFMerge(xfdf_doc); doc.Save((output_path + "form1_filled_xfdf.pdf").c_str(), SDFDoc::e_linearized, 0); cout << "Done." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Example 3) Extract data to FDF try { cout << "Extract data to FDF." << endl; PDFDoc in_doc((output_path + "form1_filled.pdf").c_str()); in_doc.InitSecurityHandler(); FDFDoc doc = in_doc.FDFExtract(); doc.SetPDFFileName("../form1.pdf"); doc.Save((output_path + "form1_filled_data.fdf").c_str()); // Now save the FDF document in XFDF format doc.SaveAsXFDF((output_path + "form1_filled_data.xfdf").c_str()); cout << "Done." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Example 4) Read FDF files directly try { FDFDoc doc((output_path + "form1_filled_data.fdf").c_str()); for(FDFFieldIterator itr = doc.GetFieldIterator(); itr.HasNext(); itr.Next()) { cout << "Field name: " << itr.Current().GetName() << endl; cout << "Field partial name: " << itr.Current().GetPartialName() << endl; cout << "------------------------------" << endl; } cout << "Done." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Example 5) Direct generation of FDF. try { FDFDoc doc; // Create new fields (i.e. key/value pairs). doc.FieldCreate("Company", PDF::Field::e_text, "PDFTron Systems"); doc.FieldCreate("First Name", PDF::Field::e_text, "John"); doc.FieldCreate("Last Name", PDF::Field::e_text, "Doe"); // ... // doc.SetPdfFileName("mydoc.pdf"); doc.Save((output_path + "sample_output.fdf").c_str()); cout << "Done." << endl; } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } PDFNet::Terminate(); return ret; }