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
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):


// 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()) {

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

  GState gstate = e.GetGState();
  gstate.SetFillColor(new ColorPt(0.5));

  Rect bbox = new Rect();

  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");

// Alternatively you can create a form XObject from any PDF page using
// ( 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;