List of usage examples for com.itextpdf.text Chunk Chunk
private Chunk(final Float tabInterval, final boolean isWhitespace)
From source file:Controller.CrearPDF.java
/** * We create a PDF document with iText using different elements to learn * to use this library.//from w ww . j ava 2 s . c om * Creamos un documento PDF con iText usando diferentes elementos para aprender * a usar esta librera. * @param pdfNewFile <code>String</code> * pdf File we are going to write. * Fichero pdf en el que vamos a escribir. */ // public void createPDF(File pdfNewFile, ReparacionEntity reparacion) throws Exception { public void createPDF(File pdfNewFile) throws Exception { // We create the document and set the file name. // Creamos el documento e indicamos el nombre del fichero. try { // ClienteController cc = new ClienteController(); // Cliente cliente = cc.buscarPorId(reparacion.getCliente()); Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(pdfNewFile)); } catch (FileNotFoundException fileNotFoundException) { System.out.println("No such file was found to generate the PDF " + "(No se encontr el fichero para generar el pdf)" + fileNotFoundException); } document.open(); // We add metadata to PDF // Aadimos los metadatos del PDF document.addTitle("Table export to PDF (Exportamos la tabla a PDF)"); document.addSubject("Using iText (usando iText)"); document.addKeywords("Java, PDF, iText"); document.addAuthor("Cdigo Xules"); document.addCreator("Cdigo Xules"); // First page // Primera pgina Chunk chunk = new Chunk("RDEN DE TRABAJO", categoryFont); Chunk c2 = new Chunk("\n\n\nCentro de Formacin SATCAR6\n" + "Calle Artes Grficas n1 Nave 12 A\n" + "Pinto (28320), Madrid", smallFont); Chunk c3 = new Chunk("Datos del Cliente", smallBold); // Chunk c4 = new Chunk("Nombre: "+cliente.getRazonSocial(),smallBold); // Chunk c5 = new Chunk("Poblacin: "+cliente.getPoblacion(),smallBold); // Chunk c6 = new Chunk("Provincia: "+cliente.getProvincia(),smallBold); // Chunk c7 = new Chunk("Cdigo Postal: "+cliente.getCp(),smallBold); // Chunk c8 = new Chunk("Num Telfono: Pepito"+cliente.getTlf1(),smallBold); Chunk c4 = new Chunk("Nombre: Jesus Eduardo Garcia Toril", smallBold); Chunk c5 = new Chunk("Poblacin: Pinto", smallBold); Chunk c6 = new Chunk("Provincia: Madrid", smallBold); Chunk c7 = new Chunk("Cdigo Postal: 28320", smallBold); Chunk c8 = new Chunk("Num Telfono: 659408182", smallBold); Paragraph parrafo = new Paragraph(chunk); Paragraph p2 = new Paragraph(c2); Paragraph p3 = new Paragraph(c3); Phrase ph1 = new Phrase(c4); Phrase ph2 = new Phrase(c5); Phrase ph3 = new Phrase(c6); Phrase ph4 = new Phrase(c7); Phrase ph5 = new Phrase(c8); // Let's create de first Chapter (Creemos el primer captulo) // We add an image (Aadimos una imagen) Image image; try { parrafo.setAlignment(Element.ALIGN_CENTER); image = Image.getInstance(iTextExampleImage); image.setAbsolutePosition(0, 750); p2.setAlignment(Element.ALIGN_LEFT); document.add(parrafo); document.add(image); document.add(p2); document.add(p3); document.add(ph1); document.add(ph2); document.add(ph3); document.add(ph4); document.add(ph5); } catch (BadElementException ex) { System.out.println("Image BadElementException" + ex); } // Second page - some elements // Segunda pgina - Algunos elementos // List by iText (listas por iText) String text = "test 1 2 3 "; for (int i = 0; i < 5; i++) { text = text + text; } List list = new List(List.UNORDERED); ListItem item = new ListItem(text); item.setAlignment(Element.ALIGN_JUSTIFIED); list.add(item); text = "a b c align "; for (int i = 0; i < 5; i++) { text = text + text; } item = new ListItem(text); item.setAlignment(Element.ALIGN_JUSTIFIED); list.add(item); text = "supercalifragilisticexpialidocious "; for (int i = 0; i < 3; i++) { text = text + text; } item = new ListItem(text); item.setAlignment(Element.ALIGN_JUSTIFIED); list.add(item); // How to use PdfPTable // Utilizacin de PdfPTable // We use various elements to add title and subtitle // Usamos varios elementos para aadir ttulo y subttulo Anchor anchor = new Anchor("Table export to PDF (Exportamos la tabla a PDF)", categoryFont); anchor.setName("Table export to PDF (Exportamos la tabla a PDF)"); Chapter chapTitle = new Chapter(new Paragraph(anchor), 1); Paragraph paragraph = new Paragraph("Do it by Xules (Realizado por Xules)", subcategoryFont); Section paragraphMore = chapTitle.addSection(paragraph); paragraphMore.add(new Paragraph("This is a simple example (Este es un ejemplo sencillo)")); Integer numColumns = 6; Integer numRows = 120; // We create the table (Creamos la tabla). PdfPTable table = new PdfPTable(numColumns); // Now we fill the PDF table // Ahora llenamos la tabla del PDF PdfPCell columnHeader; // Fill table rows (rellenamos las filas de la tabla). for (int column = 0; column < numColumns; column++) { columnHeader = new PdfPCell(new Phrase("COL " + column)); columnHeader.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(columnHeader); } table.setHeaderRows(1); // Fill table rows (rellenamos las filas de la tabla). for (int row = 0; row < numRows; row++) { for (int column = 0; column < numColumns; column++) { table.addCell("Row " + row + " - Col" + column); } } // We add the table (Aadimos la tabla) paragraphMore.add(table); // We add the paragraph with the table (Aadimos el elemento con la tabla). document.add(chapTitle); document.close(); System.out.println("Your PDF file has been generated!(Se ha generado tu hoja PDF!"); } catch (DocumentException documentException) { System.out.println( "The file not exists (Se ha producido un error al generar un documento): " + documentException); } }
From source file:controller.PDFGenerator.java
@Override public void GenerateDocument(Resolution doc) { String resId = "RES-IC-" + format(doc.getId()) + "-" + Calendar.getInstance().get(Calendar.YEAR); Document pdf = createDocument(resId + ".pdf"); if (pdf == null) return;// w w w. ja va 2 s.c o m try { pdf.open(); Font boldFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD); Font parFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); Chunk chunk = new Chunk(doc.getTitle() + "\n\n", boldFont); Paragraph par = new Paragraph(chunk); par.setAlignment(Paragraph.ALIGN_CENTER); pdf.add(par); chunk = new Chunk(resId + "\n\n", boldFont); par = new Paragraph(chunk); par.setAlignment(Paragraph.ALIGN_CENTER); pdf.add(par); chunk = new Chunk("Atencin: ", boldFont); par = new Paragraph(chunk); chunk = new Chunk(doc.getAttention() + "\n\n", parFont); par.add(chunk); par.setAlignment(Paragraph.ALIGN_LEFT); par.setIndentationLeft((float) 3.0); pdf.add(par); chunk = new Chunk(doc.getIntro() + "\n\n", parFont); par = new Paragraph(chunk); par.setAlignment(Paragraph.ALIGN_JUSTIFIED); pdf.add(par); chunk = new Chunk((doc.isOneresult() == true ? "RESULTANDO NICO:\n" : "RESULTANDO:\n"), boldFont); par = new Paragraph(chunk); chunk = new Chunk(doc.getResult() + "\n\n", parFont); par.add(chunk); par.setAlignment(Paragraph.ALIGN_JUSTIFIED); pdf.add(par); chunk = new Chunk((doc.isOneconsideration() == true ? "CONSIDERANDO NICO:\n" : "CONSIDERANDOS:\n"), boldFont); par = new Paragraph(chunk); chunk = new Chunk(doc.getConsider() + "\n\n", parFont); par.add(chunk); par.setAlignment(Paragraph.ALIGN_JUSTIFIED); pdf.add(par); chunk = new Chunk("RESUELVO:\n", boldFont); par = new Paragraph(chunk); chunk = new Chunk(doc.getResolve() + "\n\n", parFont); par.add(chunk); par.setAlignment(Paragraph.ALIGN_JUSTIFIED); pdf.add(par); chunk = new Chunk("NOTIFIQUESE:\n", boldFont); par = new Paragraph(chunk); par.setAlignment(Paragraph.ALIGN_LEFT); pdf.add(par); chunk = new Chunk(doc.getNotify(), parFont); par = new Paragraph(chunk); par.setAlignment(Paragraph.ALIGN_LEFT); par.setIndentationLeft(250); pdf.add(par); pdf.close(); } catch (Exception ex) { System.out.println("Error writirn pdf."); } }
From source file:de.beimax.talenttree.AbstractPageGenerator.java
License:Open Source License
/** * Parse a string to PDF/itext phrase to be rendered later * @param key language key/*from w ww .ja va2 s.c om*/ * @param fontSize size of font * @param narrowFonts use narrow fonts? * @return * @throws Exception */ protected Phrase parseTextProperty(String key, float fontSize, boolean narrowFonts) throws Exception { // define fonts Font fontRegular, fontBold; if (narrowFonts) { fontRegular = new Font(generator.getFontCondensedRegular(), fontSize); fontBold = new Font(generator.getFontCondensedBold(), fontSize); } else { fontRegular = new Font(generator.getFontRegular(), fontSize); fontBold = new Font(generator.getFontBold(), fontSize); } Font fontSymbol = new Font(generator.getFontSymbol(), fontSize); Phrase phrase = new Phrase(); phrase.setLeading(fontSize * 1.2f); // get localized element String localized = getLocalizedString(key); for (String part : localized.split("\\|")) { if (part.length() == 0) continue; // make sure to not fire index out of range char first = part.charAt(0); char last = part.charAt(part.length() - 1); switch (first) { case '*': // bold part = part.substring(1); // remove first char if (last == ' ') part = part.substring(0, part.length() - 1); phrase.add(new Chunk(part, fontBold)); if (last == ' ') phrase.add(new Chunk(" ", fontRegular)); break; case '#': // symbol font part = part.substring(1); // remove first char if (last == ' ') part = part.substring(0, part.length() - 1); phrase.add(new Chunk(part, fontSymbol)); if (last == ' ') phrase.add(new Chunk(" ", fontRegular)); break; default: // all other cases phrase.add(new Chunk(part, fontRegular)); } } return phrase; }
From source file:de.beimax.talenttree.PageGeneratorCareer.java
License:Open Source License
/** * Add skill data//from w ww .ja v a 2 s .c om * @param prefix translated prefix (printed bold) without : * @param dataKey data key in YAML file, skills/bonus_skills * @throws Exception */ protected void addSkillData(String prefix, String dataKey) throws Exception { Font fontRegular = new Font(generator.getFontRegular(), 10.5f); Font fontBold = new Font(generator.getFontBold(), 10.5f); // add career skills Phrase phrase = new Phrase(); phrase.add(new Chunk(prefix + ": ", fontBold)); ArrayList<String> skills = new ArrayList<String>(); //noinspection unchecked for (String key : (Iterable<String>) data.get(dataKey)) skills.add(getLocalizedString(key)); // sort localized Collections.sort(skills, Collator.getInstance()); // build skill list StringBuilder sb = new StringBuilder(); for (String skill : skills) { if (sb.length() != 0) sb.append(", "); sb.append(skill); } phrase.add(new Chunk(sb.toString(), fontRegular)); Paragraph p = new Paragraph(phrase); p.setAlignment(Element.ALIGN_JUSTIFIED); p.setIndentationRight(getUsablePageWidth() - PDFGenerator.headerTextMaxWidth); p.setLeading(13.2f); document.add(p); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkChapter(String text) { return new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeChapter, Font.BOLD)); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkElement(String text) { return new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeElement, Font.BOLD)); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkText(String text) { return new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeText, Font.NORMAL)); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkTextBold(String text) { return new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeText, Font.BOLD)); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkTextBoldWithReference(String text, String reference) { Chunk chunkText = new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeText, Font.BOLD)); // Referenz setzen chunkText.setLocalGoto(reference);/*from w w w. ja v a 2 s .c om*/ // Unterstreichen chunkText.setUnderline(new BaseColor(0x00, 0x0f, 0xFF), 0.5f, 0.0f, -4f, 0.0f, PdfContentByte.LINE_CAP_BUTT); return chunkText; }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
private Chunk getChunkTextItalic(String text) { return new Chunk(text, FontFactory.getFont(fontNameStandard, fontSizeText, Font.ITALIC)); }