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

335 views
Skip to first unread message

Support

unread,
May 12, 2010, 1:46:16 PM5/12/10
to PDFTron PDFNet SDK
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
image_element.GetGState().SetSoftMask(smask).

The code may look along the following lines:

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

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

w.Dispose();
b.Dispose();

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 sup...@pdftron.com
To unsubscribe from this group, send email to pdfnet-sdk-...@googlegroups.com. For more information, please visit us at http://www.pdftron.com

Will

unread,
Mar 14, 2016, 3:17:10 PM3/14/16
to PDFTron PDFNet SDK
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
Reply all
Reply to author
Forward
0 new messages