Reputation: 311
I am trying to place image in PdfPCell using ItextSharp. I want to place the image in the vert/horiz center and that the image will fit center crop (to hide the rest of the image) the only methods i was found is ScaleAbsolute which strech the image and its looking bad and the other is ScaleToFit Which dont fill the all space of the cell. Is there some way to display the image in cell (like overflor:hidden html)
@mkl Hello. i try your solution and.... WOWW its look like its in the right way to work great. the only thing is that the code working great with portrait images and almost good with landscape images. There is something i can do to fix that? (its only happen with landscape image on square area, the code working great on with or height areas
Upvotes: 0
Views: 827
Reputation: 95908
As Bruno already mentioned in a comment, you have to use cell events for custom scaling and clipping tasks.
I.e. the cell initially is "layouted" without the image, and when that is finished, the cell event is triggered with the position and dimension of the cell and the table canvases. Now you can draw the image however you want, scaling (or rotating, or skewing, ...) and clipping it arbitrarily.
This of course does imply that you do have to make sure that the table cell does not collapse (after all it is empty during "layouting"), e.g. by setting a FixedHeight
or else by neighboring cells enforcing a desired height.
For example, if you take this image:
and want to put it into a cell which is four times as wide as it is high, in a full-width, single-column table, applying the scaling and clipping you described, you can do that like this (assuming document
is your Document
instance):
Image itextImage = RETRIEVE YOUR IMAGE AS ITEXTSHARP IMAGE;
PdfPCell cell = new PdfPCell()
{
FixedHeight = (document.PageSize.Width - document.LeftMargin - document.RightMargin) / 4,
CellEvent = new ImageEvent(itextImage)
};
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100;
table.AddCell("Above the image");
table.AddCell(cell);
table.AddCell("Below the image");
document.Add(table);
with the cell event helper class
class ImageEvent : IPdfPCellEvent
{
Image image;
public ImageEvent(Image image)
{
this.image = image;
}
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
{
PdfContentByte canvas = canvases[0];
float scaleX = position.Width / image.Width;
float scaleY = position.Height / image.Height;
float scale = Math.Max(scaleX, scaleY);
image.ScaleToFit(image.Width * scale, image.Height * scale);
image.SetAbsolutePosition((position.Left + position.Right - image.ScaledWidth) / 2, (position.Bottom + position.Top - image.ScaledHeight) / 2);
canvas.SaveState();
canvas.Rectangle(position.Left, position.Bottom, position.Width, position.Height);
canvas.Clip();
canvas.NewPath();
canvas.AddImage(image);
canvas.RestoreState();
}
}
(As you see, the scale
factor is chosen as the maximum of scaleX
and scaleY
for your desired layout. If you choose the minimum instead, the result would be the ScaleToFit
version of your original example. Or if you choose a value a bit higher than the maximum, you zoom even more into the center of your image.)
and the result looks like this:
Upvotes: 1