List of usage examples for com.lowagie.text.pdf PdfContentByte beginText
public void beginText()
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;// ww w .j a va2s. c o m 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. *//*w w w . j a v a 2s .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.fp.document.service.impl.CashReceiptCoverSheetServiceImpl.java
License:Open Source License
/** * * This method writes out the value provided to the output provided and aligns the value outputted using the xPos float * provided./*from w w w. ja va 2 s . co m*/ * @param output The content byte used to write out the field to the PDF template. * @param xPos The x coordinate of the starting point on the document where the value will be written to. * @param fieldValue The value to be written to the PDF cover sheet. */ protected void writeCheckField(PdfContentByte output, float xPos, String fieldValue) { output.beginText(); output.setTextMatrix(xPos, getCurrentRenderingYPosition()); output.newlineShowText(fieldValue); output.endText(); }
From source file:org.kuali.kfs.module.purap.pdf.PurapPdf.java
License:Open Source License
/** * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that * this is a Test document and include the environment, if the environment is not a production environment. * * @param writer The PdfWriter for this document. * @param document The document./*from w w w.j a va 2 s . c om*/ * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ @Override public void onStartPage(PdfWriter writer, Document document) { if (!KRADUtils.isProductionEnvironment()) { PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.beginText(); cb.setFontAndSize(helv, 48); String watermarkText = "Test document (" + environment + ")"; cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2, document.getPageSize().height() / 2, 45); cb.endText(); cb.restoreState(); } if (GlobalVariables.getUserSession() != null && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) { GlobalVariables.getUserSession().removeObject("isPreview"); PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.beginText(); cb.setFontAndSize(helv, 48); String watermarkText = "DRAFT"; cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2, document.getPageSize().height() / 2, 45); cb.endText(); cb.restoreState(); } }
From source file:org.kuali.kfs.module.purap.pdf.PurapPdf.java
License:Open Source License
/** * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the * footer./*from www. j av a 2s.c o m*/ * * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ @Override public void onEndPage(PdfWriter writer, Document document) { LOG.debug("onEndPage() started."); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headerTable headerTable.setTotalWidth(document.right() - document.left()); headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb); // compose the footer String text = "Page " + writer.getPageNumber() + " of "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); // show the footer float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); }
From source file:org.kuali.kfs.module.purap.pdf.PurchaseOrderQuoteRequestsPdf.java
License:Open Source License
/** * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the * footer./*w w w. j a va2s .c o m*/ * * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onEndPage(PdfWriter writer, Document document) { LOG.debug("onEndPage() started."); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // compose the footer String text = "Page " + writer.getPageNumber() + " of "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(helv, 12); // show the footer float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); }
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 ww.j a v a 2s. 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(); }
From source file:org.kuali.kfs.sys.PdfFormFillerUtil.java
License:Open Source License
/** * This Method creates a custom watermark on the File. * * @param templateStream//from www . ja va 2 s . c om * @param watermarkText * @return * @throws IOException * @throws DocumentException */ public static byte[] createWatermarkOnFile(byte[] templateStream, String watermarkText) 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_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); PdfGState gstate = new PdfGState(); gstate.setFillOpacity(0.5f); while (i <= n) { // Watermark under the existing page Rectangle pageSize = pdfReader.getPageSizeWithRotation(i); over = pdfStamper.getOverContent(i); over.beginText(); over.setFontAndSize(bf, 200); over.setGState(gstate); over.setColorFill(Color.LIGHT_GRAY); over.showTextAligned(Element.ALIGN_CENTER, watermarkText, (pageSize.width() / 2), (pageSize.height() / 2), 45); over.endText(); i++; } pdfStamper.close(); } catch (DocumentException ex) { throw new IOException("iText error creating watermark on PDF", ex); } catch (IOException ex) { throw new IOException("IO error creating watermark on PDF", ex); } return outputStream.toByteArray(); }
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. * //from ww w .j a va 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()); } }
From source file:org.kuali.ole.module.purap.pdf.PurapPdf.java
License:Educational Community License
/** * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that * this is a Test document and include the environment, if the environment is not a production environment. * * @param writer The PdfWriter for this document. * @param document The document.//from w w w .j a v a 2s.c om * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onStartPage(PdfWriter writer, Document document) { if (!KRADUtils.isProductionEnvironment()) { PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.beginText(); cb.setFontAndSize(helv, 48); String watermarkText = "Test document (" + environment + ")"; cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2, document.getPageSize().height() / 2, 45); cb.endText(); cb.restoreState(); } if (GlobalVariables.getUserSession() != null && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) { GlobalVariables.getUserSession().removeObject("isPreview"); PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.beginText(); cb.setFontAndSize(helv, 48); String watermarkText = "DRAFT"; cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2, document.getPageSize().height() / 2, 45); cb.endText(); cb.restoreState(); } }