//
// Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved.
//
using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;
using pdftron.FDF;
namespace FDFTestCS
{
///
/// PDFNet includes full support for FDF (Forms Data Format) and for merging/extracting
/// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
/// available in PDFNet.
///
class Class1
{
///
/// The main entry point for the application.
///
static void Main(string[] args)
{
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 = new PDFDoc(input_path + "form1.pdf");
doc.InitSecurityHandler();
FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
{
Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
Console.Write("Field type: ");
Field.Type type = itr.Current().GetType();
switch(type)
{
case Field.Type.e_button:
Console.WriteLine("Button"); break;
case Field.Type.e_text:
Console.WriteLine("Text"); break;
case Field.Type.e_choice:
Console.WriteLine("Choice"); break;
case Field.Type.e_signature:
Console.WriteLine("Signature"); break;
}
Console.WriteLine("------------------------------");
}
doc.Close();
Console.WriteLine("Done.");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
// Example 2) Merge data from FDF
try
{
Console.WriteLine("Merge data from fdf file...");
PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
doc.InitSecurityHandler();
FDFDoc fdf_doc = new FDFDoc(input_path + "form1_data.fdf");
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", Obj.CreateBool(false));
doc.Save(output_path + "form1_filled.pdf", SDFDoc.SaveOptions.e_linearized);
doc.Close();
Console.WriteLine("Done. Result saved in form1_filled.pdf");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
// Example 3) Extract data to FDF
try
{
Console.WriteLine("Extract data to fdf file...");
PDFDoc in_doc = new PDFDoc(output_path + "form1_filled.pdf");
in_doc.InitSecurityHandler();
FDFDoc doc = in_doc.FDFExtract();
doc.SetPdfFileName("../form1.pdf");
doc.Save(output_path + "form1_filled_data.fdf");
in_doc.Close();
Console.WriteLine("Done. Result saved in form1_filled_data.fdf");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
// Example 4) Read FDF files directly
try
{
FDFDoc doc = new FDFDoc(output_path + "form1_filled_data.fdf");
FDFFieldIterator itr = doc.GetFieldIterator();
for(; itr.HasNext(); itr.Next())
{
Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
Console.WriteLine("Field partial name: {0:s}", itr.Current().GetPartialName());
Console.WriteLine("------------------------------");
}
Console.WriteLine("Done.");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
// Example 5) Direct generation of FDF.
try
{
FDFDoc doc = new FDFDoc();
// Create new fields (i.e. key/value pairs).
doc.FieldCreate("Company", (int)Field.Type.e_text, "PDFTron Systems");
doc.FieldCreate("First Name", (int)Field.Type.e_text, "John");
doc.FieldCreate("Last Name", (int)Field.Type.e_text, "Doe");
// ...
// doc.SetPdfFileName("mydoc.pdf");
doc.Save(output_path + "fdf_sample_output.fdf");
Console.WriteLine("Done. Results saved in fdf_sample_output.fdf");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
PDFNet.Terminate();
}
}
}