List of usage examples for com.lowagie.text.pdf PdfContentByte getEffectiveStringWidth
public float getEffectiveStringWidth(String text, boolean kerned)
From source file:com.actelion.research.spiritapp.print.CagePrinterPDF.java
License:Open Source License
private static void print(PdfContentByte canvas, String txt, float x, float y, float maxX, int maxHeight, int lineHeight, String fontName, Color color, float fontSize) throws Exception { float width = maxX - x - 5; float minY = Math.min(y, y - maxHeight + lineHeight - 3); if (txt == null) return;// w w w. j a v a2 s . c o m canvas.setFontAndSize(BaseFont.createFont(fontName, "Cp1252", false), fontSize); while (txt.length() > 0 && y >= minY) { int index = 0; while (true) { int index2 = index + 1; if (index2 >= txt.length()) { index = index2; break; } float w = canvas.getEffectiveStringWidth(txt.substring(0, index2), true); System.out.println("CagePrinterPDF.print() " + txt.substring(0, index2) + " " + w + "," + width); if (w > width) break; index = index2; } if (index < 0) index = txt.length(); String t = txt.substring(0, index); System.out.println( "CagePrinterPDF.print() " + t + " at " + x + "x" + y + " (of " + txt + ") minY=" + minY); txt = txt.substring(index).trim(); Phrase phrase = new Paragraph(t, FontFactory.getFont(fontName, fontSize, color)); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, x, y, 0); y -= lineHeight; } }
From source file:org.kuali.coeus.common.impl.print.watermark.WatermarkServiceImpl.java
License:Open Source License
/** * This method is for setting the properties of watermark Text. *///w ww . j a v a 2 s . co m private void decoratePdfWatermarkText(PdfContentByte pdfContentByte, Rectangle rectangle, WatermarkBean watermarkBean) { float x, y, x1, y1, angle; final float OPACITY = 0.3f; PdfGState pdfGState = new PdfGState(); pdfGState.setFillOpacity(OPACITY); int pageWidth = (int) rectangle.getWidth(); int pageHeight = (int) rectangle.getHeight(); try { if (watermarkBean.getType().equalsIgnoreCase(WatermarkConstants.WATERMARK_TYPE_TEXT)) { pdfContentByte.beginText(); pdfContentByte.setGState(pdfGState); Color fillColor = watermarkBean.getFont().getColor() == null ? WatermarkConstants.DEFAULT_WATERMARK_COLOR : watermarkBean.getFont().getColor(); pdfContentByte.setColorFill(fillColor); if (watermarkBean.getPosition().equals(WatermarkConstants.WATERMARK_POSITION_FOOTER)) { pdfContentByte.setFontAndSize(watermarkBean.getFont().getBaseFont(), watermarkBean.getPositionFont().getSize()); if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_CENTER)) { pdfContentByte.showTextAligned(Element.ALIGN_CENTER, watermarkBean.getText(), (rectangle.getLeft(rectangle.getBorderWidthLeft()) + rectangle.getRight(rectangle.getBorderWidthRight())) / 2, rectangle.getBottom(rectangle.getBorderWidthBottom() + watermarkBean.getPositionFont().getSize()), 0); } else if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_RIGHT)) { pdfContentByte.showTextAligned(Element.ALIGN_RIGHT, watermarkBean.getText(), rectangle.getRight(rectangle.getBorderWidthRight()), rectangle.getBottom(rectangle.getBorderWidthBottom() + watermarkBean.getPositionFont().getSize()), 0); } else if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_LEFT)) { pdfContentByte.showTextAligned(Element.ALIGN_LEFT, watermarkBean.getText(), rectangle.getLeft(rectangle.getBorderWidthLeft()), rectangle.getBottom(rectangle.getBorderWidthBottom() + watermarkBean.getPositionFont().getSize()), 0); } } else if (watermarkBean.getPosition().equals(WatermarkConstants.WATERMARK_POSITION_HEADER)) { pdfContentByte.setFontAndSize(watermarkBean.getFont().getBaseFont(), watermarkBean.getPositionFont().getSize()); if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_CENTER)) { pdfContentByte.showTextAligned(Element.ALIGN_CENTER, watermarkBean.getText(), (rectangle.getLeft(rectangle.getBorderWidthLeft()) + rectangle.getRight(rectangle.getBorderWidthRight())) / 2, rectangle.getTop( rectangle.getBorderWidthTop() + watermarkBean.getPositionFont().getSize()), 0); } else if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_RIGHT)) { pdfContentByte.showTextAligned(Element.ALIGN_RIGHT, watermarkBean.getText(), rectangle.getRight(rectangle.getBorderWidthRight()), rectangle.getTop( rectangle.getBorderWidthTop() + watermarkBean.getPositionFont().getSize()), 0); } else if (watermarkBean.getAlignment().equals(WatermarkConstants.ALIGN_LEFT)) { pdfContentByte.showTextAligned(Element.ALIGN_LEFT, watermarkBean.getText(), rectangle.getLeft(rectangle.getBorderWidthLeft()), rectangle.getTop( rectangle.getBorderWidthTop() + watermarkBean.getPositionFont().getSize()), 0); } } else { pdfContentByte.setFontAndSize(watermarkBean.getFont().getBaseFont(), watermarkBean.getFont().getSize()); int textWidth = (int) pdfContentByte.getEffectiveStringWidth(watermarkBean.getText(), false); int diagonal = (int) Math.sqrt((pageWidth * pageWidth) + (pageHeight * pageHeight)); int pivotPoint = (diagonal - textWidth) / 2; angle = (float) Math.atan((float) pageHeight / pageWidth); x = (float) (pivotPoint * pageWidth) / diagonal; y = (float) (pivotPoint * pageHeight) / diagonal; x1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.sin(angle)); y1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.cos(angle)); pdfContentByte.showTextAligned(Element.ALIGN_LEFT, watermarkBean.getText(), x + x1, y - y1, (float) Math.toDegrees(angle)); } pdfContentByte.endText(); } } catch (Exception exception) { LOG.error("Exception occured in WatermarkServiceImpl. Water mark Exception: " + exception.getMessage()); } }
From source file:org.kuali.kra.printing.service.impl.WatermarkServiceImpl.java
License:Educational Community License
/** * This method is for setting the properties of watermark Text. * //ww w . jav a 2 s . c o m * @param pdfContentByte * @param pageWidth * @param pageHeight * @param watermarkBean */ private void decoratePdfWatermarkText(PdfContentByte pdfContentByte, int pageWidth, int pageHeight, WatermarkBean watermarkBean) { float x, y, x1, y1, angle; try { if (watermarkBean.getType().equalsIgnoreCase(WatermarkConstants.WATERMARK_TYPE_TEXT)) { pdfContentByte.beginText(); pdfContentByte.setFontAndSize(watermarkBean.getFont().getBaseFont(), watermarkBean.getFont().getSize()); Color fillColor = watermarkBean.getFont().getColor() == null ? WatermarkConstants.DEFAULT_WATERMARK_COLOR : watermarkBean.getFont().getColor(); pdfContentByte.setColorFill(fillColor); int textWidth = (int) pdfContentByte.getEffectiveStringWidth(watermarkBean.getText(), false); int diagonal = (int) Math.sqrt((pageWidth * pageWidth) + (pageHeight * pageHeight)); int pivotPoint = (diagonal - textWidth) / 2; angle = (float) Math.atan((float) pageHeight / pageWidth); x = (float) (pivotPoint * pageWidth) / diagonal; y = (float) (pivotPoint * pageHeight) / diagonal; x1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.sin(angle)); y1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.cos(angle)); pdfContentByte.showTextAligned(Element.ALIGN_LEFT, watermarkBean.getText(), x + x1, y - y1, (float) Math.toDegrees(angle)); pdfContentByte.endText(); } } catch (Exception exception) { LOG.error("Exception occured in WatermarkServiceImpl. Water mark Exception: " + exception.getMessage()); } }