Java Code Examples for com.lowagie.text.Rectangle#getHeight()

The following examples show how to use com.lowagie.text.Rectangle#getHeight() . 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: RtfPageSetting.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Set the page size to use. This method will use guessFormat to try to guess the correct
 * page format. If no format could be guessed, the sizes from the pageSize are used and
 * the landscape setting is determined by comparing width and height;
 * 
 * @param pageSize The pageSize to use
 */
public void setPageSize(Rectangle pageSize) {
    if(!guessFormat(pageSize, false)) {
        this.pageWidth = (int) (pageSize.getWidth() * RtfElement.TWIPS_FACTOR);
        this.pageHeight = (int) (pageSize.getHeight() * RtfElement.TWIPS_FACTOR);
        this.landscape = pageWidth > pageHeight;
    }
}
 
Example 2
Source File: PdfExportHelper.java    From mdw with Apache License 2.0 5 votes vote down vote up
private void printDiagram(DocWriter writer, Chapter chapter, Process process,
        Rectangle pageSize) throws Exception {
    ProcessCanvas canvas = new ProcessCanvas(project, process);
    canvas.prepare();

    Dimension size = new PngProcessExporter(project).getDiagramSize(process);
    // create a template and a Graphics2D object that corresponds with it
    int w;
    int h;
    float scale;
    if ((float) size.width < pageSize.getWidth() * 0.8
            && (float) size.height < pageSize.getHeight() * 0.8) {
        w = size.width + 36;
        h = size.height + 36;
        scale = -1f;
    }
    else {
        scale = pageSize.getWidth() * 0.8f / (float) size.width;
        if (scale > pageSize.getHeight() * 0.8f / (float) size.height)
            scale = pageSize.getHeight() * 0.8f / (float) size.height;
        w = (int) (size.width * scale) + 36;
        h = (int) (size.height * scale) + 36;
    }

    PdfContentByte cb = ((PdfWriter) writer).getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    if (scale > 0)
        g2.scale(scale, scale);
    tp.setWidth(w);
    tp.setHeight(h);
    canvas.paintComponent(g2);
    g2.dispose();
    Image img = new ImgTemplate(tp);
    chapter.add(img);
    canvas.dispose();
}
 
Example 3
Source File: PdfWatermarkUtils.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public static void addWatermark(PdfStamper pdfStamper, BaseFont baseFont, Color fontColor, String waterMarkString) throws Exception {
	if (null == pdfStamper || null == baseFont) {
		throw new java.lang.Exception("PdfStamper or BaseFont is null.");
	}
	if (StringUtils.isBlank(waterMarkString)) {
		return;
	}
	PdfContentByte pdfContentByte = null;
	Rectangle pageRect = null;
	PdfGState pdfGState = new PdfGState();
	// 设置透明度为0.4
	pdfGState.setFillOpacity(0.4f);
	pdfGState.setStrokeOpacity(0.4f);
       int pageNum = pdfStamper.getReader().getNumberOfPages();
       try {
           for (int i = 1; i <= pageNum; i++) {
           	pageRect = pdfStamper.getReader().getPageSizeWithRotation(i);
           	// 计算水印X,Y坐标
           	float x = pageRect.getWidth() / 2;
           	float y = pageRect.getHeight() / 2;
           	//获得PDF最顶层
           	pdfContentByte = pdfStamper.getOverContent(i);
           	pdfContentByte.saveState();
           	// set Transparency
           	pdfContentByte.setGState(pdfGState);
           	pdfContentByte.beginText();
           	pdfContentByte.setColorFill(fontColor);
           	pdfContentByte.setFontAndSize(baseFont, 60);
           	// 水印文字成45度角倾斜
           	pdfContentByte.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x, y, 45);
           	pdfContentByte.endText();             	
           }
       } catch (Exception e) {
       	e.printStackTrace();
       } finally {
       	pdfContentByte = null;
           pageRect = null;        	
       }
}
 
Example 4
Source File: TextOnlySignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void draw() {

	String text = parameters.getTextParameters().getText();
	
	appearance.setRender(PdfSignatureAppearance.SignatureRenderDescription);
	
	if (Utils.isStringNotBlank(signatureFieldId)) {
		appearance.setVisibleSignature(signatureFieldId);

		appearance.setLayer2Font(iTextFont);
		appearance.setLayer2Text(text);
		
	} else {
		Rectangle pageSize = appearance.getStamper().getReader().getPageSize(parameters.getPage());
		float originY = pageSize.getHeight();
		
		ITextFontMetrics iTextFontMetrics = new ITextFontMetrics(iTextFont.getBaseFont());

		int width = parameters.getWidth();
		int height = parameters.getHeight();
		if (width == 0 || height == 0) {
			Dimension dimension = computeDimensions(iTextFontMetrics);
			width = dimension.width;
			height = dimension.height;
		}

		Rectangle rect = new Rectangle(parameters.getxAxis(), originY - parameters.getyAxis() - height, parameters.getxAxis() + width,
				originY - parameters.getyAxis());
		appearance.setVisibleSignature(rect, parameters.getPage()); // defines signature field borders
		
		showText(iTextFontMetrics, rect);
	}

}
 
Example 5
Source File: ItextPDFWatermarkWriter.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
private int getOrientation(PdfContentByte canvas, int pageNumber) {
	Rectangle pageSize = document.getPageSize(pageNumber);
	if (pageSize.getHeight() > pageSize.getWidth()) {
		return PAGE_ORIENTATION_PORTRAIT;
	} else {
		return PAGE_ORIENTATION_LANDSCAPE;
	}
}
 
Example 6
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
private float getBoxArea(Rectangle box) {
	float result = -1;
	if (box != null) {
		result = box.getWidth() * box.getHeight();
	}
	return result;
}
 
Example 7
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Move this rectangle to the specified bottom-left point.
 *
 * @param rect rectangle to move
 * @param x new x origin
 * @param y new y origin
 */
public void moveRectangleTo(Rectangle rect, float x, float y) {
	float width = rect.getWidth();
	float height = rect.getHeight();
	rect.setLeft(x);
	rect.setBottom(y);
	rect.setRight(rect.getLeft() + width);
	rect.setTop(rect.getBottom() + height);
}
 
Example 8
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Translate this rectangle over the specified following distances.
 *
 * @param rect rectangle to move
 * @param dx delta x
 * @param dy delta y
 */
public void translateRectangle(Rectangle rect, float dx, float dy) {
	float width = rect.getWidth();
	float height = rect.getHeight();
	rect.setLeft(rect.getLeft() + dx);
	rect.setBottom(rect.getBottom() + dy);
	rect.setRight(rect.getLeft() + dx + width);
	rect.setTop(rect.getBottom() + dy + height);
}
 
Example 9
Source File: VectorLayerComponentImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private Rectangle calculateLabelRect(PdfContext context, InternalFeature f, String label, Font font) {
	Rectangle textSize = context.getTextSize(label, font);
	float margin = 0.25f * font.getSize();
	Rectangle rect = new Rectangle(textSize.getWidth() + 2 * margin, textSize.getHeight() + 2 * margin);
	Coordinate labelPosition = geoService.calcDefaultLabelPosition(f);
	// SPRINT-53 Labels should be rendered in Screen Space
	new MapToUserFilter().filter(labelPosition);

	context.moveRectangleTo(rect, (float) labelPosition.x - rect.getWidth() / 2f,
			(float) labelPosition.y - rect.getHeight() / 2f);
	if (f.getGeometry() instanceof Point) {
		float shiftHeight = 0.5f * (rect.getHeight() + getSymbolHeight(f));
		// move up 15 pixels to make the symbol visible
		context.moveRectangleTo(rect, rect.getLeft(), rect.getBottom() + shiftHeight + SYMBOL_CONNECT_LENGTH);
	}
	if (rect.getLeft() < 0) {
		context.moveRectangleTo(rect, 10, rect.getBottom());
	}
	if (rect.getBottom() < 0) {
		context.moveRectangleTo(rect, rect.getLeft(), 10);
	}
	if (rect.getTop() > getBounds().getHeight()) {
		context.moveRectangleTo(rect, rect.getLeft(), getBounds().getHeight() - rect.getHeight() - 10);
	}
	if (rect.getRight() > getBounds().getWidth()) {
		context.moveRectangleTo(rect, getBounds().getWidth() - rect.getWidth() - 10, rect.getBottom());
	}
	return rect;
}
 
Example 10
Source File: TwoOnOneTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Reads the pages of an existing PDF file and puts 2 pages from the
 * existing doc into one of the new doc.
 */
@Test
public void main() throws Exception {
	// we create a reader for a certain document
	PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR + "ChapterSection.pdf");
	// we retrieve the total number of pages
	int n = reader.getNumberOfPages();
	// we retrieve the size of the first page
	Rectangle psize = reader.getPageSize(1);
	float width = psize.getHeight();
	float height = psize.getWidth();

	// step 1: creation of a document-object
	Document document = new Document(new Rectangle(width, height));
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("2on1.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add content
	PdfContentByte cb = writer.getDirectContent();
	int i = 0;
	int p = 0;
	while (i < n) {
		document.newPage();
		p++;
		i++;
		PdfImportedPage page1 = writer.getImportedPage(reader, i);
		cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
		if (i < n) {
			i++;
			PdfImportedPage page2 = writer.getImportedPage(reader, i);
			cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
		}
		BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		cb.beginText();
		cb.setFontAndSize(bf, 14);
		cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0 ? 1 : 0)),
				width / 2, 40, 0);
		cb.endText();
	}
	// step 5: we close the document
	document.close();

}
 
Example 11
Source File: TextOnlySignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void showText(ITextFontMetrics iTextFontMetrics, Rectangle sigFieldRect) {
	
	SignatureImageTextParameters textParameters = parameters.getTextParameters();
	String text = textParameters.getText();

	float size = getProperSize();
	
	PdfTemplate layer = appearance.getLayer(2);
	layer.setFontAndSize(iTextFont.getBaseFont(), size);
	
	Rectangle boundingRectangle = new Rectangle(sigFieldRect.getWidth(), sigFieldRect.getHeight()); // defines text field borders
	boundingRectangle.setBackgroundColor(parameters.getTextParameters().getBackgroundColor());
	layer.rectangle(boundingRectangle);
	
	layer.setBoundingBox(boundingRectangle);
	layer.setColorStroke(textParameters.getTextColor());
	
	String[] lines = iTextFontMetrics.getLines(text);
	
	layer.beginText();
	
	float strHeight = iTextFontMetrics.getHeight(lines[0], size);
	float y = boundingRectangle.getHeight() - textParameters.getPadding();
	float x = textParameters.getPadding();
	
	layer.moveText(x, y);
	layer.newlineText();

	y = -strHeight;

       float previousOffset = 0;
	for (String line : lines) {
           float offsetX = 0;
		float lineWidth = iTextFontMetrics.getWidth(line, size);
		switch (textParameters.getSignerTextHorizontalAlignment()) {
			case RIGHT:
				offsetX = boundingRectangle.getWidth() - lineWidth - textParameters.getPadding() * 2 - previousOffset;
				break;
			case CENTER:
				offsetX = (boundingRectangle.getWidth() - lineWidth) / 2 - textParameters.getPadding() - previousOffset;
				break;
			default:
				break;
		}
		previousOffset += offsetX;
		layer.moveText(offsetX, y);
		layer.newlineShowText(line);
	}
	
	layer.endText();
}
 
Example 12
Source File: PdfContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
private float getAbsoluteY(float f, Rectangle rect) {
	return rect.getBottom() + f * rect.getHeight();
}
 
Example 13
Source File: PrintComponentImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
private void layoutChild(PrintComponent<?> child, Rectangle box) {
	LayoutConstraint layoutConstraint = child.getConstraint();
	float bx = box.getLeft();
	float by = box.getBottom();
	float bw = box.getWidth();
	float bh = box.getHeight();
	float cw = child.getBounds().getWidth();
	float ch = child.getBounds().getHeight();
	float marginX = layoutConstraint.getMarginX();
	float marginY = layoutConstraint.getMarginY();
	float absw = layoutConstraint.getWidth();
	float absh = layoutConstraint.getHeight();
	float x = 0;
	float y = 0;
	float w = cw;
	float h = ch;
	switch (layoutConstraint.getAlignmentX()) {
		case LayoutConstraint.LEFT:
			x = bx + marginX;
			break;
		case LayoutConstraint.CENTER:
			x = bx + (bw - cw) / 2;
			break;
		case LayoutConstraint.RIGHT:
			x = bx + bw - marginX - cw;
			break;
		case LayoutConstraint.JUSTIFIED:
			x = bx + marginX;
			w = bw - 2 * marginX;
			break;
		case LayoutConstraint.ABSOLUTE:
			x = marginX;
			w = absw;
			break;
		default:
			throw new IllegalStateException("Unknown X alignment " + layoutConstraint.getAlignmentX());
	}
	switch (layoutConstraint.getAlignmentY()) {
		case LayoutConstraint.BOTTOM:
			y = by + marginY;
			break;
		case LayoutConstraint.CENTER:
			y = by + (bh - ch) / 2;
			break;
		case LayoutConstraint.TOP:
			y = by + bh - marginY - ch;
			break;
		case LayoutConstraint.JUSTIFIED:
			y = by + marginY;
			h = bh - 2 * marginY;
			break;
		case LayoutConstraint.ABSOLUTE:
			y = marginY;
			h = absh;
			break;
		default:
			throw new IllegalStateException("Unknown Y alignment " + layoutConstraint.getAlignmentY());
	}
	child.setBounds(new Rectangle(x, y, x + w, y + h));
}
 
Example 14
Source File: RtfPageSetting.java    From itext2 with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * This method compares to Rectangles. They are considered equal if width and height are the same
 * 
 * @param rect1 The first Rectangle to compare
 * @param rect2 The second Rectangle to compare
 * @return <code>True</code> if the Rectangles equal, <code>false</code> otherwise
 */
private boolean rectEquals(Rectangle rect1, Rectangle rect2) {
    return (rect1.getWidth() == rect2.getWidth()) && (rect1.getHeight() == rect2.getHeight());
}