Product:PDFnet
Product Version:9.5.0.1
Please give a brief summary of your issue:
How can I create linear gradients with alpha transparency?
Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)
Hi, Im trying to create a linear gradient going from CMYK 0 0 0 1 alpha 1 to CMYK 0 0 0 0 alpha 0.
I have been attempting with:
public void drawingTest()
{
PDFNet.Initialize(pdfTronSerial);
using (PDFDoc doc = new PDFDoc())
using (ElementBuilder eb = new ElementBuilder()) // ElementBuilder is used to build new Element objects
using (ElementWriter writer = new ElementWriter()) // ElementWriter is used to write Elements to the page
{
Page page = doc.PageCreate(new Rect(0, 0, 612, 794));
writer.Begin(page); // begin writing to this page
eb.PathBegin();
eb.MoveTo(250, 250);
eb.LineTo(500, 250);
eb.LineTo(500, 500);
eb.LineTo(250, 500);
eb.LineTo(250, 250);
Element heart = eb.PathEnd();
heart.SetPathFill(true);
heart.GetGState().SetFillColorSpace(ColorSpace.CreatePattern());
heart.GetGState().SetFillColor(CreateAxialShading(doc));
heart.GetGState().SetSoftMask(CreateSoftMask(doc,page));
//heart.GetGState().Get
writer.WriteElement(heart);
writer.End(); // Save the page
doc.PagePushBack(page);
doc.Save("d:\\drawTest.pdf", SDFDoc.SaveOptions.e_remove_unused);
}
}
static PatternColor CreateAxialShading(PDFDoc doc)
{
// Create a new Shading object ------------
Obj pattern_dict = doc.CreateIndirectDict();
// Initialize pattern dictionary. For details on what each parameter represents
// please refer to Tables 4.30 and 4.26 in PDF Reference Manual
pattern_dict.PutName("Type", "Pattern");
pattern_dict.PutNumber("PatternType", 2); // 2 stands for shading
Obj shadingDict = pattern_dict.PutDict("Shading");
shadingDict.PutNumber("ShadingType", 2);
shadingDict.PutName("ColorSpace", "DeviceCMYK");
// Set the coordinates of the axial shading to the output
shadingDict.PutRect("Coords", 250, 250, 500, 500);
// Set the Functions for the axial shading
Obj funct = shadingDict.PutDict("Function");
Obj C0 = funct.PutArray("C0");
C0.PushBackNumber(0);
C0.PushBackNumber(0);
C0.PushBackNumber(0);
C0.PushBackNumber(1);
Obj C1 = funct.PutArray("C1");
C1.PushBackNumber(0);
C1.PushBackNumber(0);
C1.PushBackNumber(0);
C1.PushBackNumber(0);
Obj domain = funct.PutArray("Domain");
domain.PushBackNumber(0);
domain.PushBackNumber(1);
funct.PutNumber("FunctionType", 2);
funct.PutNumber("N", 1);
return new PatternColor(pattern_dict);
}
Obj CreateSoftMask(PDFDoc doc, Page page)
{
Obj soft_dict = null;
using (ElementWriter w = new ElementWriter())
{
using (ElementBuilder b = new ElementBuilder())
{
w.Begin(page);
b.PathBegin();
b.MoveTo(250, 250);
b.LineTo(500, 250);
b.LineTo(500, 500);
b.LineTo(250, 500);
b.LineTo(250, 250);
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));
gstate.SetFillColorSpace(ColorSpace.CreatePattern());
gstate.SetFillColor(CreateAxialShading(doc));
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");
soft_dict = doc.CreateIndirectDict();
soft_dict.PutName("S", "Luminosity");
//soft_dict.PutName("S", "Alpha");
soft_dict.PutName("Type", "Mask");
soft_dict.Put("G", xobject);
}
}
return soft_dict;
}
But I cant get the soft mask to work with either the pattern gradient, and if I try with a solid alpha the grey box used to create the alpha is visible through the linear gradient.
Please provide a link to a minimal sample where the issue is reproducible: