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

The following examples show how to use com.lowagie.text.Image#getScaledHeight() . 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
/**
 * Returns the height needed to draw the remaining text.
 *
 * @return a height
 */

public float remainingHeight() {
	float result = 0f;
	for (Iterator i = images.iterator(); i.hasNext();) {
		Image image = (Image) i.next();
		result += image.getScaledHeight();
	}
	return remainingLinesHeight() + cellspacing + 2 * cellpadding + result;
}
 
Example 2
Source File: PdfCell.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the height needed to draw the remaining text.
 *
 * @return a height
 */

public float remainingHeight() {
    float result = 0f;
    for (Iterator i = images.iterator(); i.hasNext();) {
        Image image = (Image) i.next();
        result += image.getScaledHeight();
    }
    return remainingLinesHeight() + cellspacing + 2 * cellpadding + result;
}
 
Example 3
Source File: OddEvenTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Combines 2 tiff-files into 1 PDF (similar to tiffmesh).
 * 
 * @param args
 *            [0] the file with the odd pages [1] the file with the even
 *            pages [2] the resulting file
 */
public void main(String... args) throws Exception {
	if (args.length < 3) {
		System.err.println("OddEven needs 3 Arguments.");
		System.out
				.println("Usage: com.lowagie.examples.objects.images.tiff.OddEven odd_file.tif even_file.tif combined_file.pdf");
		return;
	}
	RandomAccessFileOrArray odd = new RandomAccessFileOrArray(args[0]);
	RandomAccessFileOrArray even = new RandomAccessFileOrArray(args[1]);
	Image img = TiffImage.getTiffImage(odd, 1);
	Document document = new Document(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(args[2]));
	document.open();
	PdfContentByte cb = writer.getDirectContent();
	int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage.getNumberOfPages(even));
	for (int c = 0; c < count; ++c) {

		Image imgOdd = TiffImage.getTiffImage(odd, c + 1);
		Image imgEven = TiffImage.getTiffImage(even, count - c);
		document.setPageSize(new Rectangle(imgOdd.getScaledWidth(), imgOdd.getScaledHeight()));
		document.newPage();
		imgOdd.setAbsolutePosition(0, 0);
		cb.addImage(imgOdd);
		document.setPageSize(new Rectangle(imgEven.getScaledWidth(), imgEven.getScaledHeight()));
		document.newPage();
		imgEven.setAbsolutePosition(0, 0);
		cb.addImage(imgEven);

	}
	odd.close();
	even.close();
	document.close();

}
 
Example 4
Source File: PdfPCell.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Returns the height of the cell.
 * @return	the height of the cell
 * @since	3.0.0
 */
public float getMaxHeight() {
	boolean pivoted = (getRotation() == 90 || getRotation() == 270);
	Image img = getImage();
	if (img != null) {
		img.scalePercent(100);
		float refWidth = pivoted ? img.getScaledHeight() : img.getScaledWidth();
		float scale = (getRight() - getEffectivePaddingRight()
                   - getEffectivePaddingLeft() - getLeft()) / refWidth;
		img.scalePercent(scale * 100);
		float refHeight = pivoted ? img.getScaledWidth() : img.getScaledHeight();
		setBottom(getTop() - getEffectivePaddingTop() - getEffectivePaddingBottom() - refHeight);
	}
	else {
		if (pivoted && hasFixedHeight())
			setBottom(getTop() - getFixedHeight());
		else {
			ColumnText ct = ColumnText.duplicate(getColumn());
			float right, top, left, bottom;
			if (pivoted) {
				right = PdfPRow.RIGHT_LIMIT;
				top = getRight() - getEffectivePaddingRight();
				left = 0;
				bottom = getLeft() + getEffectivePaddingLeft();
			}
			else {
				right = isNoWrap() ? PdfPRow.RIGHT_LIMIT : getRight() - getEffectivePaddingRight();
				top = getTop() - getEffectivePaddingTop();
				left = getLeft() + getEffectivePaddingLeft();
				bottom = hasFixedHeight() ? top + getEffectivePaddingBottom() - getFixedHeight() : PdfPRow.BOTTOM_LIMIT;
			}
			PdfPRow.setColumn(ct, left, bottom, right, top);
			try {
				ct.go(true);
			} catch (DocumentException e) {
				throw new ExceptionConverter(e);
			}
			if (pivoted)
				setBottom(getTop() - getEffectivePaddingTop() - getEffectivePaddingBottom() - ct.getFilledWidth());
			else {
				float yLine = ct.getYLine();
				if (isUseDescender())
					yLine += ct.getDescender();
				setBottom(yLine - getEffectivePaddingBottom());
			}
		}
	}
	float height = getHeight();
	if (height < getFixedHeight())
		height = getFixedHeight();
	else if (height < getMinimumHeight())
		height = getMinimumHeight();
	return height;
}
 
Example 5
Source File: PdfDocument.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Adds an image to the document.
 *
 * @param image the <CODE>Image</CODE> to add
 * @throws PdfException on error
 * @throws DocumentException on error
 */

protected void add(Image image) throws PdfException, DocumentException {

	if (image.hasAbsoluteY()) {
		graphics.addImage(image);
		pageEmpty = false;
		return;
	}

	// if there isn't enough room for the image on this page, save it for the next page
	if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
		if (!strictImageSequence && imageWait == null) {
			imageWait = image;
			return;
		}
		newPage();
		if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
			imageWait = image;
			return;
		}
	}
	pageEmpty = false;
	// avoid endless loops
	if (image == imageWait) {
		imageWait = null;
	}
	boolean textwrap = (image.getAlignment() & Image.TEXTWRAP) == Image.TEXTWRAP && !((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE);
	boolean underlying = (image.getAlignment() & Image.UNDERLYING) == Image.UNDERLYING;
	float diff = leading / 2;
	if (textwrap) {
		diff += leading;
	}
	float lowerleft = indentTop() - currentHeight - image.getScaledHeight() - diff;
	float mt[] = image.matrix();
	float startPosition = indentLeft() - mt[4];
	if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
		startPosition = indentRight() - image.getScaledWidth() - mt[4];
	}
	if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
		startPosition = indentLeft() + (indentRight() - indentLeft() - image.getScaledWidth()) / 2 - mt[4];
	}
	if (image.hasAbsoluteX()) {
		startPosition = image.getAbsoluteX();
	}
	if (textwrap) {
		if (imageEnd < 0 || imageEnd < currentHeight + image.getScaledHeight() + diff) {
			imageEnd = currentHeight + image.getScaledHeight() + diff;
		}
		if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
			// indentation suggested by Pelikan Stephan
			indentation.imageIndentRight += image.getScaledWidth() + image.getIndentationLeft();
		} else {
			// indentation suggested by Pelikan Stephan
			indentation.imageIndentLeft += image.getScaledWidth() + image.getIndentationRight();
		}
	} else {
		if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
			startPosition -= image.getIndentationRight();
		} else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
			startPosition += image.getIndentationLeft() - image.getIndentationRight();
		} else {
			startPosition += image.getIndentationLeft();
		}
	}
	graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], startPosition, lowerleft - mt[5]);
	if (!(textwrap || underlying)) {
		currentHeight += image.getScaledHeight() + diff;
		flushLines();
		text.moveText(0, -(image.getScaledHeight() + diff));
		newLine();
	}
}
 
Example 6
Source File: PdfPCell.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Returns the height of the cell.
 * @return	the height of the cell
 * @since	3.0.0
 */
public float getMaxHeight() {
	boolean pivoted = (getRotation() == 90 || getRotation() == 270);
	Image img = getImage();
	if (img != null) {
		img.scalePercent(100);
		float refWidth = pivoted ? img.getScaledHeight() : img.getScaledWidth();
		float scale = (getRight() - getEffectivePaddingRight()
                   - getEffectivePaddingLeft() - getLeft()) / refWidth;
		img.scalePercent(scale * 100);
		float refHeight = pivoted ? img.getScaledWidth() : img.getScaledHeight();
		setBottom(getTop() - getEffectivePaddingTop() - getEffectivePaddingBottom() - refHeight);
	}
	else {
		if ((pivoted && hasFixedHeight()) || getColumn() == null)
			setBottom(getTop() - getFixedHeight());
		else {
			ColumnText ct = ColumnText.duplicate(getColumn());
			float right, top, left, bottom;
			if (pivoted) {
				right = PdfPRow.RIGHT_LIMIT;
				top = getRight() - getEffectivePaddingRight();
				left = 0;
				bottom = getLeft() + getEffectivePaddingLeft();
			}
			else {
				right = isNoWrap() ? PdfPRow.RIGHT_LIMIT : getRight() - getEffectivePaddingRight();
				top = getTop() - getEffectivePaddingTop();
				left = getLeft() + getEffectivePaddingLeft();
				bottom = hasFixedHeight() ? getTop() + getEffectivePaddingBottom() - getFixedHeight() : PdfPRow.BOTTOM_LIMIT;
			}
			PdfPRow.setColumn(ct, left, bottom, right, top);
			try {
				ct.go(true);
			} catch (DocumentException e) {
				throw new ExceptionConverter(e);
			}
			if (pivoted)
				setBottom(getTop() - getEffectivePaddingTop() - getEffectivePaddingBottom() - ct.getFilledWidth());
			else {
				float yLine = ct.getYLine();
				if (isUseDescender())
					yLine += ct.getDescender();
				setBottom(yLine - getEffectivePaddingBottom());
			}
		}
	}
	float height = getHeight();
	if (hasFixedHeight())
		height = getFixedHeight();
	else if (height < getMinimumHeight())
		height = getMinimumHeight();
	return height;
}
 
Example 7
Source File: PdfDocument.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adds an image to the document.
 * @param image the <CODE>Image</CODE> to add
 * @throws PdfException on error
 * @throws DocumentException on error
 */

protected void add(Image image) throws PdfException, DocumentException {

    if (image.hasAbsoluteY()) {
        graphics.addImage(image);
        pageEmpty = false;
        return;
    }

    // if there isn't enough room for the image on this page, save it for the next page
    if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
        if (!strictImageSequence && imageWait == null) {
            imageWait = image;
            return;
        }
        newPage();
        if (currentHeight != 0 && indentTop() - currentHeight - image.getScaledHeight() < indentBottom()) {
            imageWait = image;
            return;
        }
    }
    pageEmpty = false;
    // avoid endless loops
    if (image == imageWait)
        imageWait = null;
    boolean textwrap = (image.getAlignment() & Image.TEXTWRAP) == Image.TEXTWRAP
    && !((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE);
    boolean underlying = (image.getAlignment() & Image.UNDERLYING) == Image.UNDERLYING;
    float diff = leading / 2;
    if (textwrap) {
        diff += leading;
    }
    float lowerleft = indentTop() - currentHeight - image.getScaledHeight() -diff;
    float mt[] = image.matrix();
    float startPosition = indentLeft() - mt[4];
    if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) startPosition = indentRight() - image.getScaledWidth() - mt[4];
    if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) startPosition = indentLeft() + ((indentRight() - indentLeft() - image.getScaledWidth()) / 2) - mt[4];
    if (image.hasAbsoluteX()) startPosition = image.getAbsoluteX();
    if (textwrap) {
        if (imageEnd < 0 || imageEnd < currentHeight + image.getScaledHeight() + diff) {
            imageEnd = currentHeight + image.getScaledHeight() + diff;
        }
        if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
        	// indentation suggested by Pelikan Stephan
        	indentation.imageIndentRight += image.getScaledWidth() + image.getIndentationLeft();
        }
        else {
        	// indentation suggested by Pelikan Stephan
        	indentation.imageIndentLeft += image.getScaledWidth() + image.getIndentationRight();
        }
    }
    else {
    	if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) startPosition -= image.getIndentationRight();
    	else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) startPosition += image.getIndentationLeft() - image.getIndentationRight();
    	else startPosition += image.getIndentationLeft();
    }
    graphics.addImage(image, mt[0], mt[1], mt[2], mt[3], startPosition, lowerleft - mt[5]);
    if (!(textwrap || underlying)) {
        currentHeight += image.getScaledHeight() + diff;
        flushLines();
        text.moveText(0, - (image.getScaledHeight() + diff));
        newLine();
    }
}
 
Example 8
Source File: JRPdfExporter.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
private InternalImageProcessorResult processImageFillFrame(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);
		}
	}

	switch (printImage.getRotation())
	{
		case LEFT :
		{
			image.scaleAbsolute(availableImageHeight, availableImageWidth);
			image.setRotationDegrees(90);
			break;
		}
		case RIGHT :
		{
			image.scaleAbsolute(availableImageHeight, availableImageWidth);
			image.setRotationDegrees(-90);
			break;
		}
		case UPSIDE_DOWN :
		{
			image.scaleAbsolute(availableImageWidth, availableImageHeight);
			image.setRotationDegrees(180);
			break;
		}
		case NONE :
		default :
		{
			image.scaleAbsolute(availableImageWidth, availableImageHeight);
		}
	}
	
	return 
		new InternalImageProcessorResult(
			new Chunk(image, 0, 0), 
			image.getScaledWidth(), 
			image.getScaledHeight(),
			0,
			0
			);
}
 
Example 9
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
			);
}