如何在模板剪裁上设置线宽 [英] How to set line width on template clipping
问题描述
我有一些 PdfTemplate
,我想将其形状剪辑到某个路径。我知道怎么做,但剪切线总是相同的(可能是1像素),我希望能够改变它。有没有办法做到这一点?像调整大小模板这样的一半措施将无法解决问题。
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.
一段代码:
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()
显然不起作用。 C#和Java答案都有帮助。
SetLineWidth()
obviously doesn't work. Both C# and Java answers will help.
编辑:在这种情况下,我们有三角形img。如果我们想要像这样剪辑这个图像但不改变坐标(我想在10上设置线宽)该怎么办:
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();
问题#2:您正在将剪切路径用于两个不同的目的。
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.
看起来不对。我不确定每个PDF查看器是否会实际描绘该路径,因为您明确使用该路径来剪辑内容。
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!
这也很奇怪:
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);
这不会绘制三角形!
这些行应该像这样更正:
These lines should be corrected like this:
template.MoveTo(0, 45);
template.LineTo(45, 45);
template.LineTo(45, 0);
template.LineTo(0, 45);
这篇关于如何在模板剪裁上设置线宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!