List of usage examples for com.itextpdf.text.pdf ColumnText getYLine
public float getYLine()
From source file:com.chaschev.itext.ColumnTextBuilder.java
License:Apache License
public ColumnTextBuilder addTruncatedLine(Chunk chunk, boolean addEllipsis) { final float pixelsForEllipsis = 6; try {//from w w w.j ava2s. co m ColumnText dup = ColumnText.duplicate(columnText); final Rectangle oneLineRectangle = new Rectangle(simpleColumnRectangle); oneLineRectangle.setTop(dup.getYLine()); final float fontHeight = calcApproximateFontHeight(chunk.getFont()) * 1.6f; oneLineRectangle.setBottom(dup.getYLine() - fontHeight); if (addEllipsis) { oneLineRectangle.setRight(oneLineRectangle.getRight() - pixelsForEllipsis); } dup.setSimpleColumn(oneLineRectangle); dup.addText(chunk); final int status = dup.go(); float yLine; if (addEllipsis && ColumnText.hasMoreText(status)) { oneLineRectangle.setLeft(dup.getLastX() + 2); oneLineRectangle.setRight(oneLineRectangle.getRight() + pixelsForEllipsis * 2); dup = ColumnText.duplicate(dup); dup.setSimpleColumn(oneLineRectangle); final Chunk ellipses = new Chunk("...\n", chunk.getFont()); dup.setText(new Phrase(ellipses)); dup.go(); yLine = dup.getYLine(); } else { yLine = dup.getYLine(); } setYLine(yLine); return this; } catch (DocumentException e) { throw Exceptions.runtime(e); } }
From source file:com.ideationdesignservices.txtbook.pdf.TxtBookPdf.java
public void doAddNextConversation(Document document, Cursor cursor, ColumnText ct, String senderString, String contentString, Bitmap contentImage, Boolean isVideo, Boolean isMe) { float[][] COLUMNS; if (COLUMNS_REG[0][0] == Txtbook.leftMargin(this.writer, document, this.settings.addFrontCover)) { COLUMNS = COLUMNS_REG;//from ww w . j a v a2 s . c o m } else { COLUMNS = COLUMNS_ALT; } String dateString = PdfObject.NOTHING; if (this.settings.useTimestamps.booleanValue()) { Long date = Long.valueOf(cursor.getLong(cursor.getColumnIndex("normalized_date"))); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy h:mm a", Locale.US); simpleDateFormat.setTimeZone(TimeZone.getDefault()); dateString = simpleDateFormat.format(new Date(date.longValue())); } try { this.currentY = ct.getYLine(); addConversationPart(ct, this.currentColumn, dateString, senderString, contentString, contentImage, isVideo, isMe); if (ColumnText.hasMoreText(ct.go(true))) { this.currentColumn = (this.currentColumn + 1) % 2; if (this.currentColumn == 0) { document.newPage(); if (COLUMNS_REG[0][0] == Txtbook.leftMargin(this.writer, document, this.settings.addFrontCover)) { COLUMNS = COLUMNS_REG; } else { COLUMNS = COLUMNS_ALT; } } ct.setSimpleColumn(COLUMNS[this.currentColumn][0], COLUMNS[this.currentColumn][1], COLUMNS[this.currentColumn][2], COLUMNS[this.currentColumn][3]); this.currentY = COLUMNS[this.currentColumn][3]; } ct.setYLine(this.currentY); ct.setText(null); float width = addConversationPart(ct, this.currentColumn, dateString, senderString, contentString, contentImage, isVideo, isMe); int status = ct.go(false); Document document2 = document; ColumnText columnText = ct; addConversationBackground(document2, columnText, this.currentColumn, this.currentY, width, this.currentY - ct.getYLine(), isMe); } 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. ja va 2 s. 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:gov.utah.dts.det.ccl.documents.reporting.reports.CaseloadReport.java
@Override public void render(Map<String, Object> context, OutputStream outputStream, FileDescriptor descriptor) throws TemplateException { List<Object[]> results = getResults(context); ColumnType columnType = ColumnType.valueOf((String) context.get(COLUMN_TYPE_KEY)); RoleType roleType = RoleType.valueOf((String) context.get(SPECIALIST_TYPE_KEY)); ReportTable<Integer> reportTable = new ReportTable<Integer>(); for (Object[] res : results) { String column = null;//from ww w. j a v a 2s .c o m switch (columnType) { case LICENSE_TYPE: column = (String) res[3]; break; case CITY: column = (String) res[3]; break; case COUNTY: column = locationService.getCounty((String) res[3], (String) res[4], (String) res[5]); break; case ZIP_CODE: String zip = (String) res[5]; if (zip.length() > 5) { zip = zip.substring(0, 5); } column = zip; break; } if (StringUtils.isBlank(column)) { column = "Unknown"; } reportTable.addColumn(column); String specRow = null; String specFnm = (String) res[1]; String specLnm = (String) res[2]; if (StringUtils.isNotBlank(specFnm) || StringUtils.isNotBlank(specLnm)) { PersonalName name = new PersonalName(specFnm, specLnm); specRow = name.getFirstAndLastName(); } else { specRow = "Unknown"; } reportTable.addRow(specRow); Integer val = reportTable.getTableDataItem(specRow, column); if (val == null) { val = new Integer(1); } else { val = new Integer(val.intValue() + 1); } reportTable.addTableDataItem(specRow, column, val); } setFileName(context, descriptor); Document document = new Document(PAGE_SIZE, MARGIN, MARGIN, MARGIN, MARGIN); try { PdfWriter canvas = PdfWriter.getInstance(document, outputStream); document.open(); ColumnText text = new ColumnText(canvas.getDirectContent()); text.setSimpleColumn(document.getPageSize().getLeft(MARGIN), document.getPageSize().getBottom(MARGIN), document.getPageSize().getRight(MARGIN), document.getPageSize().getTop(MARGIN)); StringBuilder sb = new StringBuilder(roleType.getDisplayName()); sb.append(" Caseloads"); Paragraph heading = new Paragraph(sb.toString(), REPORT_HEADING_FONT); heading.setAlignment(Element.ALIGN_CENTER); text.addElement(heading); text.go(); float yLine = text.getYLine(); yLine -= FONT_SIZE; int columns = reportTable.getColumns().size() + 2; String[][] totTab = new String[reportTable.getRows().size()][columns]; float maxLicWidth = 0f; float maxTotWidth = FONT_SIZE; int rowIdx = 0; for (Iterator<String> rowItr = reportTable.getRows().iterator(); rowItr.hasNext();) { String row = rowItr.next(); totTab[rowIdx][0] = row; float licWidth = getTextWidth(row, FONT); if (licWidth > maxLicWidth) { maxLicWidth = licWidth; } int rowTotal = 0; int colIdx = 1; for (Iterator<String> colItr = reportTable.getColumns().iterator(); colItr.hasNext();) { String col = colItr.next(); Integer val = reportTable.getTableDataItem(row, col); if (val != null) { totTab[rowIdx][colIdx] = val.toString(); rowTotal += val.intValue(); } colIdx++; } String rowTotalStr = Integer.toString(rowTotal); float totWidth = getTextWidth(rowTotalStr, TABLE_HEADER_FONT); if (totWidth > maxTotWidth) { maxTotWidth = totWidth; } totTab[rowIdx][columns - 1] = rowTotalStr; rowIdx++; } //add border and cell padding to licensor width maxLicWidth += (TABLE_CELL_PADDING * 2) + (TABLE_BORDER_SIZE * 2); maxTotWidth += (TABLE_CELL_PADDING * 2) + (TABLE_BORDER_SIZE * 2); float width = PAGE_SIZE.getWidth() - (2 * MARGIN); width -= maxLicWidth; float[] colWidths = new float[columns]; colWidths[0] = maxLicWidth; for (int i = 1; i < columns; i++) { colWidths[i] = maxTotWidth; } PdfPTable table = new PdfPTable(reportTable.getColumns().size() + 2); table.setHeaderRows(1); table.setLockedWidth(true); table.setTotalWidth(colWidths); table.setSpacingBefore(FONT_SIZE); setDefaultCellAttributes(table.getDefaultCell()); PdfPCell cell = null; cell = new PdfPCell(new Phrase("")); cell.setBorderWidthTop(0); cell.setBorderWidthLeft(0); table.addCell(cell); for (Iterator<String> itr = reportTable.getColumns().iterator(); itr.hasNext();) { cell = getRotatedHeaderCell(itr.next()); table.addCell(cell); } cell = getRotatedHeaderCell("Total"); table.addCell(cell); for (String[] row : totTab) { for (int i = 0; i < row.length; i++) { if (row[i] == null) { table.addCell(getNumberCell("")); } else { table.addCell(getCell(new Phrase(row[i], i == row.length - 1 ? TABLE_HEADER_FONT : FONT), i == 0 ? Element.ALIGN_LEFT : Element.ALIGN_CENTER)); } } } writeTable(table, document, canvas.getDirectContent(), colWidths, yLine, true); } catch (DocumentException de) { throw new TemplateException(de); } document.close(); }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private void drawOverlayText(PdfContentByte canvas, List<Rectangle> textRectangles, PdfString overlayText, PdfString otDA, PdfNumber otQ, PdfBoolean otRepeat) throws DocumentException, IOException { ColumnText ct = new ColumnText(canvas); ct.setLeading(0, 1.2F);/*from w w w. j av a 2 s . c o m*/ ct.setUseAscender(true); String otStr = overlayText.toUnicodeString(); canvas.saveState(); Map<String, List> parsedDA = parseDAParam(otDA); Font font = null; if (parsedDA.containsKey(STROKE_COLOR)) { List strokeColorArgs = parsedDA.get(STROKE_COLOR); setStrokeColor(canvas, strokeColorArgs); } if (parsedDA.containsKey(FILL_COLOR)) { List fillColorArgs = parsedDA.get(FILL_COLOR); setFillColor(canvas, fillColorArgs); } if (parsedDA.containsKey("Tf")) { List tfArgs = parsedDA.get("Tf"); font = retrieveFontFromAcroForm((PdfName) tfArgs.get(0), (PdfNumber) tfArgs.get(1)); } for (Rectangle textRect : textRectangles) { ct.setSimpleColumn(textRect); if (otQ != null) { ct.setAlignment(otQ.intValue()); } Phrase otPhrase; if (font != null) { otPhrase = new Phrase(otStr, font); } else { otPhrase = new Phrase(otStr); } float y = ct.getYLine(); if (otRepeat != null && otRepeat.booleanValue()) { int status = ct.go(true); while (!ColumnText.hasMoreText(status)) { otPhrase.add(otStr); ct.setText(otPhrase); ct.setYLine(y); status = ct.go(true); } } ct.setText(otPhrase); ct.setYLine(y); ct.go(); } canvas.restoreState(); }