List of usage examples for com.lowagie.text Rectangle getHeight
public float getHeight()
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
public void fillRoundRectangle(Rectangle rect, Color color, float r) { template.saveState();//w w w . j a v a2s .c o m setFill(color); template.roundRectangle(origX + rect.getLeft(), origY + rect.getBottom(), rect.getWidth(), rect.getHeight(), r); template.fill(); template.restoreState(); }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
/** * Move this rectangle to the specified bottom-left point. * * @param rect rectangle to move//from w ww . j a v a2 s .c o m * @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); }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
/** * Translate this rectangle over the specified following distances. * * @param rect rectangle to move//from ww w .j ava 2s . co m * @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); }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
/** * Draws the specified image with the first rectangle's bounds, clipping with the second one. * * @param img image/*from w w w .j a va 2 s . c o m*/ * @param rect rectangle * @param clipRect clipping bounds * @param opacity opacity of the image (1 = opaque, 0= transparent) */ public void drawImage(Image img, Rectangle rect, Rectangle clipRect, float opacity) { try { template.saveState(); // opacity PdfGState state = new PdfGState(); state.setFillOpacity(opacity); state.setBlendMode(PdfGState.BM_NORMAL); template.setGState(state); // clipping code if (clipRect != null) { template.rectangle(clipRect.getLeft() + origX, clipRect.getBottom() + origY, clipRect.getWidth(), clipRect.getHeight()); template.clip(); template.newPath(); } template.addImage(img, rect.getWidth(), 0, 0, rect.getHeight(), origX + rect.getLeft(), origY + rect.getBottom()); } catch (DocumentException e) { log.warn("could not draw image", e); } finally { template.restoreState(); } }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
private float getAbsoluteY(float f, Rectangle rect) { return rect.getBottom() + f * rect.getHeight(); }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
/** * Draw the specified geometry.//ww w . ja v a2s. c o m * * @param geometry geometry to draw * @param symbol symbol for geometry * @param fillColor fill colour * @param strokeColor stroke colour * @param lineWidth line width * @param clipRect clipping rectangle */ public void drawGeometry(Geometry geometry, SymbolInfo symbol, Color fillColor, Color strokeColor, float lineWidth, float[] dashArray, Rectangle clipRect) { template.saveState(); // clipping code if (clipRect != null) { template.rectangle(clipRect.getLeft() + origX, clipRect.getBottom() + origY, clipRect.getWidth(), clipRect.getHeight()); template.clip(); template.newPath(); } setStroke(strokeColor, lineWidth, dashArray); setFill(fillColor); drawGeometry(geometry, symbol); template.restoreState(); }
From source file:org.geomajas.plugin.print.component.PdfContext.java
License:Open Source License
public Graphics2D getGraphics2D(Rectangle clipRect) { Graphics2D graphics = template.createGraphics(template.getWidth(), template.getHeight()); graphics.translate(origX, template.getHeight() - origY - clipRect.getHeight()); graphics.clipRect(0, 0, (int) clipRect.getWidth(), (int) clipRect.getHeight()); return graphics; }
From source file:org.geomajas.plugin.printing.command.print.PrintGetTemplateCommand.java
License:Open Source License
private float getPageSizeRelativeToA3(PrintGetTemplateRequest request) { Rectangle r; if (request.getPageSize() != null) { r = PageSize.getRectangle(request.getPageSize()); } else {// ww w .j a va 2s .c om float width = request.getTemplate().getPage().getLayoutConstraint().getWidth(); float height = request.getTemplate().getPage().getLayoutConstraint().getHeight(); r = new Rectangle(0, 0, width, height); } return (r.getWidth() / PageSize.A3.getWidth() + r.getHeight() / PageSize.A3.getHeight()) / 2; }
From source file:org.jaffa.modules.printing.services.PdfHelper.java
License:Open Source License
/** * Scale the pages of the input pdfOutput document to the given pageSize. * @param pdfOutput The PDF document to rescale, in the form of a ByteArrayOutputStream. * @param pageSize The new page size to which to scale to PDF document, e.g. "A4". * @param noEnlarge If true, center pages instead of enlarging them. * Use noEnlarge if the new page size is larger than the old one * and the pages should be centered instead of enlarged. * @param preserveAspectRatio If true, the aspect ratio will be preserved. * @return The PDF document with its pages scaled to the input pageSize. *//*from www. j a va 2 s . c om*/ public static byte[] scalePdfPages(byte[] pdfOutput, String pageSize, boolean noEnlarge, boolean preserveAspectRatio) throws FormPrintException { if (pageSize == null || pdfOutput == null) { return pdfOutput; } // Get the dimensions of the given pageSize in PostScript points. // A PostScript point is a 72th of an inch. float dimX; float dimY; Rectangle rectangle; try { rectangle = PageSize.getRectangle(pageSize); } catch (Exception ex) { FormPrintException e = new PdfProcessingException( "scalePdfPages - Invalid page size = " + pageSize + " "); log.error(" scalePdfPages - Invalid page size: " + pageSize + ". " + ex.getMessage() + ". "); throw e; } if (rectangle != null) { dimX = rectangle.getWidth(); dimY = rectangle.getHeight(); } else { FormPrintException e = new PdfProcessingException("scalePdfPages - Invalid page size: " + pageSize); log.error(" scalePdfPages - Invalid page size: " + pageSize); throw e; } //Create portrait and landscape rectangles for the given page size. Rectangle portraitPageSize; Rectangle landscapePageSize; if (dimY > dimX) { portraitPageSize = new Rectangle(dimX, dimY); landscapePageSize = new Rectangle(dimY, dimX); } else { portraitPageSize = new Rectangle(dimY, dimX); landscapePageSize = new Rectangle(dimX, dimY); } // Remove the document rotation before resizing the document. byte[] output = removeRotation(pdfOutput); PdfReader currentReader = null; try { currentReader = new PdfReader(output); } catch (IOException ex) { FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Reader"); log.error(" scalePdfPages - Failed to create a PDF Reader "); throw e; } OutputStream baos = new ByteArrayOutputStream(); Rectangle newSize = new Rectangle(dimX, dimY); Document document = new Document(newSize, 0, 0, 0, 0); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, baos); } catch (DocumentException ex) { FormPrintException e = new PdfProcessingException("scalePdfPages - Failed to create a PDF Writer"); log.error(" scalePdfPages - Failed to create a PDF Writer "); throw e; } document.open(); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page; float offsetX, offsetY; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { Rectangle currentSize = currentReader.getPageSizeWithRotation(i); if (currentReader.getPageRotation(i) != 0) { FormPrintException e = new PdfProcessingException("Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form."); log.error(" Page Rotation, " + currentReader.getPageRotation(i) + ", must be removed to re-scale the form. "); throw e; } //Reset the page size for each page because there may be a mix of sizes in the document. float currentWidth = currentSize.getWidth(); float currentHeight = currentSize.getHeight(); if (currentWidth > currentHeight) { newSize = landscapePageSize; } else { newSize = portraitPageSize; } document.setPageSize(newSize); document.newPage(); float factorX = newSize.getWidth() / currentSize.getWidth(); float factorY = newSize.getHeight() / currentSize.getHeight(); // Use noEnlarge if the new page size is larger than the old one // and the pages should be centered instead of enlarged. if (noEnlarge) { if (factorX > 1) { factorX = 1; } if (factorY > 1) { factorY = 1; } } if (preserveAspectRatio) { factorX = Math.min(factorX, factorY); factorY = factorX; } offsetX = (newSize.getWidth() - (currentSize.getWidth() * factorX)) / 2f; offsetY = (newSize.getHeight() - (currentSize.getHeight() * factorY)) / 2f; page = writer.getImportedPage(currentReader, i); cb.addTemplate(page, factorX, 0, 0, factorY, offsetX, offsetY); } document.close(); return ((ByteArrayOutputStream) baos).toByteArray(); }
From source file:org.jaffa.modules.printing.services.PdfHelper.java
License:Open Source License
/** * Remove the rotation from the pdfOutput document pages. *///from w w w . java 2 s . c o m private static byte[] removeRotation(byte[] pdfOutput) throws FormPrintException { PdfReader currentReader = null; try { currentReader = new PdfReader(pdfOutput); } catch (IOException ex) { FormPrintException e = new PdfProcessingException( "Remove PDF Page Rotation - Failed to create a PDF Reader"); log.error(" Remove PDF Page Rotation - Failed to create a PDF Reader "); throw e; } boolean needed = false; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { if (currentReader.getPageRotation(i) != 0) { needed = true; } } if (!needed) { return pdfOutput; } OutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, baos); } catch (DocumentException ex) { FormPrintException e = new PdfProcessingException( "Remove PDF Page Rotation - Failed to create a PDF Writer"); log.error(" Remove PDF Page Rotation - Failed to create a PDF Writer "); throw e; } PdfContentByte cb = null; PdfImportedPage page; for (int i = 1; i <= currentReader.getNumberOfPages(); i++) { Rectangle currentSize = currentReader.getPageSizeWithRotation(i); currentSize = new Rectangle(currentSize.getWidth(), currentSize.getHeight()); // strip rotation document.setPageSize(currentSize); if (cb == null) { document.open(); cb = writer.getDirectContent(); } else { document.newPage(); } int rotation = currentReader.getPageRotation(i); page = writer.getImportedPage(currentReader, i); float a, b, c, d, e, f; if (rotation == 0) { a = 1; b = 0; c = 0; d = 1; e = 0; f = 0; } else if (rotation == 90) { a = 0; b = -1; c = 1; d = 0; e = 0; f = currentSize.getHeight(); } else if (rotation == 180) { a = -1; b = 0; c = 0; d = -1; e = currentSize.getWidth(); f = currentSize.getHeight(); } else if (rotation == 270) { a = 0; b = 1; c = -1; d = 0; e = currentSize.getWidth(); f = 0; } else { FormPrintException ex = new PdfProcessingException( "Remove PDF Page Rotation - Unparsable rotation value: " + rotation); log.error(" Remove PDF Page Rotation - Unparsable form rotation value: " + rotation); throw ex; } cb.addTemplate(page, a, b, c, d, e, f); } document.close(); return ((ByteArrayOutputStream) baos).toByteArray(); }