private PdfPTable cuerpo(final List<String> listaHeaderTable, final Map<String, String> mapa) throws DocumentException { PdfPTable body = new PdfPTable(12); body.setWidthPercentage(100); Font colorLetra = null; Paragraph aliniarTexto = null; PdfPCell cellCuerpoHeader = null; // pinta los header de la tabla for (String valorCelda : listaHeaderTable) { aliniarTexto = new Paragraph(); aliniarTexto.setAlignment(Element.ALIGN_CENTER); colorLetra = new Font(); colorLetra.setColor(new BaseColor(Color.white)); cellCuerpoHeader = new PdfPCell(new Phrase(valorCelda, colorLetra)); cellCuerpoHeader.setHorizontalAlignment(Element.ALIGN_CENTER); cellCuerpoHeader.setBackgroundColor(new BaseColor(0, 0, 0)); cellCuerpoHeader.setBorder(Rectangle.NO_BORDER); cellCuerpoHeader.setColspan(12); body.addCell(cellCuerpoHeader); } PdfPCell cellSaltoDeLinea = new PdfPCell(); cellSaltoDeLinea = new PdfPCell(new Phrase(" ")); cellSaltoDeLinea.setBorder(Rectangle.NO_BORDER); cellSaltoDeLinea.setColspan(12); body.addCell(cellSaltoDeLinea); for (Map.Entry<String, String> entry : mapa.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); PdfPCell celdaEnBlanco = new PdfPCell(new Phrase("")); celdaEnBlanco.setColspan(2); celdaEnBlanco.setBorder(0); aliniarTexto = new Paragraph(); aliniarTexto.setAlignment(Element.ALIGN_CENTER); colorLetra = new Font(); colorLetra.setColor(new BaseColor(Color.BLACK)); PdfPCell celdaKey = new PdfPCell(new Phrase(key, colorLetra)); celdaKey.setColspan(3); celdaKey.setBorder(0); PdfPCell celdaValor = new PdfPCell(new Phrase(value, colorLetra)); celdaValor.setColspan(5); celdaValor.setBorder(0); body.addCell(celdaEnBlanco); body.addCell(celdaKey); body.addCell(celdaValor); body.addCell(celdaEnBlanco); } return body; }
private PdfPTable footer() { PdfPTable footer = new PdfPTable(1); footer.setTotalWidth(523); Font colorLetra = new Font(); colorLetra.setColor(new BaseColor(Color.white)); colorLetra.setSize(8); PdfPCell cell = new PdfPCell( new Paragraph( messageSource.getMessage("generaPdf.pdf.DireccionPart1", null, Locale.getDefault()), colorLetra)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(new BaseColor(0, 0, 0)); footer.addCell(cell); cell = new PdfPCell( new Paragraph( messageSource.getMessage("generaPdf.pdf.DireccionPart2", null, Locale.getDefault()), colorLetra)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(new BaseColor(0, 0, 0)); footer.addCell(cell); cell = new PdfPCell( new Paragraph( messageSource.getMessage("generaPdf.pdf.DireccionPart3", null, Locale.getDefault()), colorLetra)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(new BaseColor(0, 0, 0)); footer.addCell(cell); return footer; }
private Map<String, Object> getMapSol() { final HashMap<String, Object> mapCarac = new HashMap<String, Object>(); final Paragraph paraText = new Paragraph(); paraText.setAlignment(Element.ALIGN_CENTER); final Font colorLetra = new Font(); colorLetra.setColor(new BaseColor(Color.white)); final Font colorRow = new Font(); colorRow.setColor(new BaseColor(39, 40, 41)); mapCarac.put(VUTramConstants.KEY_COLORF, colorLetra); mapCarac.put(VUTramConstants.KEY_PARG, paraText); mapCarac.put(VUTramConstants.KEY_COLORH, new BaseColor(57, 60, 62)); mapCarac.put(VUTramConstants.KEY_COLORR, new BaseColor(84, 84, 84)); mapCarac.put(VUTramConstants.KEY_COLORRF, colorRow); return mapCarac; }
@Override protected void buildPdfDocument( Map<String, Object> model, Document doc, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { // get data model which is passed by the Spring container Test test = (Test) model.get("test"); doc.add(new Paragraph(test.getName())); doc.add(new Paragraph(test.getMaterial().getName())); Set<TestAttribute> sta = test.getTestAttributs(); if (sta.size() > 0) { PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100.0f); table.setWidths(new float[] {1.0f}); table.setSpacingBefore(10); // define font for table header row Font font = FontFactory.getFont(FontFactory.HELVETICA); font.setColor(BaseColor.WHITE); // define table header cell PdfPCell cell = new PdfPCell(); cell.setBackgroundColor(BaseColor.BLUE); cell.setPadding(5); // write table header cell.setPhrase(new Phrase("Test attribute type", font)); table.addCell(cell); cell.setPhrase(new Phrase("Value", font)); table.addCell(cell); for (TestAttribute ta : sta) { table.addCell(ta.getTypeTestAttr().getName()); table.addCell(ta.getValue()); } doc.add(table); } }
private InputStream generatePdf(List<Map<String, Object>> detailList) throws IOException, DocumentException { ByteArrayOutputStream output = new ByteArrayOutputStream(); Rectangle rectPageSize = new Rectangle(PageSize.A4); Document document = new Document(rectPageSize, STR_20, STR_20, STR_20, STR_20); PdfWriter.getInstance(document, output); document.open(); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 设置中文字体 com.itextpdf.text.Font headFont = new com.itextpdf.text.Font(bfChinese, STR_10, com.itextpdf.text.Font.BOLD); com.itextpdf.text.Font commonFont = new com.itextpdf.text.Font(bfChinese, STR_10, com.itextpdf.text.Font.NORMAL); // 设置字体大小 com.itextpdf.text.Font amtFont = new com.itextpdf.text.Font(bfChinese, STR_10, com.itextpdf.text.Font.BOLD); // 设置字体大小 amtFont.setColor(STR_39, STR_157, STR_94); float[] widths = { STR_0_1F, STR_0_15F, STR_0_15F, STR_0_15F, STR_0_15F, STR_0_15F, STR_0_15F, STR_0_15F }; PdfPTable table = new PdfPTable(widths); table.getDefaultCell().setBorder(1); PdfPCell cell; headStyle(headFont, table); if (detailList.size() == 0) { cell = new PdfPCell(new Paragraph(NO_RECORD, commonFont)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setColspan(STR_9); table.addCell(cell); } else { fillData(detailList, commonFont, amtFont, table); } document.add(table); document.close(); InputStream inputStream = new ByteArrayInputStream(output.toByteArray()); output.close(); return inputStream; }
/** * Constructor for the MovieCalendar class; initializes the base font object. * * @throws IOException * @throws DocumentException */ public MovieTextInfo() throws DocumentException, IOException { bf = BaseFont.createFont(); Font f = new Font(bf, HEIGHT_LOCATION / 2); f.setColor(BaseColor.WHITE); press = new Phrase("P", f); }
@Override protected void buildPdfDocument( Map<String, Object> model, Document doc, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { @SuppressWarnings("unchecked") List<Teacher> teacherList = (List<Teacher>) model.get("teacherList"); // prepare font Font helvetica = PdfTemplateUtils.getDefaultSansFont(); Font helveticaHeader = PdfTemplateUtils.getDefaultSansFont(); helveticaHeader.setColor(BaseColor.WHITE); PdfTemplateUtils.drawTemplateBars(doc, writer); PdfPTable table = new PdfPTable(7); table.getDefaultCell().setBorder(0); table.setWidthPercentage(100.0f); table.setWidths(new float[] {1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f}); table.setSpacingBefore(10); // define table header cell PdfPCell cell = new PdfPCell(); cell.setBackgroundColor(BaseColor.LIGHT_GRAY); cell.setBorder(Rectangle.NO_BORDER); cell.setPadding(5); // write table header cell.setPhrase(new Phrase("Name", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("Email", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("Telefon", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("Eintritt", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("SVNR", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("Gehalt", helveticaHeader)); table.addCell(cell); cell.setPhrase(new Phrase("Stile", helveticaHeader)); table.addCell(cell); DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy"); NumberFormat formatter = NumberFormat.getCurrencyInstance(); cell = new PdfPCell(); cell.setBorder(Rectangle.NO_BORDER); cell.setPadding(5); for (Teacher teacher : teacherList) { cell.setPhrase(new Phrase(teacher.getFirstname() + " " + teacher.getLastname(), helvetica)); table.addCell(cell); cell.setPhrase(new Phrase(teacher.getEmail(), helvetica)); table.addCell(cell); cell.setPhrase(new Phrase(teacher.getTelephone(), helvetica)); table.addCell(cell); cell.setPhrase(new Phrase(fmt.print(teacher.getEngagementDate()), helvetica)); table.addCell(cell); cell.setPhrase(new Phrase(teacher.getSvnr(), helvetica)); table.addCell(cell); cell.setPhrase(new Phrase(formatter.format(teacher.getSalary()), helvetica)); table.addCell(cell); String courseList = ""; boolean first = true; for (Course c : teacher.getCourses()) { if (!first) { courseList += ", "; } courseList += c.getName(); first = false; } cell.setPhrase(new Phrase(courseList, helvetica)); table.addCell(cell); } doc.add(table); }