List of usage examples for com.itextpdf.text.pdf PdfContentByte setFontAndSize
public void setFontAndSize(final BaseFont bf, final float size)
From source file:gravabncertificado007.CarimboCertificado.java
public void aplicaCarimboBin(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException { PdfReader.unethicalreading = true;// www .ja v a 2 s .c o m //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); Rectangle psize = reader.getPageSize(1); float width = psize.getWidth(); float height = psize.getHeight(); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf")); document.open(); int i = 0; BN = BN.substring(BN.length() - 13, BN.length() - 4); PdfContentByte cb = writer.getDirectContent(); while (i < reader.getNumberOfPages()) { i++; document.newPage(); PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); //CARIMBO DA BN BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0); cb.endText(); } document.close(); writer.close(); reader.close(); }
From source file:gravabncertificado007.CarimboCertificado.java
public void aplicaCariboGedi(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException { PdfReader.unethicalreading = true;/*from w ww . j a v a2s . c o m*/ //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); Rectangle psize = reader.getPageSize(1); float width = psize.getWidth(); float height = psize.getHeight(); Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-G.pdf")); document.open(); int i = 0; BN = BN.substring(BN.length() - 13, BN.length() - 4); PdfContentByte cb = writer.getDirectContent(); while (i < reader.getNumberOfPages()) { i++; document.newPage(); PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " _________________ ", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |Copia Controlada |", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |_________________|", width / 6, 14, 0); cb.endText(); } document.close(); writer.close(); reader.close(); }
From source file:jasperSoft.MergePDF.java
/** * /*from w w w . java 2 s .c o m*/ * @param streamOfPDFFiles * @param outputStream * @param paginate */ public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try { List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) { document.close(); } try { if (outputStream != null) { outputStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
From source file:jati.GerandoArquivoCarimbado.java
public static void GerandoArquivoCarimbadoPDF(String caminhoarquivo, String BN) throws InvalidPdfException, IOException { //Copiando arquivo informado. try {/*from www.j a va2 s . c o m*/ // Adicionado parametro para nao retornar erro quando o documento for protegido. PdfReader.unethicalreading = true; PdfWriter writer = PdfWriter.getInstance(montaraAquivo(caminhoarquivo), new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf")); // ABRE O DOCUMENTO CRIADO PARA MANUSEIO montaraAquivo(caminhoarquivo).open(); //SUBSTRING RETIRA PARTE DO TEXTO ENTRE OS INDICES ESPECIFICADOS //caminhoDestino RECEBE UM NOVO CAMINHO RESULTANTE DOS INDICES DE CAMINHO.LENGTH - BN.LENGTH // QUE FORMAM UMA NOVA STRING String caminhodestino = (caminhoarquivo.substring(0, caminhoarquivo.length() - BN.length())); File destinooriginal = new File(caminhodestino + "ORIGINAL\\"); if (!destinooriginal.exists()) { destinooriginal.mkdir(); } caminhodestino = (caminhodestino + "ORIGINAL\\" + BN); //TESTANDO NOVA FORMA DE COPIAR File origem = new File(caminhoarquivo); File destino = new File(caminhodestino); FileInputStream fis = new FileInputStream(origem); FileOutputStream fos = new FileOutputStream(destino); FileChannel inChannel = fis.getChannel(); FileChannel outChannel = fos.getChannel(); long transferFrom = outChannel.transferFrom(inChannel, 0, inChannel.size()); fis.close(); fos.close(); inChannel.close(); outChannel.close(); // Thread.sleep(10); BN = BN.substring(0, BN.length() - 4); PdfContentByte cb = writer.getDirectContent(); int i = 0; while (i < reader.getNumberOfPages()) { montaraAquivo(caminhodestino).newPage(); i++; //PDFCONTETBYTE GERA UMA ESPECIE DE CODIGO DE BARRAS PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); //CARIMBO DA BN BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 14); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0); cb.endText(); } montaraAquivo(caminhodestino).close(); writer.close(); reader.close(); origem.delete(); } catch (IOException | DocumentException ex) { } }
From source file:me.Aron.Heinecke.fbot.lib.Converter.java
License:Apache License
/*** * Add a note to the bottom of a pdf file in italic font * @param rfile file to be read from/*from ww w. j a va 2s.com*/ * @param wfile file to be written to * @param text text to add * @return path to the resulting pdf, null if it failed */ private String addPDFNote(File rfile, File wfile, String text) { try { PdfReader pdfReader = new PdfReader(rfile.getAbsolutePath()); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(wfile)); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte cb = pdfStamper.getUnderContent(i); BaseFont bf = BaseFont.createFont(); bf.setPostscriptFontName("ITALIC"); cb.beginText(); cb.setFontAndSize(bf, 12); cb.setTextMatrix(10, 20); cb.showText(text); cb.endText(); } pdfStamper.close(); return wfile.getAbsolutePath(); } catch (IOException | DocumentException e) { fbot.getLogger().exception("converter", e); return null; } }
From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java
License:Apache License
/** * Applies a text watermark (current date, user name, etc, depending on * options)// w w w .j a v a2 s.c o m * * @param reader * @param writer * @param options */ private void textAction(Action ruleAction, NodeRef actionedUponNodeRef, ContentReader actionedUponContentReader, Map<String, Object> options) { PdfStamper stamp = null; File tempDir = null; ContentWriter writer = null; String watermarkText; StringTokenizer st; Vector<String> tokens = new Vector<String>(); try { // get a temp file to stash the watermarked PDF in before moving to // repo File alfTempDir = TempFileProvider.getTempDir(); tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId()); tempDir.mkdir(); File file = new File(tempDir, serviceRegistry.getFileFolderService().getFileInfo(actionedUponNodeRef).getName()); // get the PDF input stream and create a reader for iText PdfReader reader = new PdfReader(actionedUponContentReader.getContentInputStream()); stamp = new PdfStamper(reader, new FileOutputStream(file)); PdfContentByte pcb; // get the PDF pages and position String pages = (String) options.get(PARAM_PAGE); String position = (String) options.get(PARAM_POSITION); String depth = (String) options.get(PARAM_WATERMARK_DEPTH); // create the base font for the text stamp BaseFont bf = BaseFont.createFont((String) options.get(PARAM_WATERMARK_FONT), BaseFont.CP1250, BaseFont.EMBEDDED); // get watermark text and process template with model String templateText = (String) options.get(PARAM_WATERMARK_TEXT); Map<String, Object> model = buildWatermarkTemplateModel(actionedUponNodeRef); StringWriter watermarkWriter = new StringWriter(); freemarkerProcessor.processString(templateText, model, watermarkWriter); watermarkText = watermarkWriter.getBuffer().toString(); // tokenize watermark text to support multiple lines and copy tokens // to vector for re-use st = new StringTokenizer(watermarkText, "\r\n", false); while (st.hasMoreTokens()) { tokens.add(st.nextToken()); } // stamp each page int numpages = reader.getNumberOfPages(); for (int i = 1; i <= numpages; i++) { Rectangle r = reader.getPageSizeWithRotation(i); // if this is an under-text stamp, use getUnderContent. // if this is an over-text stamp, use getOverContent. if (depth.equals(DEPTH_OVER)) { pcb = stamp.getOverContent(i); } else { pcb = stamp.getUnderContent(i); } // set the font and size float size = Float.parseFloat((String) options.get(PARAM_WATERMARK_SIZE)); pcb.setFontAndSize(bf, size); // only apply stamp to requested pages if (checkPage(pages, i, numpages)) { writeAlignedText(pcb, r, tokens, size, position); } } stamp.close(); // Get a writer and prep it for putting it back into the repo //can't use BasePDFActionExecuter.getWriter here need the nodeRef of the destination NodeRef destinationNode = createDestinationNode(file.getName(), (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER), actionedUponNodeRef); writer = serviceRegistry.getContentService().getWriter(destinationNode, ContentModel.PROP_CONTENT, true); writer.setEncoding(actionedUponContentReader.getEncoding()); writer.setMimetype(FILE_MIMETYPE); // Put it in the repo writer.putContent(file); // delete the temp file file.delete(); } catch (IOException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } catch (DocumentException e) { throw new AlfrescoRuntimeException(e.getMessage(), e); } finally { if (tempDir != null) { try { tempDir.delete(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } if (stamp != null) { try { stamp.close(); } catch (Exception ex) { throw new AlfrescoRuntimeException(ex.getMessage(), ex); } } } }
From source file:org.durel.mydooble.PDF.java
License:Open Source License
public PDF(int nbItems) throws IOException, DocumentException { buffer = new ByteArrayOutputStream(); doc = new Document(PageSize.A4); writer = PdfWriter.getInstance(doc, buffer); writer.setCompressionLevel(0);/* w ww. ja va 2 s . com*/ this.nbItems = nbItems; doc.open(); PdfContentByte cb = writer.getDirectContent(); cb.setFontAndSize(font, FONT_SIZE); }
From source file:org.gephi.preview.plugin.renderers.EdgeLabelRenderer.java
License:Open Source License
public void renderPDF(PDFTarget target, String label, float x, float y, Color color, float outlineSize, Color outlineColor) {//from w w w. j av a 2s. c om PdfContentByte cb = target.getContentByte(); cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue()); BaseFont bf = target.getBaseFont(font); float textHeight = getTextHeight(bf, font.getSize(), label); if (outlineSize > 0) { cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue()); cb.setLineWidth(outlineSize); cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND); cb.setLineCap(PdfContentByte.LINE_CAP_ROUND); if (outlineColor.getAlpha() < 255) { cb.saveState(); float alpha = outlineColor.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.beginText(); cb.setFontAndSize(bf, font.getSize()); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f); cb.endText(); if (outlineColor.getAlpha() < 255) { cb.restoreState(); } } cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); cb.beginText(); cb.setFontAndSize(bf, font.getSize()); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f); cb.endText(); }
From source file:org.gephi.preview.plugin.renderers.NodeLabelRenderer.java
License:Open Source License
public void renderPDF(PDFTarget target, Node node, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) { Font font = fontCache.get(fontSize); PdfContentByte cb = target.getContentByte(); BaseFont bf = target.getBaseFont(font); //Box//from ww w .j a va2 s . c o m if (showBox) { cb.setRGBColorFill(boxColor.getRed(), boxColor.getGreen(), boxColor.getBlue()); if (boxColor.getAlpha() < 255) { cb.saveState(); float alpha = boxColor.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setFillOpacity(alpha); cb.setGState(gState); } float textWidth = getTextWidth(bf, fontSize, label); float textHeight = getTextHeight(bf, fontSize, label); //A height of just textHeight seems to be half the text height sometimes //BaseFont getAscentPoint and getDescentPoint may be not very precise cb.rectangle(x - textWidth / 2f - outlineSize / 2f, -y - outlineSize / 2f - textHeight, textWidth + outlineSize, textHeight * 2f + outlineSize); cb.fill(); if (boxColor.getAlpha() < 255) { cb.restoreState(); } } cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue()); float textHeight = getTextHeight(bf, fontSize, label); if (outlineSize > 0) { cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue()); cb.setLineWidth(outlineSize); cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND); cb.setLineCap(PdfContentByte.LINE_CAP_ROUND); if (outlineColor.getAlpha() < 255) { cb.saveState(); float alpha = outlineColor.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.beginText(); cb.setFontAndSize(bf, font.getSize()); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f); cb.endText(); if (outlineColor.getAlpha() < 255) { cb.restoreState(); } } cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); cb.beginText(); cb.setFontAndSize(bf, font.getSize()); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f); cb.endText(); }
From source file:org.javad.pdf.PageTitle.java
License:Apache License
@Override public OutputBounds generate(PdfContentByte content) { int maxWidth = 0; content.setColorStroke(BaseColor.BLACK); Font f = FontRegistry.getInstance().getFont(PdfFontDefinition.Title); content.setFontAndSize(f.getBaseFont(), f.getSize()); float top = getY(); content.setHorizontalScaling(110.0f); if (getTitle() != null && !getTitle().isEmpty()) { maxWidth = (int) f.getBaseFont().getWidthPoint(getTitle().toUpperCase(), f.getSize()); PdfUtil.renderConstrainedText(content, getTitle().toUpperCase(), f, getX(), top, (int) (maxWidth * 1.1)); }//w w w .j ava 2 s . co m if (getSubTitle() != null && !getSubTitle().isEmpty()) { Font subFont = FontRegistry.getInstance().getFont(PdfFontDefinition.Subtitle); top -= subFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f); content.setFontAndSize(subFont.getBaseFont(), subFont.getSize()); maxWidth = Math.max(maxWidth, (int) subFont.getBaseFont().getWidthPoint(getSubTitle().toUpperCase(), subFont.getSize())); PdfUtil.renderConstrainedText(content, getSubTitle().toUpperCase(), subFont, getX(), top, (int) (maxWidth * 1.10)); } content.setHorizontalScaling(100.0f); if (getClassifier() != null && !getClassifier().isEmpty()) { Font classFont = FontRegistry.getInstance().getFont(PdfFontDefinition.Classifier); content.setFontAndSize(classFont.getBaseFont(), classFont.getSize()); top -= classFont.getCalculatedSize() + PdfUtil.convertFromMillimeters(3.0f); float width = classFont.getBaseFont().getWidthPoint(getClassifier(), classFont.getCalculatedSize()) + 4; maxWidth = Math.max(maxWidth, (int) width + (2 * DASH_LENGTH)); content.setLineWidth(0.8f); float lineTop = top + ((int) classFont.getSize() / 2 - 1); content.moveTo(getX() - (width / 2) - DASH_LENGTH, lineTop); content.lineTo(getX() - (width / 2), lineTop); content.moveTo(getX() + (width / 2), lineTop); content.lineTo(getX() + (width / 2) + DASH_LENGTH, lineTop); content.stroke(); PdfUtil.renderConstrainedText(content, getClassifier(), classFont, getX(), top, (int) (maxWidth * 1.10)); } OutputBounds rect = new OutputBounds(getX() - maxWidth / 2, getY(), maxWidth, getY() - top); return rect; }