using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.entities;
using it.stefanochizzolini.clown.documents.contents.fonts;
using it.stefanochizzolini.clown.documents.contents.xObjects;
using it.stefanochizzolini.clown.documents.interaction.forms;
using it.stefanochizzolini.clown.files;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample demonstrates how to inspect the AcroForm fields of a PDF document.</summary>
*/
public class AcroFormParsingSample
: ISample
{
#region dynamic
#region interface
#region public
#region ISample
public void Run(
SampleLoader loader
)
{
// (boilerplate user choice -- ignore it)
string filePath = loader.GetPdfFileChoice("Please select a PDF file");
// 1. Open the PDF file!
File file = new File(filePath);
// Get the PDF document!
Document document = file.Document;
// 2. Get the acroform!
Form form = document.Form;
if(form == null)
{
Console.WriteLine("\nNo acroform available (AcroForm dictionary doesn't exist).");
return;
}
Console.WriteLine("\nIterating through the fields collection...\n");
// 3. Showing the acroform fields...
Dictionary<string,int> objCounters = new Dictionary<string,int>();
foreach(Field field in form.Fields.Values)
{
Console.WriteLine("* Field '" + field.FullName + "' (" + field.BaseObject + ")");
string typeName = field.GetType().Name;
Console.WriteLine(" Type = " + typeName);
Page fieldPage = field.Widget.Page;
if(fieldPage == null)
{Console.WriteLine(" Page = undefined");}
else
{Console.WriteLine(" Page = " + (fieldPage.Index + 1) + " (" + fieldPage.BaseObject + ")");}
Console.WriteLine(" Data = " + field.BaseDataObject.ToString());
if(objCounters.ContainsKey(typeName))
{objCounters[typeName] = objCounters[typeName] + 1;}
else
{objCounters[typeName] = 1;}
}
Console.WriteLine("\nFields partial counts (grouped by type):");
foreach(KeyValuePair<string,int> entry in objCounters)
{Console.WriteLine(" " + entry.Key + ": " + entry.Value);}
Console.WriteLine("Fields total count: " + form.Fields.Count);
}
#endregion
#endregion
#endregion
#endregion
}
}
|