List of usage examples for com.itextpdf.text.pdf PdfContentByte showTextAligned
public void showTextAligned(final int alignment, final String text, final float x, final float y, final float rotation)
From source file:fattura.Fattura.java
public void inserisciDatiPubb(String cliente, String data, PdfStamper s) throws SQLException, IOException, DocumentException { ArrayPubblicita arraypubb = new ArrayPubblicita(); arraypubb.ArrayIDpubb(cliente, data); float importo; PdfContentByte content = s.getUnderContent(1);//1 for the first page BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); content.setFontAndSize(bf, 7);/*from w w w . ja va 2 s.c o m*/ for (int j = 0; j < arraypubb.IDpubb.length; j++) { arraypubb.accessoPubb(j); importo = arraypubb.importoPubblicita(j); String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo, solo un appoggio importi.add(importo); //salva l'importo maglia nella prima posizione array i cui elementi poi andranno sommati per calcolare il totale for (int z = 0; z < arraypubb.riga.length; z++) { content.showTextAligned(PdfContentByte.ALIGN_LEFT, arraypubb.riga[z] + " ", 95 + x, 538 - y, 0); x += 97; //sposta la coordinata y ad ogni stampa } content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Mat. Pubb.", 95 + x, 538 - y, 0); x += 101; content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0); x = 0; y += 20; } }
From source file:gravabncertificado.GerandoArquivoCarimbado_1.java
public static void GerandoArquivoCarimbadoPDF(String caminhoarquivo, String BN) throws InvalidPdfException { //Copiando arquivo informado. try {//w w w. ja v a2 s. c o m // Adicionado parametro para nao retornar erro quando o documento for protegido. PdfReader.unethicalreading = true; //Cria o reader para o primeiro PDF PdfReader reader = new PdfReader(caminhoarquivo); // n recebe o numero total de paginas int n = reader.getNumberOfPages(); //Tamanho da primeira Pagina ; //Cria Segundo PDF Document document = new Document(PageSize.A4, 36, 36, 36, 36); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf")); document.open(); // Adiciona conteudo ao PDF Carimbado. PdfContentByte cb = writer.getDirectContent(); int i = 0; int p = 0; String caminhodestino = (caminhoarquivo.substring(0, caminhoarquivo.length() - BN.length())); // DESABILITADO PORQUE O MOVER NAO ESTAVA FUNCIONANDO. 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); while (i < n) { document.newPage(); p++; i++; PdfContentByte under = writer.getDirectContentUnder(); PdfImportedPage page1 = writer.getImportedPage(reader, i); cb.addTemplate(page1, 0, i * 0.2f); // Page 1: a rectangle /* RETANGULO DESATIVADO drawRectangle(under, 100, 50); under.setRGBColorFill(255, 220, 220); under.rectangle(width /50, 5, 118, 40); under.fill(); */ //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(); origem.delete(); /* PdfContentByte under = writer.getDirectContentUnder(); // Page 1: a rectangle drawRectangle(under, 20, 20); under.setRGBColorFill(0xFF, 0xD7, 0x00); under.rectangle(5, 5, 15, 15); under.fill(); */ //document.newPage(); //COPIANDO ARQUIVO CARIMBADO /* DESABILITADO PORQUE NAO ESTAVA FUNCIONANDO. boolean bool; //caminhoarquivo = caminhoarquivo.replace("\\", "\\\\"); //caminhodestino = caminhodestino.replace("\\", "\\\\"); File origem = new File(caminhoarquivo); File destino = new File(caminhodestino); bool = origem.renameTo(destino); System.out.println(origem); System.out.println(caminhodestino); */ } catch (IOException | DocumentException ex) { } }
From source file:gravabncertificado007.CarimboCertificado.java
public void aplicaCarimboBin(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException { PdfReader.unethicalreading = true;//from w w w .ja va 2 s . com //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 ww w. j av a 2 s. co 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
/** * /*w w w .jav a2s .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 {/* w w w . j ava 2 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:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java
License:Apache License
/** * Writes text watermark to one of the 5 preconfigured locations * //from www . j a va 2 s . c o m * @param pcb * @param r * @param tokens * @param size * @param position */ private void writeAlignedText(PdfContentByte pcb, Rectangle r, Vector<String> tokens, float size, String position) { // get the dimensions of our 'rectangle' for text float height = size * tokens.size(); float width = 0; float centerX = 0, startY = 0; for (int i = 0; i < tokens.size(); i++) { if (pcb.getEffectiveStringWidth(tokens.get(i), false) > width) { width = pcb.getEffectiveStringWidth(tokens.get(i), false); } } // now that we have the width and height, we can calculate the center // position for // the rectangle that will contain our text. if (position.equals(POSITION_BOTTOMLEFT)) { centerX = width / 2 + PAD; startY = 0 + PAD + height; } else if (position.equals(POSITION_BOTTOMRIGHT)) { centerX = r.getWidth() - (width / 2) - PAD; startY = 0 + PAD + height; } else if (position.equals(POSITION_TOPLEFT)) { centerX = width / 2 + PAD; startY = r.getHeight() - (PAD * 2); } else if (position.equals(POSITION_TOPRIGHT)) { centerX = r.getWidth() - (width / 2) - PAD; startY = r.getHeight() - (PAD * 2); } else if (position.equals(POSITION_CENTER)) { centerX = r.getWidth() / 2; startY = (r.getHeight() / 2) + (height / 2); } // apply text to PDF pcb.beginText(); for (int t = 0; t < tokens.size(); t++) { pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, tokens.get(t), centerX, startY - (size * t), 0); } pcb.endText(); }
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 . java2 s. com*/ 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 www.j a v a 2 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:printInv.GenerateInvoice.java
private void createPDF(String pdfFilename) { Document doc = new Document(); PdfWriter docWriter = null;//w ww . j ava 2 s .c o m initializeFonts(); try { // String path = "docs/" + pdfFilename; String path = pdfFilename; docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path)); doc.addAuthor("SmartWMS"); doc.addCreationDate(); doc.addProducer(); doc.addCreator("SmartWMS"); doc.addTitle("Invoice"); doc.setPageSize(PageSize.LETTER); doc.open(); PdfContentByte cb = docWriter.getDirectContent(); boolean beginPage = true; int y = 0; System.out.println("n ===========" + n); for (int i = 0; i < n; i++) { if (beginPage) { beginPage = false; generateLayout(doc, cb); generateHeader(doc, cb); y = 615; } generateDetail(doc, cb, i, y); y = y - 15; if (y < 50) { printPageNumber(cb); doc.newPage(); beginPage = true; } } printPageNumber(cb); cb.beginText(); cb.setFontAndSize(bfBold, 10); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Grand Total : " + total, 570, 35, 0); cb.endText(); } catch (DocumentException dex) { dex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (doc != null) { doc.close(); } if (docWriter != null) { docWriter.close(); } } }