从顶部剪切jpg iTextSharp.text.image



我需要从 20 像素的顶部剪切.jpg图像。

我有以下代码将图像与对象相关联:

iTextSharp.text.Rectangle rect = pdf.AcroFields.GetFieldPositions("BarCode")[0].position;
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Application.StartupPath + segnacollo.BarCode);
img.ScaleAbsoluteHeight(rect.Height);
img.ScaleAbsoluteWidth(rect.Width);
img.SetAbsolutePosition(rect.Left, rect.Bottom);
pdf.GetOverContent(1).AddImage(img);

谢谢

请看一下问题的答案 如何给图像圆角?

在此示例中,我们剪裁图像,使其变圆角:

Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h);
t.Ellipse(0, 0, w, h);
t.Clip();
t.NewPath();
t.AddImage(img, w, 0, 0, h, 0, -600);
Image clipped = Image.GetInstance(t);

你想要删除图像顶部的一部分,你可以使你的代码更简单:

Image img = Image.GetInstance(some_path_to_an_image);
float w = img.ScaledWidth;
float h = img.ScaledHeight;
PdfTemplate t = writer.DirectContent.CreateTemplate(w, h - 20);
t.AddImage(img, w, 0, 0, h, 0, 0);
Image clipped = Image.GetInstance(t);

在此示例中,我创建了一个比原始图像小 20 个用户单位的PdfTemplate。我将原始图像添加到该模板,并从该模板创建新图像。

重要提示:这会在视觉上剪切图像,因为您不会在 PDF 中看到顶部条带(20 个用户单位高)。但是,如果您从 PDF 中提取图像,您将看到文档中存在完整的图像。

相关内容

最新更新