List of usage examples for com.itextpdf.text Image getWidth
public float getWidth()
From source file:cz.zcu.kiv.eegdatabase.logic.pdf.PDFUtils.java
License:Apache License
public Paragraph setHeader(Document document, String title) throws IOException, BadElementException { Paragraph paragraph = new Paragraph(title, FontFactory.getFont(FontFactory.HELVETICA, 14, Font.BOLD, BaseColor.BLACK)); paragraph.setAlignment(Element.ALIGN_CENTER); Image img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + HEADERIMG)); float topMargin = img.getHeight() + 2 * PADDING[0]; img.setAbsolutePosition(PADDING[3], (PageSize.A4.getHeight() - img.getHeight()) - PADDING[0]); paragraph.add(img);//www. ja v a 2 s . co m img = GetResizedAndCenteredImage(path + convertPath("/files/images/" + FOOTERIMG)); img.setAbsolutePosition((PageSize.A4.getWidth() - img.getWidth()) / 2, PADDING[2]); paragraph.add(img); float bottomMargin = img.getHeight() + 2 * PADDING[2]; document.setMargins(topMargin, PADDING[1], bottomMargin, PADDING[3]); paragraph.setSpacingAfter(1.5f * PADDING[0]); return paragraph; }
From source file:cz.zcu.kiv.eegdatabase.logic.pdf.PDFUtils.java
License:Apache License
private Image GetResizedAndCenteredImage(String filename) throws IOException, BadElementException { Image img = Image.getInstance(filename); if (img.getWidth() <= PageSize.A4.getWidth() - PADDING[1] - PADDING[3]) { return img; }/*from w w w. j ava 2 s .c om*/ float newWidth = PageSize.A4.getWidth() - PADDING[1] - PADDING[3]; float ratio = newWidth / img.getWidth(); float newHeight = img.getHeight() * ratio; img.scaleAbsolute(newWidth, newHeight); return img; }
From source file:de.jost_net.JVerein.io.Reporter.java
License:Open Source License
public void addColumn(byte[] image, int width, int height, int horizontalalignment) throws BadElementException, MalformedURLException, IOException { Image i = Image.getInstance(image); float w = i.getWidth() / width; float h = i.getHeight() / height; if (w > h) { h = i.getHeight() / w;//from ww w . j ava 2 s . co m w = width; } else { w = i.getHeight() / h; h = height; } i.scaleToFit(w, h); PdfPCell cell = new PdfPCell(i, false); cell.setPadding(3); cell.setHorizontalAlignment(horizontalalignment); table.addCell(cell); }
From source file:Evento.action.ZapisDoPdfAction.java
License:Apache License
public void createPDF(String[] imgURL, String place, String album) throws DocumentException { Document document = new Document(); Rectangle pageSize = new Rectangle(szerokosc, wysokosc); document.setPageSize(pageSize);//w w w.java 2 s. c o m try { PdfWriter.getInstance(document, new FileOutputStream(new File(place, "nowy.pdf"))); document.open(); Image tlo = Image.getInstance(new URL(zdjecieTla)); tlo.setAbsolutePosition(0f, 0f); document.add(tlo); Paragraph preface = new Paragraph(album, new Font(FontFamily.HELVETICA, 72, Font.BOLDITALIC, new BaseColor(255, 255, 255))); preface.setAlignment(Element.ALIGN_CENTER); document.add(preface); document.newPage(); for (int i = 0; i < imgURL.length; i++) { Image tlo2 = Image.getInstance(new URL(zdjecieTla)); tlo2.setAbsolutePosition(0f, 0f); document.add(tlo2); Image image2 = Image.getInstance(new URL(imgURL[i])); if (szerokosc * 1.5f <= image2.getWidth() || wysokosc * 1.5f <= image2.getHeight()) { image2.scaleAbsolute(image2.getWidth() * 0.25f, image2.getHeight() * 0.25f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 0.25f) / 2, wysokosc / 2 - (image2.getHeight() * 0.25f) / 2); } else if ((szerokosc * 0.8f <= image2.getWidth() || wysokosc * 0.8f <= image2.getHeight()) && (szerokosc * 1.2f >= image2.getWidth() || wysokosc * 1.2f >= image2.getHeight())) { image2.scaleAbsolute(image2.getWidth() * 0.8f, image2.getHeight() * 0.8f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 0.8f) / 2, wysokosc / 2 - (image2.getHeight() * 0.8f) / 2); } else if ((szerokosc * 0.4f >= image2.getWidth() || wysokosc * 0.4f >= image2.getHeight()) && (szerokosc * 0.7f <= image2.getWidth() || wysokosc * 0.7f <= image2.getHeight())) { image2.scaleAbsolute(image2.getWidth() * 1.4f, image2.getHeight() * 1.4f); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth() * 1.4f) / 2, wysokosc / 2 - (image2.getHeight() * 1.4f) / 2); } else { image2.scaleAbsolute(image2.getWidth(), image2.getHeight()); image2.setAbsolutePosition(szerokosc / 2f - (image2.getWidth()) / 2, wysokosc / 2 - (image2.getHeight()) / 2); } for (int k = 0; k <= 1000; k++) ; for (int j = 0; j <= 1000; j++) ; document.add(image2); document.newPage(); } document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:eyeofthetiger.utils.PDFDossardGenerator.java
public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException { sortParticipants(participants);//w ww. j a va 2 s . com 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; }// w ww. j ava 2 s.com 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: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); }/*w w w.j a va 2s .c om*/ 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 w w w .ja v a 2 s .co m*/ 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:io.github.nick11roberts.pdf_drive.PreviewActivity.java
License:Open Source License
public void addImagesToPDF() throws Exception { Document document = new Document(); ByteArrayOutputStream stream; Bitmap bmpImage;//from w w w. j ava2 s.co m byte[] imagebytes; Image image; float scaler; PdfWriter.getInstance(document, new FileOutputStream(tmpPdfFile)); document.open(); for (int i = camIndex - 1; i >= 0; i--) { stream = new ByteArrayOutputStream(); bmpImage = Bitmap.createBitmap(imagesFromCamera.get(i)); //bmpImage = rotateImage(bmpImage, -90); bmpImage.compress(Bitmap.CompressFormat.JPEG, 100, stream); imagebytes = stream.toByteArray(); image = Image.getInstance(imagebytes); scaler = ((document.getPageSize().getWidth()) / image.getWidth()) * 100; image.scalePercent(scaler); image.setAlignment(Image.MIDDLE | Image.ALIGN_MIDDLE); document.add(image); document.newPage(); stream.close(); stream = null; } document.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 w w w . ja v a 2s.c om } if (imageHeight < h) { h = imageHeight; } } if (imageWidth >= w || imageHeight >= h) { scaled = true; } if (scaled) { image.scaleAbsolute(w - 2, h - 2); } return image; }