Product: PDFTron .NET x64
Product Version: 9.1.0
Setting Custom Checkbox Appearance
I am trying to customize the appearance of a checkbox field to use other ZapfDingbats characters (namely, a circle).
I am using the following version of the CreateCheckmarkAppearance() function:
private Obj CreateCheckmarkAppearance(PDFDoc doc, Rect updateRectangle) {
// Create a checkmark appearance stream
var build = new ElementBuilder();
var writer = new ElementWriter();
writer.Begin(doc);
// Draw background
Element e = build.CreateRect(updateRectangle.x1, updateRectangle.y1, updateRectangle.Width(), updateRectangle.Height());
e.SetPathFill(true); // this path is should be filled
// Set the path color space and color
GState gstate = e.GetGState();
gstate.SetFillColorSpace(ColorSpace.CreateDeviceRGB());
gstate.SetFillColor(new ColorPt(1, 0, 0)); // red
writer.WritePlacedElement(e);
// Draw checkmark
writer.WriteElement(build.CreateTextBegin());
// other options are circle ("l"), diamond ("H"), cross ("\x35")
// See section D.4 "ZapfDingbats Set and Encoding" in PDF Reference
// Manual for the complete graphical map for ZapfDingbats font.
e = build.CreateTextRun("l", Font.Create(doc, Font.StandardType1Font.e_zapf_dingbats), 1);
writer.WriteElement(e);
writer.WriteElement(build.CreateTextEnd());
Obj stm = writer.End();
// Set the bounding box
stm.PutRect("BBox", updateRectangle.x1, updateRectangle.y1, updateRectangle.x2, updateRectangle.y2);
stm.PutName("Subtype", "Form");
return stm;
}
I’ve confirmed that I can successfully create a widget field and set the border and background colors, but, so far, I have been unable to modify the appearance in any way using the SetAppearance() function, like so:
var widget = CheckBoxWidget.Create(srcPdfDoc, valField.GetUpdateRect(), valField);
// Widget colors are set properly when the following lines are uncommented
//widget.SetBackgroundColor(new ColorPt(1, 0, 0), 3);
//widget.SetBorderColor(new ColorPt(1, 0, 0), 3);
widget.SetAppearance(this.CreateCheckmarkAppearance(srcPdfDoc, valField.GetUpdateRect()));
widget.SetChecked(true);
widget.RefreshAppearance();
valField.RefreshAppearance();
srcPdfDoc.GetPage(1).AnnotPushFront(widget);
Why isn’t the background fill color getting set to red, let alone the ZapfDingbats character getting modified?