0

I need to cut a .jpg image from top of 20 pixels.

I have this code to associate the image to the object:

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);

Thanks

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
Ale
  • 63
  • 1
  • 9

1 Answers1

-1

Please take a look at the answer to the question How to give an image rounded corners?

In this example, we clip an image so that it gets rounded corners:

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);

You want to remove part of the top of the image, you can make your code even simpler:

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);

In this example, I created a PdfTemplate that is 20 user units smaller than the original image. I add the original image to that template, and I create a new image from that template.

Important: this visually clips the image in the sense that you won't see the top strip (20 user units high) in the PDF. However, if you extract the image from the PDF, you'll see that the complete image is present in the document.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165