List of usage examples for com.lowagie.text.pdf PdfContentByte setColorFill
public void setColorFill(Color color)
From source file:gov.anl.aps.cdb.portal.plugins.support.icmsLink.IcmsWatermarkUtility.java
License:Open Source License
/** * Adds a stamp of some metadata to ICMS documents. * * Function Credit: Thomas Fors/* w w w . j a va 2 s . c o m*/ * * @return byte array ologgerf the stamped PDF file * @throws DocumentException - Error loading pdfstamper or creating font * @throws IOException - Error performing IO operation * @throws Base64DecodingException - Error converting downloadContent string to byte[] */ private byte[] addWatermarkToPDFFile() throws Base64DecodingException, DocumentException, IOException { byte[] pdfBytes = Base64.decode(downloadContentBase64Encoded); DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yy hh:mm:ss a"); LocalDateTime now = LocalDateTime.now(); String downloadTime = dtf.format(now); UserInfo user = (UserInfo) SessionUtility.getUser(); String username = null; if (user != null) { username = user.getUsername(); } else { username = "unknown user"; } String bottomMessage = "Downloaded via APS CDB by: " + username + " at " + downloadTime; controlledRev = updateOptionalValue(controlledRev); dnsCollectionId = updateOptionalValue(dnsCollectionId); dnsDocNumber = updateOptionalValue(dnsDocNumber); String watermarkContents = "Content ID: " + docName; watermarkContents += " Rev: " + controlledRev; watermarkContents += " Released: " + date; watermarkContents += " DNS Collection ID: " + dnsCollectionId; watermarkContents += " DNS Document ID: " + dnsDocNumber; PdfReader pdfReader = new PdfReader(pdfBytes); int n = pdfReader.getNumberOfPages(); ByteArrayOutputStream out = new ByteArrayOutputStream(); PdfStamper stamp = new PdfStamper(pdfReader, out); PdfContentByte over; BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); for (int i = 0; i < n; i++) { over = stamp.getOverContent(i + 1); over.beginText(); over.setTextMatrix(30, 30); over.setFontAndSize(bf, 10); over.setColorFill(new Color(0x80, 0x80, 0x80)); over.showTextAligned(Element.ALIGN_LEFT, watermarkContents, 25, 25, 90); over.showTextAligned(Element.ALIGN_LEFT, bottomMessage, 50, 10, 0); if (status.equals(ICMS_UNDER_REV_STATUS)) { over.setColorFill(new Color(0xFF, 0x00, 0x00)); } //over.showTextAligned(Element.ALIGN_LEFT, status, 25, 25 + bf.getWidthPoint(watermarkContents + " - ", 10), 90); over.endText(); } stamp.close(); return out.toByteArray(); }
From source file:net.sf.jasperreports.engine.export.PdfGlyphGraphics2D.java
License:Open Source License
@Override public void drawGlyphVector(GlyphVector glyphVector, float x, float y) { Font awtFont = glyphVector.getFont(); Map<Attribute, Object> fontAttrs = new HashMap<Attribute, Object>(); Map<TextAttribute, ?> awtFontAttributes = awtFont.getAttributes(); fontAttrs.putAll(awtFontAttributes); //the following relies on FontInfo.getFontInfo matching the face/font name com.lowagie.text.Font currentFont = pdfExporter.getFont(fontAttrs, locale, false); boolean bold = (currentFont.getStyle() & com.lowagie.text.Font.BOLD) != 0; boolean italic = (currentFont.getStyle() & com.lowagie.text.Font.ITALIC) != 0; PdfContentByte text = pdfContentByte.getDuplicate(); text.beginText();//from w ww . j a va 2 s. c o m float[] originalCoords = new float[] { x, y }; float[] transformedCoors = new float[2]; getTransform().transform(originalCoords, 0, transformedCoors, 0, 1); text.setTextMatrix(1, 0, italic ? ITALIC_ANGLE : 0f, 1, transformedCoors[0], pdfExporter.getCurrentPageFormat().getPageHeight() - transformedCoors[1]); double scaleX = awtFont.getTransform().getScaleX(); double scaleY = awtFont.getTransform().getScaleY(); double minScale = Math.min(scaleX, scaleY); text.setFontAndSize(currentFont.getBaseFont(), (float) (minScale * awtFont.getSize2D())); if (bold) { text.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); text.setLineWidth(currentFont.getSize() * BOLD_STRIKE_FACTOR); text.setColorStroke(getColor()); } text.setColorFill(getColor()); //FIXME find a way to determine the characters that correspond to this glyph vector // so that we can map the font glyphs that do not directly map to a character text.showText(glyphVector); text.resetRGBColorFill(); if (bold) { text.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); text.setLineWidth(1f); text.resetRGBColorStroke(); } text.endText(); pdfContentByte.add(text); }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void stampText(int pageCount, PdfPageData currentPageData, final StampTextToPDFPages stampText) { File tempFile = null;/* w w w.jav a 2s . c o m*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = stampText.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { String chosenText = stampText.getText(); if (!chosenText.equals("")) { String chosenFont = stampText.getFontName(); Color chosenFontColor = stampText.getFontColor(); int chosenFontSize = stampText.getFontSize(); int chosenRotation = stampText.getRotation(); String chosenPlacement = stampText.getPlacement(); String chosenHorizontalPosition = stampText.getHorizontalPosition(); String chosenVerticalPosition = stampText.getVerticalPosition(); float chosenHorizontalOffset = stampText.getHorizontalOffset(); float chosenVerticalOffset = stampText.getVerticalOffset(); BaseFont font = BaseFont.createFont(chosenFont, BaseFont.WINANSI, false); PdfContentByte cb; if (chosenPlacement.equals("Overlay")) cb = stamp.getOverContent(page); else cb = stamp.getUnderContent(page); cb.beginText(); cb.setColorFill(chosenFontColor); cb.setFontAndSize(font, chosenFontSize); int currentRotation = currentPageData.getRotation(page); Rectangle pageSize; if (currentRotation == 90 || currentRotation == 270) pageSize = reader.getPageSize(page).rotate(); else pageSize = reader.getPageSize(page); float startx; float starty; if (chosenVerticalPosition.equals("From the top")) { starty = pageSize.height(); } else if (chosenVerticalPosition.equals("Centered")) { starty = pageSize.height() / 2; } else { starty = 0; } if (chosenHorizontalPosition.equals("From the left")) { startx = 0; } else if (chosenHorizontalPosition.equals("Centered")) { startx = pageSize.width() / 2; } else { startx = pageSize.width(); } cb.showTextAligned(Element.ALIGN_CENTER, chosenText, startx + chosenHorizontalOffset, starty + chosenVerticalOffset, chosenRotation); cb.endText(); } } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void addHeaderFooter(int pageCount, PdfPageData currentPageData, final AddHeaderFooterToPDFPages addHeaderFooter) { File tempFile = null;/* w w w .j a v a2 s .c om*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToEdit = addHeaderFooter.getPages(); if (pgsToEdit == null) return; List pagesToEdit = new ArrayList(); for (int i = 0; i < pgsToEdit.length; i++) pagesToEdit.add(new Integer(pgsToEdit[i])); final PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); String chosenFont = addHeaderFooter.getFontName(); Color chosenFontColor = addHeaderFooter.getFontColor(); int chosenFontSize = addHeaderFooter.getFontSize(); float chosenLeftRightMargin = addHeaderFooter.getLeftRightMargin(); float chosenTopBottomMargin = addHeaderFooter.getTopBottomMargin(); String text[] = new String[6]; text[0] = addHeaderFooter.getLeftHeader(); text[1] = addHeaderFooter.getCenterHeader(); text[2] = addHeaderFooter.getRightHeader(); text[3] = addHeaderFooter.getLeftFooter(); text[4] = addHeaderFooter.getCenterFooter(); text[5] = addHeaderFooter.getRightFooter(); Date date = new Date(); String shortDate = DateFormat.getDateInstance(DateFormat.SHORT).format(date); String longDate = DateFormat.getDateInstance(DateFormat.LONG).format(date); SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss a"); String time12 = formatter.format(date); formatter = new SimpleDateFormat("HH.mm.ss"); String time24 = formatter.format(date); String fileName = new File(selectedFile).getName(); BaseFont font = BaseFont.createFont(chosenFont, BaseFont.WINANSI, false); for (int page = 1; page <= pageCount; page++) { if (pagesToEdit.contains(new Integer(page))) { String[] textCopy = new String[text.length]; System.arraycopy(text, 0, textCopy, 0, text.length); for (int i = 0; i < 6; i++) { textCopy[i] = textCopy[i].replaceAll("<d>", shortDate); textCopy[i] = textCopy[i].replaceAll("<D>", longDate); textCopy[i] = textCopy[i].replaceAll("<t>", time12); textCopy[i] = textCopy[i].replaceAll("<T>", time24); textCopy[i] = textCopy[i].replaceAll("<f>", fileName); textCopy[i] = textCopy[i].replaceAll("<F>", selectedFile); textCopy[i] = textCopy[i].replaceAll("<p>", "" + page); textCopy[i] = textCopy[i].replaceAll("<P>", "" + pageCount); } PdfContentByte cb = stamp.getOverContent(page); cb.beginText(); cb.setColorFill(chosenFontColor); cb.setFontAndSize(font, chosenFontSize); Rectangle pageSize = reader.getPageSizeWithRotation(page); cb.showTextAligned(Element.ALIGN_LEFT, textCopy[0], chosenLeftRightMargin, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_CENTER, textCopy[1], pageSize.width() / 2, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_RIGHT, textCopy[2], pageSize.width() - chosenLeftRightMargin, pageSize.height() - chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_LEFT, textCopy[3], chosenLeftRightMargin, chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_CENTER, textCopy[4], pageSize.width() / 2, chosenTopBottomMargin, 0); cb.showTextAligned(Element.ALIGN_RIGHT, textCopy[5], pageSize.width() - chosenLeftRightMargin, chosenTopBottomMargin, 0); cb.endText(); } } stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
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. *//*from ww w . java 2 s .c o 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.kfs.module.tem.pdf.Coversheet.java
License:Open Source License
protected void upperLeftAlignmentMark(final PdfContentByte cb) { cb.saveState();//www. jav a2 s . c om cb.rectangle(ALIGNMENT_MARGIN, (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARK_HEIGHT - ALIGNMENT_MARGIN, ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARK_HEIGHT); cb.setColorFill(BLACK); cb.fill(); cb.restoreState(); }
From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java
License:Open Source License
protected void lowerLeftAlignmentMark(final PdfContentByte cb) { cb.saveState();//from w w w . ja v a 2s.c o m cb.rectangle(ALIGNMENT_MARGIN, ALIGNMENT_MARGIN, ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARK_HEIGHT); cb.setColorFill(BLACK); cb.fill(); cb.restoreState(); }
From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java
License:Open Source License
protected void lowerRightAlignmentMark(final PdfContentByte cb) { cb.saveState();/* ww w. ja va2s . c o m*/ cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARGIN, ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARK_HEIGHT); cb.setColorFill(BLACK); cb.fill(); cb.restoreState(); }
From source file:org.kuali.kfs.module.tem.pdf.Coversheet.java
License:Open Source License
protected void upperRightAlignmentMark(final PdfContentByte cb) { cb.saveState();// w w w . j a v a 2 s .c o m cb.rectangle(LETTER.width() - (ALIGNMENT_MARGIN * 4) - ALIGNMENT_MARK_WIDTH, (LETTER.height() + TOP_MARGIN) - ALIGNMENT_MARGIN - ALIGNMENT_MARK_HEIGHT, ALIGNMENT_MARK_WIDTH, ALIGNMENT_MARK_HEIGHT); cb.setColorFill(BLACK); cb.fill(); cb.restoreState(); }
From source file:org.kuali.kfs.sys.PdfFormFillerUtil.java
License:Open Source License
/** * This method creates a Final watermark on the input Stream. * * @param templateStream/*from w w w .j a v a 2 s .c om*/ * @param finalmarkText * @return * @throws IOException * @throws DocumentException */ public static byte[] createFinalmarkOnFile(byte[] templateStream, String finalmarkText) throws IOException, DocumentException { // Create a PDF reader for the template PdfReader pdfReader = new PdfReader(templateStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // Create a PDF writer PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream); int n = pdfReader.getNumberOfPages(); int i = 1; PdfContentByte over; BaseFont bf; try { bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); PdfGState gstate = new PdfGState(); while (i <= n) { // Watermark under the existing page Rectangle pageSize = pdfReader.getPageSizeWithRotation(i); over = pdfStamper.getOverContent(i); over.beginText(); over.setFontAndSize(bf, 8); over.setGState(gstate); over.setColorFill(Color.BLACK); over.showTextAligned(Element.ALIGN_CENTER, finalmarkText, (pageSize.width() / 2), (pageSize.height() - 10), 0); over.endText(); i++; } pdfStamper.close(); } catch (DocumentException ex) { throw new IOException("iText error creating final watermark on PDF", ex); } catch (IOException ex) { throw new IOException("IO error creating final watermark on PDF", ex); } return outputStream.toByteArray(); }