How to set line width on template clipping

邮差的信 提交于 2019-12-25 12:40:54

问题


I have some PdfTemplate and I want to clip its shape to some 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? Half-measures, like resizing template will not do the trick.

Piece of 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() obviously doesn't work. Both C# and Java answers will help.

Edit: In this scenario we have img in triangle. What if we want to clip this image like this, but without changing coordinates (I would like to set line width on 10):

template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);

回答1:


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.

  1. To cut out a shape when adding an Image.
  2. 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!

This is also strange:

template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);

This doesn't draw a triangle!

These lines should be corrected like this:

template.MoveTo(0, 45);
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);


来源:https://stackoverflow.com/questions/32821326/how-to-set-line-width-on-template-clipping

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!