List of usage examples for com.lowagie.text Image getInstance
public static Image getInstance(java.awt.Image image, java.awt.Color color) throws BadElementException, IOException
From source file:net.laubenberger.wichtel.helper.HelperPdf.java
License:Open Source License
/** * Writes a PDF from multiple {@link java.awt.Image} to a {@link File}. * /* ww w .ja va 2 s . c o m*/ * @param pageSize * of the PDF * @param scale * images to fit the page size * @param file * output as PDF * @param images * for the PDF * @throws DocumentException * @throws IOException * @see File * @see java.awt.Image * @see Rectangle * @since 0.0.1 */ public static void writePdfFromImages(final Rectangle pageSize, final boolean scale, final File file, final java.awt.Image... images) throws DocumentException, IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(pageSize, scale, file, images)); if (null == pageSize) { throw new RuntimeExceptionIsNull("pageSize"); //$NON-NLS-1$ } if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } if (null == images) { throw new RuntimeExceptionIsNull("images"); //$NON-NLS-1$ } if (!HelperArray.isValid(images)) { throw new RuntimeExceptionIsEmpty("images"); //$NON-NLS-1$ } final Document document = new Document(pageSize); document.setMargins(0.0F, 0.0F, 0.0F, 0.0F); try (FilterOutputStream fos = new BufferedOutputStream(new FileOutputStream(file))) { PdfWriter.getInstance(document, fos); document.open(); for (final java.awt.Image tempImage : images) { if (null == tempImage) { throw new RuntimeExceptionIsNull("tempImage"); //$NON-NLS-1$ } final Image image = Image.getInstance(tempImage, null); if (scale) { image.scaleToFit(pageSize.getWidth(), pageSize.getHeight()); } document.add(image); document.newPage(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.sf.firemox.deckbuilder.BuildBook.java
License:Open Source License
/** * @param cardName//from w w w. java 2s .com * the key card name. * @return the image associated to this card * @throws BadElementException * @throws IOException * If some other I/O error occurs */ @SuppressWarnings("null") public Image getImage(String cardName) throws BadElementException, IOException { Iterator<String> i = null; i = cachedImageDir.iterator(); File img = null; while (i.hasNext()) { String[] extensions = new String[] { ".jpg", ".jpeg", ".png", ".gif", ".tiff" }; String base = i.next(); for (int j = 0; j < extensions.length; j++) { img = new File(base, cardName.toLowerCase() + extensions[j]); if (img.exists()) { break; } img = null; } if (img != null) break; img = null; } if (img != null) { java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(img.getAbsolutePath()); java.awt.Image awtImageX4 = awtImage.getScaledInstance(800, -1, java.awt.Image.SCALE_SMOOTH); awtImage = null; return Image.getInstance(awtImageX4, null); } i = imageSources.iterator(); while (i.hasNext()) { String urlpath = i.next(); urlpath = urlpath.replace("{name}", cardName); URL url = new URL(urlpath); URLConnection cn = url.openConnection(); if (cn.getContentType() == null || cn.getContentType().startsWith("image") && cn.getInputStream() != null) { img = new File(this.cachedImageDirStore, cardName.toLowerCase() + ".jpg"); BufferedOutputStream fw = new BufferedOutputStream(new FileOutputStream(img)); BufferedInputStream is = new BufferedInputStream(cn.getInputStream()); IOUtils.copy(is, fw); fw.close(); java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(img.getAbsolutePath()); java.awt.Image awtImageX4 = null; awtImageX4 = awtImage.getScaledInstance(800, -1, java.awt.Image.SCALE_SMOOTH); return Image.getInstance(awtImageX4, null); } } return Image.getInstance(this.dedfaultImage); }
From source file:org.areasy.common.doclet.document.tags.TagIMG.java
License:Open Source License
public Element[] toPdfObjects() { String src = getAttribute("src"); Element[] results = new Element[0]; Element result = null;/*w ww . j a v a 2 s . c om*/ if (src == null) { log.error("Image tag has no 'src' attribute."); return null; } try { // now we use awt Image to create iText image instead of // letting it read images by itself java.awt.Image awt = null; if (src.indexOf("://") > 0) awt = Toolkit.getDefaultToolkit().createImage(new URL(src)); else { String filePath = DocletUtility.getFilePath(src); awt = Toolkit.getDefaultToolkit().createImage(filePath); } //img = Image.getInstance(PDFUtil.getFilePath(src)); img = Image.getInstance(awt, null); // for the time being let's stick with A4 Rectangle size = PageSize.A4; String width = getAttribute("width"); if (width != null) { // trying to cope with the resolution differences float w = NumberUtility.toFloat(width, 0) / 96 * 72; img.scaleAbsoluteWidth(w); } float maxW = size.width() - 100; if (img.plainWidth() > maxW) img.scaleAbsoluteWidth(maxW); String height = getAttribute("height"); if (height != null) { // trying to cope with the resolution differences float h = NumberUtility.toFloat(height, 0) / 96 * 72; img.scaleAbsoluteHeight(h); } float maxH = size.height() - 100; if (img.plainHeight() > maxH) img.scaleAbsoluteHeight(maxH); result = img; img.setAlignment(HtmlTagUtility.getAlignment(getAttribute("align"), Element.ALIGN_CENTER)); String border = getAttribute("border"); if (border != null) { // trying to cope with the resolution differences float b = NumberUtility.toFloat(border, 0); img.setBorder((int) b); img.setBorderWidthTop(b); img.setBorderWidthLeft(b); img.setBorderWidthRight(b); img.setBorderWidthBottom(b); img.setBorderColor(Color.black); } } catch (FileNotFoundException e) { DocletUtility.error("** Image not found: " + src, e); } catch (Exception e) { DocletUtility.error("** Failed to read image: " + src, e); } if (result != null) { results = new Element[1]; results[0] = result; } return results; }
From source file:org.jaffa.modules.printing.services.FormPrintEngineIText.java
License:Open Source License
/** Print a java image */ private void printImage(java.awt.Image image, PdfContentByte cb, float x1, float y1, float x2, float y2, int alignment, int fitMethod, float rotate) throws BadElementException, IOException, DocumentException { if (image != null) { // Convert to an iText Image Image img = Image.getInstance(image, null); printImage(img, cb, x1, y1, x2, y2, alignment, fitMethod, rotate); }/* w w w .ja va 2s . c om*/ }
From source file:org.jrimum.bopepo.pdf.PdfDocMix.java
License:Apache License
public Image getPdfImage(java.awt.Image image) { Image pdfImage = imagesInUseMap.get(image); if (isNull(pdfImage)) { try {//w w w . j a v a 2 s.com pdfImage = Image.getInstance(image, null); imagesInUseMap.put(image, pdfImage); } catch (Exception ex) { Exceptions.throwIllegalStateException(ex); } } return pdfImage; }
From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java
License:Open Source License
/** * @see org.kuali.kfs.module.tem.pdf.PdfStream#print(java.io.OutputStream) * @throws Exception//from www. j a va 2s .c o m */ @Override public void print(final OutputStream stream) throws Exception { final Font titleFont = FontFactory.getFont(FontFactory.HELVETICA, 20, Font.BOLD); final Font headerFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD); final Font normalFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); final Document doc = new Document(); final PdfWriter writer = PdfWriter.getInstance(doc, stream); doc.open(); if (getDocumentNumber() != null) { Image image = Image.getInstance(new BarcodeHelper().generateBarcodeImage(getDocumentNumber()), null); doc.add(image); } final Paragraph title = new Paragraph("TEM Coversheet", titleFont); doc.add(title); final Paragraph faxNumber = new Paragraph( "Fax this page to " + SpringContext.getBean(ParameterService.class) .getParameterValueAsString(TravelReimbursementDocument.class, FAX_NUMBER), normalFont); doc.add(faxNumber); final Paragraph header = new Paragraph("", headerFont); header.setAlignment(ALIGN_RIGHT); header.add("Document Number: " + getDocumentNumber()); doc.add(header); doc.add(getInstructionsParagraph()); doc.add(getMailtoParagraph()); doc.add(Chunk.NEWLINE); doc.add(getTripInfo()); doc.add(Chunk.NEWLINE); doc.add(getPersonalInfo()); doc.add(Chunk.NEWLINE); doc.add(getExpenses()); drawAlignmentMarks(writer.getDirectContent()); doc.close(); writer.close(); }
From source file:org.oscarehr.web.reports.ocan.IndividualNeedRatingOverTimeReportGenerator.java
License:Open Source License
public void generateReport(OutputStream os) throws Exception { Document d = new Document(PageSize.A4.rotate()); d.setMargins(20, 20, 20, 20);//from w ww .ja v a 2 s. c o m PdfWriter writer = PdfWriter.getInstance(d, os); writer.setStrictImageSequence(true); d.open(); //header Paragraph p = new Paragraph("Individual Need Rating Over Time", titleFont); p.setAlignment(Element.ALIGN_CENTER); d.add(p); d.add(Chunk.NEWLINE); //purpose Paragraph purpose = new Paragraph(); purpose.add(new Chunk("Purpose of Report:", boldText)); purpose.add(new Phrase( "The purpose of this report is to show change over time in a specific Need Rating for an individual Consumer. It adds up the number of needs across all Domains grouped by Need Rating (e.g. Unmet Needs, Met Needs, No Needs, Unknown) for all selected OCANs that were conducted with the Consumer and displays the results in an individual need rating line graph. Each line graph that is displayed compares the Consumer and the Staff's perspective. The staff may share this report with their Consumer as well.", normalText)); d.add(purpose); d.add(Chunk.NEWLINE); //report parameters PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.getDefaultCell().setBorder(0); table.addCell(makeCell(createFieldNameAndValuePhrase("Consumer Name:", reportBean.getConsumerName()), Element.ALIGN_LEFT)); table.addCell(makeCell( createFieldNameAndValuePhrase("Report Date:", dateFormatter.format(reportBean.getReportDate())), Element.ALIGN_RIGHT)); table.addCell(makeCell(createFieldNameAndValuePhrase("Staff Name:", reportBean.getStaffName()), Element.ALIGN_LEFT)); table.addCell(""); d.add(table); d.add(Chunk.NEWLINE); int height = 260; if (reportBean.isShowUnmetNeeds()) { d.add(Image.getInstance(reportBean.getUnmetNeedsChart() .createBufferedImage((int) PageSize.A4.rotate().getWidth() - 40, height), null)); } if (reportBean.isShowMetNeeds()) { d.add(Image.getInstance(reportBean.getMetNeedsChart() .createBufferedImage((int) PageSize.A4.rotate().getWidth() - 40, height), null)); } if (reportBean.isShowNoNeeds()) { d.add(Image.getInstance(reportBean.getNoNeedsChart() .createBufferedImage((int) PageSize.A4.rotate().getWidth() - 40, height), null)); } if (reportBean.isShowUnknownNeeds()) { d.add(Image.getInstance(reportBean.getUnknownNeedsChart() .createBufferedImage((int) PageSize.A4.rotate().getWidth() - 40, height), null)); } d.close(); }
From source file:org.oscarehr.web.reports.ocan.NeedRatingOverTimeReportGenerator.java
License:Open Source License
public void generateReport(OutputStream os) throws Exception { Document d = new Document(PageSize.A4.rotate()); d.setMargins(20, 20, 20, 20);/*from ww w . j a va 2 s .co m*/ PdfWriter writer = PdfWriter.getInstance(d, os); writer.setStrictImageSequence(true); d.open(); //header Paragraph p = new Paragraph("Needs Over Time (Consumer and Staff)", titleFont); p.setAlignment(Element.ALIGN_CENTER); d.add(p); d.add(Chunk.NEWLINE); //purpose Paragraph purpose = new Paragraph(); purpose.add(new Chunk("Purpose of Report:", boldText)); purpose.add(new Phrase( "The purpose of this report is to show change over time in a specific Need Rating for an individual Consumer. It adds up the number of needs across all Domains grouped by Need Rating (e.g. Unmet Needs, Met Needs, No Needs, Unknown) for all selected OCANs that were conducted with the Consumer and displays the results in an individual need rating line graph. Each line graph that is displayed compares the Consumer and the Staff's perspective. The staff may share this report with their Consumer as well.", normalText)); d.add(purpose); d.add(Chunk.NEWLINE); //report parameters PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.getDefaultCell().setBorder(0); table.addCell( makeCell(createFieldNameAndValuePhrase("Consumer Name:", getConsumerName()), Element.ALIGN_LEFT)); table.addCell(makeCell(createFieldNameAndValuePhrase("Report Date:", dateFormatter.format(getReportDate())), Element.ALIGN_RIGHT)); table.addCell(makeCell(createFieldNameAndValuePhrase("Staff Name:", getStaffName()), Element.ALIGN_LEFT)); table.addCell(""); d.add(table); d.add(Chunk.NEWLINE); //loop here...groups of 3 int loopNo = 1; List<OcanNeedRatingOverTimeSummaryOfNeedsBean> summaryBeanList = new ArrayList<OcanNeedRatingOverTimeSummaryOfNeedsBean>(); summaryBeanList.addAll(this.summaryOfNeedsBeanList); while (true) { if (summaryBeanList.size() == 0) { break; } List<OcanNeedRatingOverTimeSummaryOfNeedsBean> currentBeanList = new ArrayList<OcanNeedRatingOverTimeSummaryOfNeedsBean>(); for (int x = 0; x < 3; x++) { if (summaryBeanList.size() == 0) { break; } currentBeanList.add(summaryBeanList.remove(0)); } //summary of needs PdfPTable summaryOfNeedsTable = null; if (currentBeanList.size() == 1) { summaryOfNeedsTable = new PdfPTable(3); summaryOfNeedsTable.setWidthPercentage(100f - 52.8f); summaryOfNeedsTable.setWidths(new float[] { 0.26f, 0.12f, 0.12f }); } if (currentBeanList.size() == 2) { summaryOfNeedsTable = new PdfPTable(6); summaryOfNeedsTable.setWidthPercentage(100f - 26.4f); summaryOfNeedsTable.setWidths(new float[] { 0.26f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } if (currentBeanList.size() == 3) { summaryOfNeedsTable = new PdfPTable(9); summaryOfNeedsTable.setWidthPercentage(100f); summaryOfNeedsTable .setWidths(new float[] { 0.26f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } summaryOfNeedsTable.setHorizontalAlignment(Element.ALIGN_LEFT); summaryOfNeedsTable.setHeaderRows(3); addSummaryOfNeedsHeader(summaryOfNeedsTable, currentBeanList, loopNo); addSummaryOfNeedsRow(summaryOfNeedsTable, "Unmet Needs", "unmet", currentBeanList); addSummaryOfNeedsRow(summaryOfNeedsTable, "Met Needs", "met", currentBeanList); addSummaryOfNeedsRow(summaryOfNeedsTable, "No Needs", "no", currentBeanList); addSummaryOfNeedsRow(summaryOfNeedsTable, "Unknown Needs", "unknown", currentBeanList); d.add(summaryOfNeedsTable); d.add(Chunk.NEWLINE); if (summaryBeanList.size() == 0) { break; } loopNo++; } //BREAKDOWN OF SUMMARY OF NEEDS //loop here...groups of 3 loopNo = 1; List<OcanNeedRatingOverTimeNeedBreakdownBean> breakdownBeanList = new ArrayList<OcanNeedRatingOverTimeNeedBreakdownBean>(); breakdownBeanList.addAll(this.needBreakdownListByOCAN); OcanNeedRatingOverTimeNeedBreakdownBean lastBreakDownBean = null; while (true) { if (breakdownBeanList.size() == 0) { break; } List<OcanNeedRatingOverTimeNeedBreakdownBean> currentBeanList = new ArrayList<OcanNeedRatingOverTimeNeedBreakdownBean>(); for (int x = 0; x < 3; x++) { if (breakdownBeanList.size() == 0) { break; } currentBeanList.add(breakdownBeanList.remove(0)); } //summary of needs PdfPTable summaryOfNeedsTable = null; if (currentBeanList.size() == 1) { if (lastBreakDownBean == null) { summaryOfNeedsTable = new PdfPTable(3); summaryOfNeedsTable.setWidthPercentage(100f - 52.8f); summaryOfNeedsTable.setWidths(new float[] { 0.26f, 0.12f, 0.12f }); } else { summaryOfNeedsTable = new PdfPTable(4); summaryOfNeedsTable.setWidthPercentage(100f - 52.8f); summaryOfNeedsTable.setWidths(new float[] { 0.26f - 0.024f, 0.024f, 0.12f, 0.12f }); } } if (currentBeanList.size() == 2) { if (lastBreakDownBean == null) { summaryOfNeedsTable = new PdfPTable(6); summaryOfNeedsTable.setWidthPercentage(100f - 26.4f); summaryOfNeedsTable.setWidths(new float[] { 0.26f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } else { summaryOfNeedsTable = new PdfPTable(7); summaryOfNeedsTable.setWidthPercentage(100f - 26.4f); summaryOfNeedsTable .setWidths(new float[] { 0.26f - 0.024f, 0.024f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } } if (currentBeanList.size() == 3) { if (lastBreakDownBean == null) { summaryOfNeedsTable = new PdfPTable(9); summaryOfNeedsTable.setWidthPercentage(100f); summaryOfNeedsTable.setWidths( new float[] { 0.26f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } else { summaryOfNeedsTable = new PdfPTable(10); summaryOfNeedsTable.setWidthPercentage(100f); summaryOfNeedsTable.setWidths(new float[] { 0.26f - 0.024f, 0.024f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f, 0.024f, 0.12f, 0.12f }); } } summaryOfNeedsTable.setHorizontalAlignment(Element.ALIGN_LEFT); addSummaryOfNeedsDomainHeader(summaryOfNeedsTable, currentBeanList, loopNo); for (int x = 0; x < domains.size(); x++) { addSummaryOfNeedsDomainRow(summaryOfNeedsTable, x, getDomains(), currentBeanList, lastBreakDownBean); } d.add(summaryOfNeedsTable); d.add(Chunk.NEWLINE); if (breakdownBeanList.size() == 0) { break; } if (currentBeanList.size() == 3) { lastBreakDownBean = currentBeanList.get(2); } loopNo++; } JFreeChart chart = generateNeedsOverTimeChart(); BufferedImage image = chart.createBufferedImage((int) PageSize.A4.rotate().getWidth() - 40, 350); Image image2 = Image.getInstance(image, null); d.add(image2); d.close(); }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfImageHandler.java
License:Open Source License
public Image createImage(RenderableReplacedContentBox content) { try {//from w ww. j a v a 2s.co m final Object o = content.getContent().getRawObject(); if (o instanceof DrawableWrapper) { final DrawableWrapper drawableWrapper = (DrawableWrapper) o; final StrictBounds bounds = new StrictBounds(content.getX(), content.getY(), content.getWidth(), content.getHeight()); DefaultImageReference imageContainer = RenderUtility.createImageFromDrawable(drawableWrapper, bounds, content.getStyleSheet(), metaData); return Image.getInstance(imageContainer.getImage(), Color.WHITE); } if (o instanceof java.awt.Image) { java.awt.Image img = (java.awt.Image) o; return Image.getInstance(img, Color.WHITE); } if (o instanceof URLImageContainer) { final URLImageContainer imageContainer = (URLImageContainer) o; final Image image = createImage(imageContainer); if (image != null) { return image; } } if (o instanceof LocalImageContainer) { final LocalImageContainer imageContainer = (LocalImageContainer) o; final java.awt.Image image = imageContainer.getImage(); return Image.getInstance(image, Color.WHITE); } return null; } catch (IOException e) { logger.info("Unable to load image. Ignoring.", e); } catch (BadElementException e) { logger.info("Unable to load image. Ignoring.", e); } return null; }
From source file:org.unitime.timetable.webutil.pdf.PdfInstructionalOfferingTableBuilder.java
License:Open Source License
private PdfPCell pdfBuildTimePrefCell(ClassAssignmentProxy classAssignment, PreferenceGroup prefGroup, boolean isEditable) { Color color = (isEditable ? sEnableColor : sDisableColor); Assignment a = null;// ww w . j ava 2 s .c o m if (getDisplayTimetable() && isShowTimetable() && classAssignment != null && prefGroup instanceof Class_) { try { a = classAssignment.getAssignment((Class_) prefGroup); } catch (Exception e) { Debug.error(e); } } PdfPCell cell = createCell(); for (Iterator i = prefGroup.effectivePreferences(TimePref.class).iterator(); i.hasNext();) { TimePref tp = (TimePref) i.next(); RequiredTimeTable rtt = tp.getRequiredTimeTable(a == null ? null : a.getTimeLocation()); if (getGridAsText()) { addText(cell, rtt.getModel().toString().replaceAll(", ", "\n"), false, false, Element.ALIGN_LEFT, color, true); } else { try { rtt.getModel().setDefaultSelection(getDefaultTimeGridSize()); if (rtt.getModel().isExactTime()) { addText(cell, rtt.exactTime(false), false, false, Element.ALIGN_LEFT, color, true); } else { java.awt.Image awtImage = rtt.createBufferedImage(getTimeVertival()); Image img = Image.getInstance(awtImage, Color.WHITE); Chunk ck = new Chunk(img, 0, 0); if (cell.getPhrase() == null) { cell.setPhrase(new Paragraph(ck)); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setHorizontalAlignment(Element.ALIGN_LEFT); } else { cell.getPhrase().add(ck); } } } catch (Exception e) { e.printStackTrace(); } } } return cell; }