Cracking the Code: Managing PDF Forms
Do you struggle with PDF forms? Challenges like:
- Trouble opening, editing, accessing, or printing PDF form documents.
- Struggling to import and export information from your PDF form.
- PDF viewer software causing PDF form incompatibility issues and loss of features.
- Inefficient paper-forms processing slows down important workflows.
PDF forms are not always the easiest to work with, but fortunately we have forms management tools available within the Adobe PDF Library’s Forms Extension module that can help you employ powerful forms technology in your own applications and beyond. Let’s take a look at our forms code samples to demonstrate how the underlying technology works.
Here's a look at the Forms code samples we have available in .NET:
Converting XFA to AcroForm
XFA was deprecated in the new PDF 2.0 format and cannot be used with special ISO formats for PDF documents (such as PDF/A). Google Chrome and most other browsers will not display XFA documents, and XFA is not supported on mobile devices.The XFA format is being phased out, and the standard for managing forms within PDF files in the future will be AcroForms. If you are going to be building or manipulating PDF forms documents programmatically in the future, knowing to rely on AcroForms rather than XFA is important.
ConvertXFAToAcroForms is a code sample that demonstrates how to convert XFA (Dynamic or Static) fields to AcroForms fields and remove XFA fields.
namespace ConvertXFAToAcroForms
{
class ConvertXFAToAcroForms
{
static void Main(string[] args)
{
Console.WriteLine("ConvertXFAToAcroForms Sample:");
using (Library lib = new Library(LibraryFlags.InitFormsExtension))
{
if (!lib.IsFormsExtensionAvailable())
{
System.Console.Out.WriteLine("Forms Plugins were not properly loaded!");
return;
}
lib.AllowOpeningXFA = true;
Console.WriteLine("Initialized the library.");
String sInput = Library.ResourceDirectory + "Sample_Input/DynamicXFA.pdf";
String sOutput = "../ConvertXFAToAcroForms-out.pdf";
if (args.Length > 0)
{
sInput = args[0];
}
if (args.Length > 1)
{
sOutput = args[1];
}
using (Document doc = new Document(sInput))
{
UInt32 pagesOutput = doc.ConvertXFAFieldsToAcroFormFields();
Console.WriteLine("XFA document was converted into an AcroForms document with {0} pages.", pagesOutput);
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
}
Importing & Exporting Forms Data
Importing and exporting data from forms can be helpful when you need to view the data in the context of a PDF as opposed to other files types, or if you need to condense the data from a form and export it to use with different file formats.
In some cases, you may encounter completed forms as data-only files instead of full PDF files. These files use a different file format than PDFs, such as FDF, XML, or CSV. By viewing the original file and importing the data in the file, you can view the data submitted by a specific recipient in the context of the PDF.
Additionally, an exported file is usually smaller than the original PDF form file, which makes it much more convenient for distributing to others.
The ImportFormsData sample demonstrates how to Import forms data into XFA and AcroForms documents.
using System;
using System.Collections.Generic;
using System.Text;
using Datalogics.PDFL;
namespace ImportFormsData
{
class ImportFormsData
{
static void Main(string[] args)
{
Console.WriteLine("ImportFormsData Sample:");
using (Library lib = new Library(LibraryFlags.InitFormsExtension))
{
if (!lib.IsFormsExtensionAvailable())
{
System.Console.Out.WriteLine("Forms Plugins were not properly loaded!");
return;
}
lib.AllowOpeningXFA = true;
Console.WriteLine("Initialized the library.");
//XFA document
String sInput = Library.ResourceDirectory + "Sample_Input/DynamicXFA.pdf";
String sInputData = Library.ResourceDirectory + "Sample_Input/DynamicXFA_data.xdp";
String sOutput = "../ImportFormsDataXFA-out.pdf";
if (args.Length > 0)
{
sOutput = args[0];
}
using (Document doc = new Document(sInput))
{
//Import the data, acceptable types include XDP, XML, and XFD
bool result = doc.ImportXFAFormsData(sInputData);
if (result)
{
Console.Out.WriteLine("Forms data was imported!");
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
}
else
{
Console.Out.WriteLine("Importing of Forms data failed!");
}
}
//AcroForms document
sInput = Library.ResourceDirectory + "Sample_Input/AcroForm.pdf";
sInputData = Library.ResourceDirectory + "Sample_Input/AcroForm_data.xfdf";
sOutput = "../ImportFormsDataAcroForms-out.pdf";
if (args.Length > 1)
{
sOutput = args[1];
}
using (Document doc = new Document(sInput))
{
//Import the data while specifying the type, in this case XFDF
bool result = doc.ImportAcroFormsData(sInputData, AcroFormImportType.XFDF);
if (result)
{
Console.Out.WriteLine("Forms data was imported!");
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
}
else
{
Console.Out.WriteLine("Importing of Forms data failed!");
}
The ExportFormsData sample demonstrates how to Export forms data from XFA and AcroForms documents.
namespace ExportFormsData
{
class ExportFormsData
{
static void Main(string[] args)
{
Console.WriteLine("ExportFormsData Sample:");
using (Library lib = new Library(LibraryFlags.InitFormsExtension))
{
if (!lib.IsFormsExtensionAvailable())
{
System.Console.Out.WriteLine("Forms Plugins were not properly loaded!");
return;
}
lib.AllowOpeningXFA = true;
Console.WriteLine("Initialized the library.");
//XFA document
String sInput = Library.ResourceDirectory + "Sample_Input/DynamicXFA.pdf";
String sOutput = "../ExportFormsDataXFA.xdp";
if (args.Length > 0)
{
sOutput = args[0];
}
using (Document doc = new Document(sInput))
{
//Export the data while specifying the type, in this case XDP
bool result = doc.ExportXFAFormsData(sOutput, XFAFormExportType.XDP);
if (result)
{
Console.Out.WriteLine("Forms data was exported!");
}
else
{
Console.Out.WriteLine("Exporting of Forms data failed!");
}
}
//AcroForms document
sInput = Library.ResourceDirectory + "Sample_Input/AcroForm.pdf";
sOutput = "../ExportFormsDataAcroForms.xfdf";
if (args.Length > 1)
{
sOutput = args[1];
}
using (Document doc = new Document(sInput))
{
//Export the data while specifying the type, in this case XFDF
bool result = doc.ExportAcroFormsData(sOutput, AcroFormExportType.XFDF);
if (result)
{
Console.Out.WriteLine("Forms data was exported!");
}
else
{
Console.Out.WriteLine("Exporting of Forms data failed!");
}
PDF Forms Flattening
Widespread challenges in forms processing workflows can prevent many users from opening and viewing the content of documents on screen, editing documents, and printing to paper.
Most XFA documents, for example, are completely unintelligible to the majority of PDF software and can’t be processed at all. By flattening XFA and AcroForms, you can solve this problem by removing editable form fields that users can interact with and replace them with baked-in textual content which will no longer be editable when viewing them with PDF software.
The FlattenForms sample demonstrates how to Flatten two types of forms fields:
- Flatten XFA (Dynamic or Static) to regular page content which converts and expands XFA fields to regular PDF content and removes the XFA fields.
- Flatten AcroForms to regular page content which converts AcroForm fields to regular page content and removes the AcroForm fields.
namespace FlattenForms
{
class FlattenForms
{
static void Main(string[] args)
{
Console.WriteLine("FlattenForms Sample:");
using (Library lib = new Library(LibraryFlags.InitFormsExtension))
{
if (!lib.IsFormsExtensionAvailable())
{
System.Console.Out.WriteLine("Forms Plugins were not properly loaded!");
return;
}
//Must be set to true to prevent default legacy behavior of PDFL
lib.AllowOpeningXFA = true;
Console.WriteLine("Initialized the library.");
//XFA document
String sInput = Library.ResourceDirectory + "Sample_Input/DynamicXFA.pdf";
String sOutput = "../FlattenXFA-out.pdf";
if (args.Length > 0)
{
sInput = args[0];
}
if (args.Length > 1)
{
sOutput = args[1];
}
using (Document doc = new Document(sInput))
{
UInt32 pagesOutput = doc.FlattenXFAFormFields();
Console.WriteLine("XFA document was expanded into {0} Flattened pages.", pagesOutput);
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
}
//AcroForms document
sInput = Library.ResourceDirectory + "Sample_Input/AcroForm.pdf";
sOutput = "../FlattenAcroForms-out.pdf";
using (Document doc = new Document(sInput))
{
doc.FlattenAcroFormFields();
Console.WriteLine("AcroForms document was Flattened.");
doc.Save(SaveFlags.Full | SaveFlags.Linearized, sOutput);
}
Forms Extension is a great SDK for managing forms.
Prefer a command-line tool? Forms Flattener is your answer.