List of usage examples for com.itextpdf.text Image getHeight
public float getHeight()
From source file:eyeofthetiger.utils.PDFDossardGenerator.java
public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException { sortParticipants(participants);/*ww w.ja v a 2s . c o m*/ Document document = new Document(PageSize.A4.rotate()); float margin = CentimeterToUserSpace(marginCm); document.setMargins(margin, margin, margin, margin); PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfReader pdfBackgroundReader = null; PdfImportedPage backgroundPage = null; if (pdfBackground != null && (new File(pdfBackground)).exists() && (new File(pdfBackground)).isFile()) { pdfBackgroundReader = new PdfReader(pdfBackground); backgroundPage = writer.getImportedPage(pdfBackgroundReader, 1); } float documentTop = document.top(); float documentBottom = document.bottom(); float documentHeight = documentTop - documentBottom; float left = document.left(); float right = document.right(); float width = right - left; float height = documentTop - documentBottom; //cb.rectangle(left, documentBottom, width, documentHeight); //cb.stroke(); boolean logoLeftExist = (new File(logoLeft)).exists() && (new File(logoLeft)).isFile(); boolean logoRightExist = (new File(logoRight)).exists() && (new File(logoRight)).isFile(); float imgLeftRight = left; float imgLeftBottom = documentTop; float imgRightLeft = right; float imgRighBottom = documentTop; Image imgLeft = null; Image imgRight = null; if (exportLogos) { if (logoLeftExist) { imgLeft = Image.getInstance(logoLeft); float h = imgLeft.getHeight(); float w = imgLeft.getWidth(); float nw = width * logoLeftWidth; float nh = (h / w) * nw; imgLeft.scaleAbsolute(nw, nh); //img.scaleAbsoluteHeight(img.getScaledWidth() / xyRatio); imgLeft.setAbsolutePosition(left, documentTop - imgLeft.getScaledHeight()); //cb.addImage(img); imgLeftRight = imgLeft.getAbsoluteX() + imgLeft.getScaledWidth(); imgLeftBottom = imgLeft.getAbsoluteY(); } if (logoRightExist) { imgRight = Image.getInstance(logoRight); float h = imgRight.getHeight(); float w = imgRight.getWidth(); float nw = width * logoRightWidth; float nh = (h / w) * nw; imgRight.scaleAbsolute(nw, nh); imgRight.setAbsolutePosition(right - imgRight.getScaledWidth(), documentTop - imgRight.getScaledHeight()); //cb.addImage(imgRight); imgRightLeft = imgRight.getAbsoluteX(); imgRighBottom = imgRight.getAbsoluteY(); } } float nameHeightPercent = 0.35f; float groupHeightPercent = 0.25f; float nameTop = documentTop; float nameBottom = nameTop; if (exportName) { nameBottom = nameTop - (documentHeight * nameHeightPercent); } float groupeTop = nameBottom; float groupeBottom = nameBottom; if (exportGroup) { groupeBottom = groupeTop - (documentHeight * groupHeightPercent); } float barcodeTop = groupeBottom; float barcodeBottom = documentBottom; ColumnText columnText; for (Participant participant : participants) { if (backgroundPage != null) { //cb.addTemplate(backgroundPage, 1f, 0, 0, 1, 0, 0); //TODO cb.addTemplate(backgroundPage, 0, 0); } float nameFontSize = 65f; float groupFontSize = 45f; float renseignementFontSize = 35f; if (imgLeft != null) { cb.addImage(imgLeft); } if (imgRight != null) { cb.addImage(imgRight); } if (exportName) { columnText = new ColumnText(cb); columnText.setAlignment(Rectangle.ALIGN_CENTER); if (imgLeftRight != -1 && imgLeftBottom != -1) { float[] leftBorder = null; if (imgLeftBottom < nameBottom) { leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, nameBottom, left, nameBottom }; } else { leftBorder = new float[] { imgLeftRight, nameTop, imgLeftRight, imgLeftBottom, left, imgLeftBottom, left, nameBottom }; } float[] rightBorder = null; if (imgRighBottom < nameBottom) { rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, nameBottom, right, nameBottom }; } else { rightBorder = new float[] { imgRightLeft, nameTop, imgRightLeft, imgRighBottom, right, imgRighBottom, right, nameBottom }; } columnText.setColumns(leftBorder, rightBorder); } else { columnText.setSimpleColumn(left, nameTop, right, nameBottom); } //cb.rectangle(left, nameBottom, width, (nameTop - nameBottom)); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom(); float previousPos = columnText.getYLine(); columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); while (nameFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) { nameFontSize = nameFontSize - 0.5f; columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); columnText.setYLine(previousPos); } columnText.setLeading(nameFontSize); columnText.setText(createCleanPhrase(txt, nameFontSize, true)); columnText.setYLine(previousPos); columnText.go(false); } if (exportGroup) { columnText = new ColumnText(cb); columnText.setAlignment(Rectangle.ALIGN_CENTER); columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom); float groupeHeight = groupeTop - groupeBottom; //cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() - document.left(), groupeHeight); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); columnText.setFollowingIndent(0); String txt1 = participant.getGroupe(); String txt2 = exportRenseignement ? "\n" + participant.getRenseignements() : null; float previousPos = columnText.getYLine(); columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); while (groupFontSize > 1 && ColumnText.hasMoreText(columnText.go(true))) { groupFontSize = groupFontSize - 0.5f; renseignementFontSize = renseignementFontSize - 0.5f; columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); } columnText.setText(null); columnText.setLeading(groupFontSize); columnText.addText(createCleanPhrase(txt1, groupFontSize, true)); columnText.addText(createCleanPhrase(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); columnText.go(false); } { columnText = new ColumnText(cb); float topMargin = 12f; columnText.setSimpleColumn(left, barcodeTop - topMargin, right, barcodeBottom); float barcodeHeight = (barcodeTop - topMargin) - barcodeBottom; //cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight); //cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); float previousPos = columnText.getYLine(); columnText.setText(null); columnText.addElement(createCleanBarcode(cb, participant.getNumero(), width, barcodeHeight)); columnText.go(false); } document.newPage(); } document.close(); if (pdfBackgroundReader != null) { pdfBackgroundReader.close(); } }
From source file:fr.ybonnel.breizhcamppdf.AvatarService.java
License:Apache License
public Image getImage(String url) throws BadElementException, IOException { if (url == null) { return null; }//from ww w . jav a 2 s . co m if (!cache.containsKey(url)) { System.out.println(url); try { Image avatar = Image.getInstance(url); int height = 40; float factor = ((float) height) / avatar.getHeight(); int width = (int) (avatar.getWidth() * factor); avatar.scaleToFit(width, height); cache.put(url, avatar); } catch (Exception ex) { } } return cache.get(url); }
From source file:fr.ybonnel.breizhcamppdf.PdfRenderer.java
License:Apache License
private void createFirstPage() throws DocumentException, IOException { Rectangle savePagesize = document.getPageSize(); document.setPageSize(PageSize.A4);/*from w w w . j a v a 2 s . co m*/ document.newPage(); Image imageLogo = Image.getInstance(PdfRenderer.class.getResource("/logo.png")); imageLogo.scaleToFit(document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin(), imageLogo.getHeight()); document.add(imageLogo); Paragraph paragraph = new Paragraph("21, 22 et 23 mai"); paragraph.setSpacingAfter(80); paragraph.getFont().setSize(20); paragraph.setAlignment(Element.ALIGN_CENTER); document.add(paragraph); Paragraph title = new Paragraph("Programme 2014"); title.setSpacingAfter(100); title.getFont().setSize(56); title.setAlignment(Element.ALIGN_CENTER); document.add(title); PdfPTable sponsors = new PdfPTable(4); sponsors.setWidthPercentage(90f); addSponsor(sponsors, "http://www.breizhcamp.org/img/LogoZenika-H-(Quadri).jpg", 2); addSponsor(sponsors, "http://blogs.technet.com/resized-image.ashx/__size/450x0/__key/communityserver-blogs-components-weblogfiles/00-00-00-80-54/6864.Microsoft-Logo.png", 2); addSponsor(sponsors, "http://www.breizhcamp.org/img/Google_Logo_3564x1189.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/cbpaas.jpeg", 1); addSponsor(sponsors, "https://github.global.ssl.fastly.net/images/modules/logos_page/GitHub-Logo.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/sonarsource-300x94.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/serli.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/Logo_sqli_group.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/SofteamCadextanLogo_w200.png", 1); addSponsor(sponsors, "http://www.breizhcamp.org/img/logoSII_w200.png", 1); document.add(sponsors); Image istic = Image.getInstance("http://www.breizhcamp.org/img/Logo_ISTIC_BLACK_FRA.jpg"); istic.scaleToFit(150, 150); istic.setAlignment(Element.ALIGN_CENTER); document.add(istic); document.setPageSize(savePagesize); }
From source file:gde.report.pdf.ITextInlineImageReplacedElementFactory.java
License:Open Source License
@Override public ReplacedElement createReplacedElement(final LayoutContext c, final BlockBox box, final UserAgentCallback uac, final int cssWidth, final int cssHeight) { final Element elem = box.getElement(); // check if we have an inline png image if (!(elem != null && elem.getNodeName().equals("img") && elem.hasAttribute("src") //$NON-NLS-1$//$NON-NLS-2$ && elem.getAttribute("src").startsWith(PREFIX))) { //$NON-NLS-1$ return null; // other.createReplacedElement(c, box, uac, cssWidth, // cssHeight); }/*from w ww . j av a 2s . co m*/ int width = 0; int height = 0; if (cssWidth > 0) { width = cssWidth; } if (cssHeight > 0) { height = cssHeight; } if (elem.hasAttribute("width")) { //$NON-NLS-1$ width = Integer.parseInt(elem.getAttribute("width")); //$NON-NLS-1$ } if (elem.hasAttribute("height")) { //$NON-NLS-1$ height = Integer.parseInt(elem.getAttribute("height")); //$NON-NLS-1$ } final String inlineData = elem.getAttribute("src").substring(PREFIX.length()); // strip //$NON-NLS-1$ // leading // "data:image/png;base64," try { final Image image = Image.getInstance(Base64.decode(inlineData)); if (width == 0) { width = (int) image.getWidth(); } if (height == 0) { height = (int) image.getHeight(); } image.scaleAbsolute(width * 16, height * 16); final ITextFSImage fsImage = new ITextFSImage(image); final ITextReplacedElement element = new ITextImageElement(fsImage); return element; } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:info.longnetpro.examples.PdfLibExamples.java
public static void generatePdf() throws DocumentException, URISyntaxException, MalformedURLException, IOException { String licFile = getLicenseFilePath(); loadLicenseFile(licFile);//from ww w. j av a2 s . c om String dest = getTargetFilePath(); Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream(dest)); Rectangle pageSize = PageSize.LETTER; Rectangle rect = new Rectangle(0f, 0f, 50f, 100f); rect.setBorder(15); rect.setBorderColor(BaseColor.RED); rect.setBorderWidth(.5f); rect.setBackgroundColor(BaseColor.BLUE); doc.setPageSize(pageSize); doc.open(); Page page = new Page(pageSize.getWidth(), pageSize.getHeight()); ContentBox rpage = page.margin(new Float[] { 10f, 50f, 10f, 50f }); for (Anchor anchor : Anchor.values()) { ContentBox box = new ContentBox(50f, 100f); float offx = anchor.equals(Anchor.CENTER) ? -50f : 0f; float offy = anchor.equals(Anchor.CENTER) ? -50f : 0f; ContentBox rr = anchor.anchorElement(rpage, box, offx, offy); if (anchor.equals(Anchor.BOTTOM_LEFT)) { float[] dim = box.scaleByPercentage(0.5f); //rr = rr.reposition(dim[0], dim[1], Anchor.TOP_RIGHT); } rect.setLeft(rr.getLeft()); rect.setBottom(rr.getBottom()); rect.setRight(rr.getRight()); rect.setTop(rr.getTop()); doc.add(rect); } String imageFile = getImageFilePath(); Image image = Image.getInstance(imageFile); float width = Measurement.dotsToUserUnits(image.getWidth(), 1200); float height = Measurement.dotsToUserUnits(image.getHeight(), 1200); System.out.println(width + " " + height); ContentBox img = Anchor.BOTTOM_LEFT.anchorElement(rpage, width, height); image.scaleToFit(width, height); image.setAbsolutePosition(img.getLeft(), img.getBottom()); System.out.println(image.getWidth() + " " + image.getHeight()); System.out.println(image.getAbsoluteX() + " " + image.getAbsoluteY()); doc.add(image); doc.close(); }
From source file:jdbreport.model.io.pdf.itext5.PdfWriter.java
License:Apache License
private com.itextpdf.text.Image awtImageToImage(java.awt.Image awtImage, jdbreport.model.Cell srcCell, float w, float h) throws BadElementException, IOException { com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(awtImage, null); boolean scaled = srcCell.isScaleIcon(); double kx = 1.0 / GraphicUtil.getScreenScaleX(); double ky = 1.0 / GraphicUtil.getScreenScaleY(); float imageWidth = (float) (image.getWidth() * kx); float imageHeight = (float) (image.getHeight() * ky); if (!scaled) { if (imageWidth < w) { w = imageWidth;/*from ww w. ja va2s . c o m*/ } if (imageHeight < h) { h = imageHeight; } } if (imageWidth >= w || imageHeight >= h) { scaled = true; } if (scaled) { image.scaleAbsolute(w - 2, h - 2); } return image; }
From source file:Login.ventas.fproyectos.java
public void fondos(Document documento, PdfContentByte canvas) { try {//from www . ja va 2s.c o m Image imghead = Image.getInstance(usuario.getDireccion() + "/plantilla.jpg"); imghead.setAbsolutePosition(0, 0); imghead.setAlignment(Image.ALIGN_CENTER); float scaler = ((documento.getPageSize().getWidth() - documento.leftMargin() - documento.rightMargin()) / imghead.getWidth()) * 100; imghead.scalePercent(scaler); PdfTemplate tp = canvas.createTemplate(PageSize.A4.getWidth(), PageSize.A4.getHeight()); //el rea destinada para el encabezado tp.addImage(imghead); x = (int) imghead.getWidth(); y = (int) imghead.getHeight(); canvas.addTemplate(tp, 0, 0);//posicin del tmplate derecha y abajo } catch (IOException | DocumentException io) { } }
From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java
License:Apache License
/** * Applies an image watermark//w w w. j a v a 2 s. co m * * @param reader * @param writer * @param options * @throws Exception */ private void imageAction(Action ruleAction, NodeRef actionedUponNodeRef, NodeRef watermarkNodeRef, ContentReader actionedUponContentReader, ContentReader watermarkContentReader, Map<String, Object> options) { PdfStamper stamp = null; File tempDir = null; ContentWriter writer = null; try { // get a temp file to stash the watermarked PDF in before moving to // repo File alfTempDir = TempFileProvider.getTempDir(); tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId()); tempDir.mkdir(); File file = new File(tempDir, serviceRegistry.getFileFolderService().getFileInfo(actionedUponNodeRef).getName()); // get the PDF input stream and create a reader for iText PdfReader reader = new PdfReader(actionedUponContentReader.getContentInputStream()); stamp = new PdfStamper(reader, new FileOutputStream(file)); PdfContentByte pcb; // get a com.itextpdf.text.Image object via java.imageio.ImageIO Image img = Image.getInstance(ImageIO.read(watermarkContentReader.getContentInputStream()), null); // get the PDF pages and position String pages = (String) options.get(PARAM_PAGE); String position = (String) options.get(PARAM_POSITION); String depth = (String) options.get(PARAM_WATERMARK_DEPTH); // image requires absolute positioning or an exception will be // thrown // set image position according to parameter. Use // PdfReader.getPageSizeWithRotation // to get the canvas size for alignment. img.setAbsolutePosition(100f, 100f); // stamp each page int numpages = reader.getNumberOfPages(); for (int i = 1; i <= numpages; i++) { Rectangle r = reader.getPageSizeWithRotation(i); // set stamp position if (position.equals(POSITION_BOTTOMLEFT)) { img.setAbsolutePosition(0, 0); } else if (position.equals(POSITION_BOTTOMRIGHT)) { img.setAbsolutePosition(r.getWidth() - img.getWidth(), 0); } else if (position.equals(POSITION_TOPLEFT)) { img.setAbsolutePosition(0, r.getHeight() - img.getHeight()); } else if (position.equals(POSITION_TOPRIGHT)) { img.setAbsolutePosition(r.getWidth() - img.getWidth(), r.getHeight() - img.getHeight()); } else if (position.equals(POSITION_CENTER)) { img.setAbsolutePosition(getCenterX(r, img), getCenterY(r, img)); } // if this is an under-text stamp, use getUnderContent. // if this is an over-text stamp, usse getOverContent. if (depth.equals(DEPTH_OVER)) { pcb = stamp.getOverContent(i); } else { pcb = stamp.getUnderContent(i); } // only apply stamp to requested pages if (checkPage(pages, i, numpages)) { pcb.addImage(img); } } stamp.close(); // Get a writer and prep it for putting it back into the repo //can't use BasePDFActionExecuter.getWriter here need the nodeRef of the destination NodeRef destinationNode = createDestinationNode(file.getName(), (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER), actionedUponNodeRef); writer = serviceRegistry.getContentService().getWriter(destinationNode, ContentModel.PROP_CONTENT, true); writer.setEncoding(actionedUponContentReader.getEncoding()); writer.setMimetype(FILE_MIMETYPE); // Put it in the repo writer.putContent(file); // delete the temp file file.delete(); } catch (IOException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } finally { if (tempDir != null) { try { tempDir.delete(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } if (stamp != null) { try { stamp.close(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } } }
From source file:org.durel.mydooble.ImageItem.java
License:Open Source License
@Override public void toPDF(PDF out, int i) { super.toPDF(out, i); try {// w w w. ja v a 2 s . c o m Image img = Image.getInstance(image); img.setDpi(288, 288); img.setInterpolation(true); float ih = (float) (img.getHeight()); float iw = (float) (img.getWidth()); if (ih > h || iw > w) { float xratio = iw / w; float yratio = ih / h; float ratio = Math.max(xratio, yratio); log.info("ih: " + ih + " - iw: " + iw + " - xratio: " + xratio + " - yratio: " + yratio + " - ratio: " + ratio + " --> " + (int) (iw / ratio) + "x" + (int) (ih / ratio)); img.scalePercent(100 / ratio); } PdfContentByte cb = out.writer.getDirectContent(); float x = bx + (c * (w + m)) + m; float y = by + (r * (h + m)) + m; img.setAbsolutePosition(x, y); cb.addImage(img); } catch (DocumentException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:pdfcreator.PDFCreator.java
License:Open Source License
@SuppressWarnings("static-access") protected void createPdf(String filename, String[] images) throws Exception { Document doc = new Document(); PdfWriter writer;/*ww w . j av a 2 s . c o m*/ if (pdfxConformance.equals("PDFA1A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1A); } else if (pdfxConformance.equals("PDFA1B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B); } else if (pdfxConformance.equals("PDFA2A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_2A); } else if (pdfxConformance.equals("PDFA2B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_2B); } else if (pdfxConformance.equals("PDFA3A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_3A); } else if (pdfxConformance.equals("PDFA3B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_3B); } else { writer = PdfWriter.getInstance(doc, new FileOutputStream(filename)); } if (pdfVersion.equals("1.4")) { writer.setPdfVersion(PdfWriter.VERSION_1_4); } else if (pdfVersion.equals("1.5")) { writer.setPdfVersion(PdfWriter.VERSION_1_5); } else if (pdfVersion.equals("1.6")) { writer.setPdfVersion(PdfWriter.VERSION_1_6); } else if (pdfVersion.equals("1.7")) { writer.setPdfVersion(PdfWriter.VERSION_1_7); } else { writer.setPdfVersion(PdfWriter.VERSION_1_4); } verbose(filename + ": open"); doc.addCreationDate(); doc.addCreator(creator); if (title != null) { doc.addTitle(title); } for (int i = 0; i < images.length; i++) { verbose(" +" + images[i]); Image img = Image.getInstance(images[i]); doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight())); doc.setMargins(0, 0, 0, 0); if (doc.isOpen()) { doc.newPage(); } else { doc.open(); } doc.add(img); doc.newPage(); } ICC_Profile icc = getImageColorProfile(images[0]); if (icc == null) { System.err.println("warning: no color profile available in " + images[0] + " using " + profileName); icc = getDefaultColorProfile(); } writer.setOutputIntents("Custom", "", null, null, icc); writer.createXmpMetadata(); doc.close(); verbose(filename + ": close"); }