How to set the line width of a clipping path?
I have a PdfTemplate
and I want to clip its shape to a specific path. I know how to do this, but the clipping line is always the same (probably 1 px) and I want to be able to change it. Is there any way to do this?
This is my code:
PdfTemplate template = contentByte.CreateTemplate(100, 200); template.MoveTo(0, 0); template.LineTo(50, 50); template.LineTo(50, 0); template.LineTo(0, 50); template.SetLineWidth(5); template.Clip(); Image img = Image.getInstance(RESOURCE); template.Add(img, 0, 0);
SetLineWidth()
doesn't work.
Posted on StackOverflow on Sep 28, 2015 by Piotr M
Problem #1: You never stroke the path, hence it is never drawn. First try this:
PdfTemplate template = contentByte.CreateTemplate(100, 200);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.SetLineWidth(5);
template.Clip();
Image img = Image.getInstance(RESOURCE);
template.Add(img, 0, 0);
template.Stroke();
Problem #2: You are using your clipping path for two different purposes.
-
To cut out a shape when adding an
Image
. -
To draw the path.
That doesn't look right. I'm not sure if every PDF viewer will actually stroke that path as you clearly use that path to clip content.
I would write this code like this:
PdfTemplate template = contentByte.CreateTemplate(100, 200);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.Clip();
template.NewPath();
Image img = Image.getInstance(RESOURCE);
template.Add(img, 0, 0);
template.MoveTo(0, 0);
template.LineTo(50, 50);
template.LineTo(50, 0);
template.LineTo(0, 50);
template.SetLineWidth(5);
template.Stroke();
The first time, you use the path as a clipping path. It doesn't make sense to define a line width for a clipping path: the path defines the shape that needs to be cut out.
The second time, you use the path to stroke the borders of a shape. The lines that make these borders have a width. Note that you're only drawing three lines. You may want to close the path!