Creating a soft mask from non-isolated knockout group (for element.GState.SetSoftMask())

Q: I have a new question that I hope you can provide some guidance
with. We are trying to use the Soft Mask in the GState (SetSoftMask),
and more specifically we would like to know if there are any samples
that describe creating a soft mask from non-isolated knockout group
alpha object data and applying the mask to a non-isolated non-knockout
group?
-----------------
A: You can create a 'Soft Mask' for the graphics state from a ‘Form
XObject’ (that can contain paths, images, text etc) .

The code may look along the following lines (C# psudocode, other
languages are similar apart from minor syntax differences):

...
element.GetGState().SetSoftMask(CreateSoftMask(doc)).
...

// Pseudocode to create a soft mask using any graphical element
(raster or vector) // that can be assigned to any other element.

Obj CreateSoftMask(PDFDoc doc)
{
  using (ElementWriter w = new ElementWriter()) {
  using (ElementBuilder b = new ElementBuilder()) {
  w.Begin(doc);

  // start constructing the path
  b.PathBegin();
  b.MoveTo(306, 396);
  b.CurveTo(681, 771, 399.75, 864.75, 306, 771);
  b.CurveTo(212.25, 864.75, -69, 771, 306, 396);
  b.ClosePath();
  Element e = b.PathEnd();

  e.SetPathFill(true);
  e.SetPathClip(false);
  e.SetPathStroke(false);
  GState gstate = e.GetGState();
  gstate.SetFillColorSpace(pdftron.PDF.ColorSpace.CreateDeviceGray());
  gstate.SetFillColor(new ColorPt(0.5));

  Rect bbox = new Rect();
  e.GetBBox(bbox);

  w.WriteElement(e);
  Obj xobject=w.End();
  xobject.PutName("S", "Transparency");
  xobject.PutName("Subtype", "Form");

  xobject.PutRect("BBox", bbox.x1, bbox.y1, bbox.x2, bbox.y2);
  xobject.PutMatrix("Matrix", new Matrix2D(1, 0, 0, 1, 0, 0));

  Obj grp= xobject.PutDict("Group");
  grp.PutName("CS","DeviceGray");
  grp.PutBool("I",false);
  grp.PutBool("K",false);
  grp.PutName("S","Transparency");
  grp.PutName("Type","Group");

// Alternatively you can create a form XObject from any PDF page using
b.CreateForm(page)
// ( or b.CreateForm(page, doc) if the page is loaded from another
PDF doc).

  Obj soft_dict = doc.CreateIndirectDict();
  if(m_use_alpha) oft_dict.PutName("S", "Alpha");
  else soft_dict.PutName("S", "Luminosity");
  soft_dict.PutName("Type", "Mask");
  soft_dict.Put("G", xobject);
                 }
               }

  return soft_dict;
}