Opacity of rectangle objects/z-index

Hi,


I am trying to draw a semi-opaque (50% transparent) rectangle over a PDF document. From the documentation it says “z-index can be negative. Graph with negative Zindex will be placed behind the text of the page.”

When i create the graph, and set the Zindex to a negative value, it does not place it behind the text and i end up with something like the image below:

Imgur: The magic of the Internet

An alternative thing which would work is if i was able to set the opacity of the rectangle drawn. Either way would work in this scenario. My code is below:

var pg = doc.Pages[page];
var canvas = new Graph((float)pg.PageInfo.Width, (float)pg.PageInfo.Height);

canvas.ZIndex = -1000;

// set page margin on all sides as 0
pg.PageInfo.Margin.Left = pg.PageInfo.Margin.Right = pg.PageInfo.Margin.Bottom = pg.PageInfo.Margin.Top = 0;

pg.Paragraphs.Add(canvas);



Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(rekt.x,
(float)((pg.PageInfo.Height - rekt.y) - rekt.height), rekt.width, rekt.height);


var c = ColorTranslator.FromHtml(mColor); // mcolor is #FFFF00 at the moment

rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(c);

canvas.Shapes.Add(rect);


Basically, I have an array of points which i can draw rectangles over, or overlay images, etc. when rendering/saving the PDF. If there is an alternative way to do this could you please let me know?

Thanks!

Hi Jonathan,

Thanks for contacting support.

In order to fill rectangle with transparent color, you can use Color.FromArgb() and add required values as per your requirement. Please check following sample code snippet to make rectangle object transparent, so that the text under it can be visible as well.

int alpha = 10;

int green = 0;

int red = 100;

int blue = 0;

Color alphaColor = Color.FromArgb(alpha, red, green, blue);

Document doc = new Document();

doc.Pages.Add();

var pg = doc.Pages[1];

var tf = new TextFragment("some text on page");

pg.Paragraphs.Add(tf);

var canvas = new Drawing.Graph((float)pg.PageInfo.Width, (float)pg.PageInfo.Height);

pg.PageInfo.Margin.Left = pg.PageInfo.Margin.Right = pg.PageInfo.Margin.Bottom = pg.PageInfo.Margin.Top = 0;

pg.Paragraphs.Add(canvas);

Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(0, 700, 100, 750);

var c = ColorTranslator.FromHtml("#FFFF00");

rect.GraphInfo.Color = Aspose.Pdf.Color.FromRgb(c);

rect.GraphInfo.FillColor = alphaColor;

canvas.Shapes.Add(rect);

doc.Save(dataDir + "RectangleGraph.pdf");

I have also attached output, generated by the above code for your reference. In case of any further assistance, please feel free to contact us.

Best Regards,

Hi,


With some changing around slightly the above is working perfectly now, thanks. Just out of interest, would it be possible to do this with a z-index as mentioned above, or was i going wrong there?

Thanks for the quick reply.

Hi Jonathan,


Thanks for writing back.

It is good to know that suggested code has worked out for you.

jonathan.murray:
Just out of interest, would it be possible to do this with a z-index as mentioned above, or was i going wrong there?

Please note that the property z-index is used to specify the order (i.e which object should be placed at which order). Greater the z-index an element/object has, more it will be above the objects with lower z-index. Nevertheless, to apply opacity, you need to set color with transparency as you have achieved recently.

For more information regarding z-index, please check “Controlling Z-Order of Rectangle and Text With Transparency” blog post on our website which demonstrate a good example of z-index. Please keep using our API, and in case of any further assistance, please feel free to contact us.


Best Regards,