[PDFNet] How do I apply a vector soft mask to an image in PDF?

Q: We need to apply a mask to an image which is dynamically added to a
PDF being created with the PDFTron SDK and i was wondering if it is
possible to draw a vector rectangle with rounded corners and apply
this over the top of the image?
A: You can easily mask an existing image with another image using
'pdftron.PDF.Image.SetSoftMask' or 'SetMask()'. Masking an image or
any other graphical element is a bit more tricky but is possible.

You would create a 'Soft Mask' form xObject (containing path or other
elements) and pass this soft mask in

The code may look along the following lines:


// 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)
  ElementWriter w = new ElementWriter();
  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");

  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;

You received this message because you are subscribed to the "PDFTron PDFNet SDK" group. To post to this group, send email to support@pdftron.com
To unsubscribe from this group, send email to pdfnet-sdk-unsubscribe@googlegroups.com. For more information, please visit us at http://www.pdftron.com

It does not work when image_element has transparent areas.

Can you please show us an example of how to make it work for cases when image_element is an Image with alpha channel and it has transparent areas, which should stay transparent, not become black ?

Thank you