Java Code Examples for com.lowagie.text.Image#scaleToFit()

The following examples show how to use com.lowagie.text.Image#scaleToFit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PdfCell.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Adds an image to this Cell.
 *
 * @param i the image to add
 * @param left the left border
 * @param right the right border
 * @param extraHeight extra height to add above image
 * @param alignment horizontal alignment (constant from Element class)
 * @return the height of the image
 */

private float addImage(Image i, float left, float right, float extraHeight, int alignment) {
	Image image = Image.getInstance(i);
	if (image.getScaledWidth() > right - left) {
		image.scaleToFit(right - left, Float.MAX_VALUE);
	}
	flushCurrentLine();
	if (line == null) {
		line = new PdfLine(left, right, alignment, leading);
	}
	PdfLine imageLine = line;

	// left and right in chunk is relative to the start of the line
	right = right - left;
	left = 0f;

	if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
		left = right - image.getScaledWidth();
	} else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
		left = left + (right - left - image.getScaledWidth()) / 2f;
	}
	Chunk imageChunk = new Chunk(image, left, 0);
	imageLine.add(new PdfChunk(imageChunk, null));
	addLine(imageLine);
	return imageLine.height();
}
 
Example 2
Source File: PdfCell.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Adds an image to this Cell.
 *
 * @param i           the image to add
 * @param left        the left border
 * @param right       the right border
 * @param extraHeight extra height to add above image
 * @param alignment   horizontal alignment (constant from Element class)
 * @return the height of the image
 */

private float addImage(Image i, float left, float right, float extraHeight, int alignment) {
    Image image = Image.getInstance(i);
    if (image.getScaledWidth() > right - left) {
        image.scaleToFit(right - left, Float.MAX_VALUE);
    }
    flushCurrentLine();
    if (line == null) {
        line = new PdfLine(left, right, alignment, leading);
    }
    PdfLine imageLine = line;

    // left and right in chunk is relative to the start of the line
    right = right - left;
    left = 0f;

    if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
        left = right - image.getScaledWidth();
    } else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
        left = left + ((right - left - image.getScaledWidth()) / 2f);
    }
    Chunk imageChunk = new Chunk(image, left, 0);
    imageLine.add(new PdfChunk(imageChunk, null));
    addLine(imageLine);
    return imageLine.height();
}
 
Example 3
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
private InternalImageProcessorResult processImageRetainShape(String rendererId, DataRenderable renderer) throws JRException
{
	Image image = null;
	
	if (printImage.isUsingCache() && loadedImagesMap.containsKey(rendererId))
	{
		image = loadedImagesMap.get(rendererId);
	}
	else
	{
		try
		{
			image = Image.getInstance(renderer.getData(jasperReportsContext));
			imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
		}
		catch (Exception e)
		{
			throw new JRException(e);
		}

		if (printImage.isUsingCache())
		{
			loadedImagesMap.put(rendererId, image);
		}
	}

	int xoffset = 0;
	int yoffset = 0;

	image.setRotationDegrees(0); // reset in case the image is from cache
	
	switch (printImage.getRotation())
	{
		case LEFT :
		{
			image.scaleToFit(availableImageHeight, availableImageWidth);
			image.setRotationDegrees(90);
			xoffset = (int)(ImageUtil.getYAlignFactor(printImage) * (availableImageWidth - image.getPlainHeight()));
			yoffset = (int)((1f - ImageUtil.getXAlignFactor(printImage)) * (availableImageHeight - image.getPlainWidth()));
			break;
		}
		case RIGHT :
		{
			image.scaleToFit(availableImageHeight, availableImageWidth);
			image.setRotationDegrees(-90);
			xoffset = (int)((1f - ImageUtil.getYAlignFactor(printImage)) * (availableImageWidth - image.getPlainHeight()));
			yoffset = (int)(ImageUtil.getXAlignFactor(printImage) * (availableImageHeight - image.getPlainWidth()));
			break;
		}
		case UPSIDE_DOWN :
		{
			image.scaleToFit(availableImageWidth, availableImageHeight);
			image.setRotationDegrees(180);
			xoffset = (int)((1f - ImageUtil.getXAlignFactor(printImage)) * (availableImageWidth - image.getPlainWidth()));
			yoffset = (int)((1f - ImageUtil.getYAlignFactor(printImage)) * (availableImageHeight - image.getPlainHeight()));
			break;
		}
		case NONE :
		default :
		{
			image.scaleToFit(availableImageWidth, availableImageHeight);
			xoffset = (int)(ImageUtil.getXAlignFactor(printImage) * (availableImageWidth - image.getPlainWidth()));
			yoffset = (int)(ImageUtil.getYAlignFactor(printImage) * (availableImageHeight - image.getPlainHeight()));
		}
	}
	
	xoffset = (xoffset < 0 ? 0 : xoffset);
	yoffset = (yoffset < 0 ? 0 : yoffset);
	
	return 
		new InternalImageProcessorResult(
			new Chunk(image, 0, 0), 
			image.getScaledWidth(), 
			image.getScaledHeight(),
			xoffset,
			yoffset
			);
}