List of usage examples for com.lowagie.text.pdf PdfTemplate getWidth
public float getWidth()
From source file:gov.noaa.pfel.coastwatch.sgt.SgtUtil.java
License:Open Source License
/** * This closes the pdf file created by createPDF, after you have * written things to g2D./*from www . j av a 2s .co m*/ * * @param oar the object[] returned from createPdf * @throwsException if trouble */ public static void closePdf(Object oar[]) throws Exception { Graphics2D g2D = (Graphics2D) oar[0]; Document document = (Document) oar[1]; PdfContentByte pdfContentByte = (PdfContentByte) oar[2]; PdfTemplate pdfTemplate = (PdfTemplate) oar[3]; g2D.dispose(); //center it if (verbose) String2.log("SgtUtil.closePdf" + " left=" + document.left() + " right=" + document.right() + " bottom=" + document.bottom() + " top=" + document.top() + " template.width=" + pdfTemplate.getWidth() + " template.height=" + pdfTemplate.getHeight()); //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f //x,y location in points 0.5f, 0, 0, 0.5f, document.left() + (document.right() - document.left() - pdfTemplate.getWidth() / 2) / 2, document.bottom() + (document.top() - document.bottom() - pdfTemplate.getHeight() / 2) / 2); /* //if boundingBox is small, center it //if boundingBox is large, shrink and center it //document.left/right/top/bottom include 1/2" margins float xScale = (document.right() - document.left()) / pdfTemplate.getWidth(); float yScale = (document.top() - document.bottom()) / pdfTemplate.getHeight(); float scale = Math.min(Math.min(xScale, yScale), 1); float xSize = pdfTemplate.getWidth() / scale; float ySize = pdfTemplate.getHeight() / scale; //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f scale, 0, 0, scale, document.left() + (document.right() - document.left() - xSize) / 2, document.bottom() + (document.top() - document.bottom() - ySize) / 2); */ document.close(); }
From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java
License:Open Source License
protected void drawBackgroundImage(float x, float y, float width, float height, float imageWidth, float imageHeight, int repeat, String imageUrl, byte[] imageData, float offsetX, float offsetY) throws Exception { contentByte.saveState();//from w w w . j a va 2s .c om clip(x, y, width, height); PdfTemplate image = null; if (imageUrl != null) { if (pageDevice.getImageCache().containsKey(imageUrl)) { image = pageDevice.getImageCache().get(imageUrl); } } if (image == null) { Image img = Image.getInstance(imageData); if (imageHeight == 0 || imageWidth == 0) { int resolutionX = img.getDpiX(); int resolutionY = img.getDpiY(); if (0 == resolutionX || 0 == resolutionY) { resolutionX = 96; resolutionY = 96; } imageWidth = img.getPlainWidth() / resolutionX * 72; imageHeight = img.getPlainHeight() / resolutionY * 72; } image = contentByte.createTemplate(imageWidth, imageHeight); image.addImage(img, imageWidth, 0, 0, imageHeight, 0, 0); if (imageUrl != null && image != null) { pageDevice.getImageCache().put(imageUrl, image); } } boolean xExtended = (repeat & BackgroundImageInfo.REPEAT_X) == BackgroundImageInfo.REPEAT_X; boolean yExtended = (repeat & BackgroundImageInfo.REPEAT_Y) == BackgroundImageInfo.REPEAT_Y; imageWidth = image.getWidth(); imageHeight = image.getHeight(); float originalX = offsetX; float originalY = offsetY; if (xExtended) { while (originalX > 0) originalX -= imageWidth; } if (yExtended) { while (originalY > 0) originalY -= imageHeight; } float startY = originalY; do { float startX = originalX; do { drawImage(image, x + startX, y + startY, imageWidth, imageHeight); startX += imageWidth; } while (startX < width && xExtended); startY += imageHeight; } while (startY < height && yExtended); contentByte.restoreState(); }
From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java
License:Open Source License
protected void drawImage(PdfTemplate image, float imageX, float imageY, float height, float width, String helpText) throws DocumentException { imageY = transformY(imageY, height); contentByte.saveState();//from w ww .j a va 2 s. c om contentByte.concatCTM(1, 0, 0, 1, imageX, imageY); float w = image.getWidth(); float h = image.getHeight(); contentByte.addTemplate(image, width / w, 0f / w, 0f / h, height / h, 0f, 0f); if (helpText != null) { showHelpText(imageX, imageY, width, height, helpText); } contentByte.restoreState(); }
From source file:org.mapfish.print.PDFUtils.java
License:Open Source License
/** * Gets an iText image with a cache that uses PdfTemplates to re-use the same * bitmap content multiple times in order to reduce the file size. *///ww w . ja v a 2 s. c om public static Image getImage(RenderingContext context, URI uri, float w, float h) throws IOException, DocumentException { //Check the image is not already used in the PDF file. // //This part is not protected against multi-threads... worst case, a single image can //be twice in the PDF, if used more than one time. But since only one !map //block is dealed with at a time, this should not happen Map<URI, PdfTemplate> cache = context.getTemplateCache(); PdfTemplate template = cache.get(uri); if (template == null) { Image content = getImageDirect(context, uri); content.setAbsolutePosition(0, 0); final PdfContentByte dc = context.getDirectContent(); synchronized (context.getPdfLock()) { //protect against parallel writing on the PDF file template = dc.createTemplate(content.getPlainWidth(), content.getPlainHeight()); template.addImage(content); } cache.put(uri, template); } //fix the size/aspect ratio of the image in function of what is specified by the user if (w == 0.0f) { if (h == 0.0f) { w = template.getWidth(); h = template.getHeight(); } else { w = h / template.getHeight() * template.getWidth(); } } else { if (h == 0.0f) { h = w / template.getWidth() * template.getHeight(); } } final Image result = Image.getInstance(template); result.scaleToFit(w, h); return result; }