Java Code Examples for com.lowagie.text.Image#RIGHT

The following examples show how to use com.lowagie.text.Image#RIGHT . 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: 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 4
Source File: ElementFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates an Image object based on a list of properties.
 * 
 * @param attributes
 * @return an Image
 */
public static Image getImage(Properties attributes) throws BadElementException, MalformedURLException, IOException {
	String value;

	value = attributes.getProperty(ElementTags.URL);
	if (value == null) {
		throw new MalformedURLException("The URL of the image is missing.");
	}
	Image image = Image.getInstance(value);

	value = attributes.getProperty(ElementTags.ALIGN);
	int align = 0;
	if (value != null) {
		if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value)) {
			align |= Image.LEFT;
		} else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value)) {
			align |= Image.RIGHT;
		} else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value)) {
			align |= Image.MIDDLE;
		}
	}
	if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.UNDERLYING))) {
		align |= Image.UNDERLYING;
	}
	if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.TEXTWRAP))) {
		align |= Image.TEXTWRAP;
	}
	image.setAlignment(align);

	value = attributes.getProperty(ElementTags.ALT);
	if (value != null) {
		image.setAlt(value);
	}

	String x = attributes.getProperty(ElementTags.ABSOLUTEX);
	String y = attributes.getProperty(ElementTags.ABSOLUTEY);
	if (x != null && y != null) {
		image.setAbsolutePosition(Float.parseFloat(x + "f"), Float.parseFloat(y + "f"));
	}
	value = attributes.getProperty(ElementTags.PLAINWIDTH);
	if (value != null) {
		image.scaleAbsoluteWidth(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.PLAINHEIGHT);
	if (value != null) {
		image.scaleAbsoluteHeight(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.ROTATION);
	if (value != null) {
		image.setRotation(Float.parseFloat(value + "f"));
	}
	return image;
}
 
Example 5
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 6
Source File: ElementFactory.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates an Image object based on a list of properties.
 * @param attributes
 * @return an Image
 */
public static Image getImage(Properties attributes)
		throws BadElementException, MalformedURLException, IOException {
	String value;

	value = attributes.getProperty(ElementTags.URL);
	if (value == null)
		throw new MalformedURLException("The URL of the image is missing.");
	Image image = Image.getInstance(value);

	value = attributes.getProperty(ElementTags.ALIGN);
	int align = 0;
	if (value != null) {
		if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value))
			align |= Image.LEFT;
		else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value))
			align |= Image.RIGHT;
		else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value))
			align |= Image.MIDDLE;
	}
	if ("true".equalsIgnoreCase(attributes
			.getProperty(ElementTags.UNDERLYING)))
		align |= Image.UNDERLYING;
	if ("true".equalsIgnoreCase(attributes
			.getProperty(ElementTags.TEXTWRAP)))
		align |= Image.TEXTWRAP;
	image.setAlignment(align);

	value = attributes.getProperty(ElementTags.ALT);
	if (value != null) {
		image.setAlt(value);
	}

	String x = attributes.getProperty(ElementTags.ABSOLUTEX);
	String y = attributes.getProperty(ElementTags.ABSOLUTEY);
	if ((x != null) && (y != null)) {
		image.setAbsolutePosition(Float.parseFloat(x + "f"), Float
				.parseFloat(y + "f"));
	}
	value = attributes.getProperty(ElementTags.PLAINWIDTH);
	if (value != null) {
		image.scaleAbsoluteWidth(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.PLAINHEIGHT);
	if (value != null) {
		image.scaleAbsoluteHeight(Float.parseFloat(value + "f"));
	}
	value = attributes.getProperty(ElementTags.ROTATION);
	if (value != null) {
		image.setRotation(Float.parseFloat(value + "f"));
	}
	return image;
}
 
Example 7
Source File: PdfDocument.java    From MesquiteCore 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
 */

private void add(Image image) throws PdfException, DocumentException {
    
    if (image.hasAbsolutePosition()) {
        addImage(graphics, image, 0, 0, 0, 0, 0, 0);
        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.scaledHeight() < indentBottom()) {
        if (!strictImageSequence && imageWait == null) {
            imageWait = image;
            return;
        }
        newPage();
        if (currentHeight != 0 && indentTop() - currentHeight - image.scaledHeight() < indentBottom()) {
            imageWait = image;
            return;
        }
    }
    pageEmpty = false;
    // avoid endless loops
    if (image == imageWait)
        imageWait = null;
    boolean textwrap = (image.alignment() & Image.TEXTWRAP) == Image.TEXTWRAP
    && !((image.alignment() & Image.MIDDLE) == Image.MIDDLE);
    boolean underlying = (image.alignment() & Image.UNDERLYING) == Image.UNDERLYING;
    float diff = leading / 2;
    if (textwrap) {
        diff += leading;
    }
    float lowerleft = indentTop() - currentHeight - image.scaledHeight() -diff;
    float mt[] = image.matrix();
    float startPosition = indentLeft() - mt[4];
    if ((image.alignment() & Image.RIGHT) == Image.RIGHT) startPosition = indentRight() - image.scaledWidth() - mt[4];
    if ((image.alignment() & Image.MIDDLE) == Image.MIDDLE) startPosition = indentLeft() + ((indentRight() - indentLeft() - image.scaledWidth()) / 2) - mt[4];
    if (image.hasAbsoluteX()) startPosition = image.absoluteX();
    addImage(graphics, image, mt[0], mt[1], mt[2], mt[3], startPosition, lowerleft - mt[5]);
    if (textwrap) {
        if (imageEnd < 0 || imageEnd < currentHeight + image.scaledHeight() + diff) {
            imageEnd = currentHeight + image.scaledHeight() + diff;
        }
        if ((image.alignment() & Image.RIGHT) == Image.RIGHT) {
        	// indentation suggested by Pelikan Stephan
            imageIndentRight += image.scaledWidth() + image.indentationLeft();
        }
        else {
        	// indentation suggested by Pelikan Stephan
            imageIndentLeft += image.scaledWidth() + image.indentationRight();
        }
    }
    if (!(textwrap || underlying)) {
        currentHeight += image.scaledHeight() + diff;
        flushLines();
        text.moveText(0, - (image.scaledHeight() + diff));
        newLine();
    }
}