Q:
I have a pdf with various check boxes, and I want them to each have a standard check mark appearance. How can I do so?
A:
The following code changes all the check boxes of a document. Note, however, that some PDF viewers’ “Highlight Fields” will overlay the check boxes with their original values; you should turn this off to see the correct display of the check boxes.)
`
static void Main(string[] args)
{
PDFNet.Initialize();
string input_path = “…/…/…/…/TestFiles/”;
string output_path = “…/…/…/…/TestFiles/Output/”;
try
{
using (PDFDoc doc = new PDFDoc(input_path +
“Checklist Test Form.pdf”))
{
doc.InitSecurityHandler();
FieldIterator itr;
for(itr=doc.GetFieldIterator(); itr.HasNext(); itr.Next())
{
Field field = itr.Current();
Field.Type type = field.GetType();
if (type == Field.Type.e_check)
{
Widget widget = new Widget(field.GetSDFObj());
widget.SetAppearance(CreateCheckmarkAppearance(doc),
Annot.AnnotationState.e_normal, “Yes”);
widget.SetAppearance(CreateCheckmarkAppearance(doc),
Annot.AnnotationState.e_down, “Yes”);
}
}
doc.RefreshFieldAppearances();
doc.Save(output_path + “Checklist Test Form.pdf”, 0);
}
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
static Obj CreateCheckmarkAppearance(PDFDoc doc)
{
using (ElementBuilder builder = new ElementBuilder())
using (ElementWriter writer = new ElementWriter())
{
writer.Begin(doc);
writer.WriteElement(builder.CreateTextBegin());
Element checkmark = builder.CreateTextRun(“4”,
Font.Create(doc,
Font.StandardType1Font.e_zapf_dingbats), 1);
writer.WriteElement(checkmark);
writer.WriteElement(builder.CreateTextEnd());
Obj stm = writer.End();
stm.PutRect(“BBox”, -0.2, -0.2, 1, 1);
stm.PutName(“Subtype”, “Form”);
return stm;
}
}
`