How can I add decorations to an annotation?

Another customer asked how to change the opacity of annotation?

Some annotation types support this, so you would just call the API for that type.

But some annotation types don’t support it, such as Sticky Note annots. So the following code shows this simpler customization to the default appereance.

static public void ChangeOpacity(Annot annot, PDFDoc doc)
{
	ElementReader reader = new ElementReader();
	ElementWriter writer = new ElementWriter();
	ElementBuilder builder = new ElementBuilder();

	writer.Begin(doc); // start new content stream
	Obj old_app_stm = annot.GetAppearance();
	reader.Begin(old_app_stm);

	Element element;
	double my_opacity = 0.2;

	while ((element = reader.Next()) != null)
	{
		GState gs = element.GetGState();
		gs.SetFillOpacity(my_opacity);
		gs.SetStrokeOpacity(my_opacity);
		writer.WriteElement(element);
	}
	// not changing the size of the appearance so keep BBox value
	Obj new_app_stm = writer.End();
	new_app_stm.Put("BBox", old_app_stm.FindObj("BBox"));

	annot.SetAppearance(new_app_stm);
}