List of usage examples for com.itextpdf.text Image getAbsoluteY
public float getAbsoluteY()
From source file:com.vectorprint.report.itext.VectorPrintDocument.java
License:Open Source License
/** * * @param image/* www . j av a 2 s. c om*/ * @param hook * @param prefix * @param wrap * @return true when the image was added to the document * @throws DocumentException */ private boolean tracePosition(Image image, AddElementHook hook, String prefix, boolean wrap) throws DocumentException { String gt = prefix + hook.styleClass; if (wrap && image.hasAbsoluteX() && image.hasAbsoluteY()) { // tracing position when an image is absolutely positioned by wrapping it in a chunk Chunk wrapper = new Chunk(image, (Float.NaN == image.getAbsoluteX()) ? 0 : image.getAbsoluteX(), (Float.NaN == image.getAbsoluteY()) ? 0 : image.getAbsoluteY(), true); try { styleHelper.delayedStyle(wrapper, gt, StyleHelper.getStylers(factory.getStylers(gt.replaceFirst(prefix, "")), Advanced.class), eventHelper); } catch (VectorPrintException ex) { throw new DocumentException(ex); } return super.add(wrapper); } else { // the chunk will provide feedback on the actual x and y of the image when onGenericTag is fired, // the width and height of the image are passed to the eventhelper Chunk chunk = positionChunk(); try { styleHelper.delayedStyle(chunk, gt, StyleHelper.getStylers(factory.getStylers(gt.replaceFirst(prefix, "")), Advanced.class), eventHelper, image); } catch (VectorPrintException ex) { throw new DocumentException(ex); } super.add(chunk); return false; } }
From source file:eyeofthetiger.utils.PDFDossardGenerator.java
public void createPdf(List<Participant> participants, OutputStream out) throws IOException, DocumentException { sortParticipants(participants);// ww w.j a v a2s. 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:info.longnetpro.examples.PdfLibExamples.java
public static void generatePdf() throws DocumentException, URISyntaxException, MalformedURLException, IOException { String licFile = getLicenseFilePath(); loadLicenseFile(licFile);//from w ww .ja v a 2s . 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(); }