List of usage examples for com.lowagie.text.pdf PdfPTable getDefaultCell
public PdfPCell getDefaultCell()
PdfPCell
that will be used as reference for all the addCell
methods except addCell(PdfPCell)
. From source file:org.sonar.report.pdf.Style.java
License:Open Source License
public static void noBorderTable(PdfPTable table) { table.getDefaultCell().setBorderColor(Color.WHITE); }
From source file:org.sonar.report.pdf.Style.java
License:Open Source License
/** * This method makes a simple table with content. * // w w w .ja va 2s . c o m * @param left Data for left column * @param right Data for right column * @param title The table title * @param noData Showed when left or right are empty * @return The table (iText table) ready to add to the document */ public static PdfPTable createSimpleTable(List<String> left, List<String> right, String title, String noData) { PdfPTable table = new PdfPTable(2); table.getDefaultCell().setColspan(2); table.addCell(new Phrase(title, Style.DASHBOARD_TITLE_FONT)); table.getDefaultCell().setBackgroundColor(Color.GRAY); table.addCell(""); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(Color.WHITE); Iterator<String> itLeft = left.iterator(); Iterator<String> itRight = right.iterator(); while (itLeft.hasNext()) { String textLeft = itLeft.next(); String textRight = itRight.next(); table.addCell(textLeft); table.addCell(textRight); } if (left.isEmpty()) { table.getDefaultCell().setColspan(2); table.addCell(noData); } table.setSpacingBefore(20); table.setSpacingAfter(20); return table; }
From source file:org.sonar.report.pdf.Style.java
License:Open Source License
public static PdfPTable createTwoColumnsTitledTable(List<String> titles, List<String> content) { PdfPTable table = new PdfPTable(10); Iterator<String> itLeft = titles.iterator(); Iterator<String> itRight = content.iterator(); while (itLeft.hasNext()) { String textLeft = itLeft.next(); String textRight = itRight.next(); table.getDefaultCell().setColspan(1); table.addCell(textLeft);/*from w w w . ja va 2 s.c o m*/ table.getDefaultCell().setColspan(9); table.addCell(textRight); } table.setSpacingBefore(20); table.setSpacingAfter(20); table.setLockedWidth(false); table.setWidthPercentage(90); return table; }
From source file:org.sonar.report.pdf.TeamWorkbookPDFReporter.java
License:Open Source License
private PdfPTable createViolationsDetailedTable(String ruleName, List<String> files, List<String> lines) { // TODO: internationalize this PdfPTable table = new PdfPTable(10); Iterator<String> itLeft = files.iterator(); Iterator<String> itRight = lines.iterator(); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(new Color(255, 228, 181)); table.addCell(new Phrase("Rule", Style.NORMAL_FONT)); table.getDefaultCell().setColspan(9); table.getDefaultCell().setBackgroundColor(Color.WHITE); table.addCell(new Phrase(ruleName, Style.NORMAL_FONT)); table.getDefaultCell().setColspan(10); table.getDefaultCell().setBackgroundColor(Color.GRAY); table.addCell(""); table.getDefaultCell().setColspan(7); table.getDefaultCell().setBackgroundColor(new Color(255, 228, 181)); table.addCell(new Phrase("File", Style.NORMAL_FONT)); table.getDefaultCell().setColspan(3); table.addCell(new Phrase("Line", Style.NORMAL_FONT)); table.getDefaultCell().setBackgroundColor(Color.WHITE); int i = 0;/* w w w . j av a2 s . co m*/ String lineNumbers = ""; while (i < files.size() - 1) { if (lineNumbers.equals("")) { lineNumbers += lines.get(i); } else { lineNumbers += ", " + lines.get(i); } if (!files.get(i).equals(files.get(i + 1))) { table.getDefaultCell().setColspan(7); table.addCell(files.get(i)); table.getDefaultCell().setColspan(3); table.addCell(lineNumbers); lineNumbers = ""; } i++; } table.getDefaultCell().setColspan(7); table.addCell(files.get(files.size() - 1)); table.getDefaultCell().setColspan(3); if (lineNumbers.equals("")) { lineNumbers += lines.get(i); } else { lineNumbers += ", " + lines.get(lines.size() - 1); } table.addCell(lineNumbers); table.setSpacingBefore(20); table.setSpacingAfter(20); table.setLockedWidth(false); table.setWidthPercentage(90); return table; }
From source file:org.sonarqube.report.extendedpdf.ExtendedHeader.java
License:Open Source License
public void onEndPage(PdfWriter writer, Document document) { String pageTemplate = "/templates/page.pdf"; try {/*from w ww . j a va 2 s . c o m*/ PdfContentByte cb = writer.getDirectContentUnder(); PdfReader reader = new PdfReader(this.getClass().getResourceAsStream(pageTemplate)); PdfImportedPage page = writer.getImportedPage(reader, 1); cb.addTemplate(page, 0, 0); Font font = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY); Rectangle pageSize = document.getPageSize(); PdfPTable head = new PdfPTable(1); head.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE); head.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER); head.getDefaultCell().setBorder(0); Phrase projectName = new Phrase(project.getName(), font); head.addCell(projectName); head.setTotalWidth(pageSize.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), pageSize.getHeight() - 15, writer.getDirectContent()); head.setSpacingAfter(10); PdfPTable foot = new PdfPTable(1); foot.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE); foot.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); foot.getDefaultCell().setBorder(0); SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm"); Phrase projectAnalysisDate = new Phrase(df.format(project.getMeasures().getDate()), font); foot.addCell(projectAnalysisDate); foot.setTotalWidth(pageSize.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), 20, writer.getDirectContent()); foot.setSpacingBefore(10); } catch (IOException e) { Logger.error("Cannot find the required template: " + pageTemplate); e.printStackTrace(); } }
From source file:org.sonarqube.report.extendedpdf.OverviewPDFReporter.java
License:Open Source License
protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter) throws org.dom4j.DocumentException, ReportException { String frontPageTemplate = "/templates/frontpage.pdf"; try {/*from w w w. j a va 2 s . c om*/ PdfContentByte cb = frontPageWriter.getDirectContent(); PdfReader reader = new PdfReader(this.getClass().getResourceAsStream(frontPageTemplate)); PdfImportedPage page = frontPageWriter.getImportedPage(reader, 1); frontPageDocument.newPage(); cb.addTemplate(page, 0, 0); Project project = getProject(); Rectangle pageSize = frontPageDocument.getPageSize(); PdfPTable projectInfo = new PdfPTable(1); projectInfo.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE); projectInfo.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); projectInfo.getDefaultCell().setBorder(Rectangle.BOTTOM); projectInfo.getDefaultCell().setPaddingBottom(10); projectInfo.getDefaultCell().setBorderColor(Color.GRAY); Font font = FontFactory.getFont(FontFactory.COURIER, 18, Font.NORMAL, Color.LIGHT_GRAY); Phrase projectName = new Phrase("Project: " + project.getName(), font); projectInfo.addCell(projectName); Phrase projectVersion = new Phrase("Version: " + project.getMeasures().getVersion(), font); projectInfo.addCell(projectVersion); SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm"); Phrase projectAnalysisDate = new Phrase("Analysis Date: " + df.format(project.getMeasures().getDate()), font); projectInfo.addCell(projectAnalysisDate); projectInfo.setTotalWidth( pageSize.getWidth() - frontPageDocument.leftMargin() * 2 - frontPageDocument.rightMargin() * 2); projectInfo.writeSelectedRows(0, -1, frontPageDocument.leftMargin(), pageSize.getHeight() - 575, frontPageWriter.getDirectContent()); projectInfo.setSpacingAfter(10); } catch (IOException e) { Logger.error("Cannot find the required template: " + frontPageTemplate); e.printStackTrace(); } }
From source file:org.unitime.timetable.action.SolutionReportAction.java
License:Open Source License
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { SolutionReportForm myForm = (SolutionReportForm) form; sessionContext.checkPermission(Right.SolutionReports); // Read operation to be performed String op = (myForm.getOp() != null ? myForm.getOp() : request.getParameter("op")); Session session = SessionDAO.getInstance().get(sessionContext.getUser().getCurrentAcademicSessionId()); BitSet sessionDays = session.getDefaultDatePattern().getPatternBitSet(); int startDayDayOfWeek = Constants.getDayOfWeek(session.getDefaultDatePattern().getStartDate()); SolverProxy solver = courseTimetablingSolverService.getSolver(); if (solver == null) { request.setAttribute("SolutionReport.message", "Neither a solver is started nor solution is loaded."); } else {//from www .ja v a 2s . co m try { for (RoomType type : RoomType.findAll()) { RoomReport roomReport = solver.getRoomReport(sessionDays, startDayDayOfWeek, type.getUniqueId()); if (roomReport != null && !roomReport.getGroups().isEmpty()) { WebTable t = getRoomReportTable(request, roomReport, false, type.getUniqueId()); if (t != null) request.setAttribute("SolutionReport.roomReportTable." + type.getReference(), t.printTable( WebTable.getOrder(sessionContext, "solutionReports.roomReport.ord"))); } } RoomReport roomReport = solver.getRoomReport(sessionDays, startDayDayOfWeek, null); if (roomReport != null && !roomReport.getGroups().isEmpty()) { WebTable t = getRoomReportTable(request, roomReport, false, null); if (t != null) request.setAttribute("SolutionReport.roomReportTable.nonUniv", t.printTable(WebTable.getOrder(sessionContext, "solutionReports.roomReport.ord"))); } DeptBalancingReport deptBalancingReport = solver.getDeptBalancingReport(); if (deptBalancingReport != null && !deptBalancingReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.deptBalancingReportTable", getDeptBalancingReportTable(request, deptBalancingReport, false).printTable( WebTable.getOrder(sessionContext, "solutionReports.deptBalancingReport.ord"))); ViolatedDistrPreferencesReport violatedDistrPreferencesReport = solver .getViolatedDistrPreferencesReport(); if (violatedDistrPreferencesReport != null && !violatedDistrPreferencesReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.violatedDistrPreferencesReportTable", getViolatedDistrPreferencesReportTable(request, violatedDistrPreferencesReport, false) .printTable(WebTable.getOrder(sessionContext, "solutionReports.violDistPrefReport.ord"))); DiscouragedInstructorBtbReport discouragedInstructorBtbReportReport = solver .getDiscouragedInstructorBtbReport(); if (discouragedInstructorBtbReportReport != null && !discouragedInstructorBtbReportReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.discouragedInstructorBtbReportReportTable", getDiscouragedInstructorBtbReportReportTable(request, discouragedInstructorBtbReportReport, false).printTable( WebTable.getOrder(sessionContext, "solutionReports.violInstBtb.ord"))); StudentConflictsReport studentConflictsReport = solver.getStudentConflictsReport(); if (studentConflictsReport != null && !studentConflictsReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.studentConflictsReportTable", getStudentConflictsReportTable(request, studentConflictsReport, false) .printTable(WebTable.getOrder(sessionContext, "solutionReports.studConf.ord"))); SameSubpartBalancingReport sameSubpartBalancingReport = solver.getSameSubpartBalancingReport(); if (sameSubpartBalancingReport != null && !sameSubpartBalancingReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.sameSubpartBalancingReportTable", getSameSubpartBalancingReportTable(request, sameSubpartBalancingReport, false) .printTable(PdfWebTable.getOrder(sessionContext, "solutionReports.sectBalancingReport.ord"))); PerturbationReport perturbationReport = solver.getPerturbationReport(); if (perturbationReport != null && !perturbationReport.getGroups().isEmpty()) request.setAttribute("SolutionReport.perturbationReportTable", getPerturbationReportTable(request, perturbationReport, false) .printTable(WebTable.getOrder(sessionContext, "solutionReports.pert.ord"))); } catch (Exception e) { e.printStackTrace(); } } if ("Export PDF".equals(op)) { OutputStream out = ExportUtils.getPdfOutputStream(response, "report"); Document doc = new Document( new Rectangle(60f + PageSize.LETTER.getHeight(), 60f + 0.75f * PageSize.LETTER.getHeight()), 30, 30, 30, 30); PdfWriter iWriter = PdfWriter.getInstance(doc, out); iWriter.setPageEvent(new PdfEventHandler()); doc.open(); boolean atLeastOneRoomReport = false; for (RoomType type : RoomType.findAll()) { RoomReport roomReport = solver.getRoomReport(sessionDays, startDayDayOfWeek, type.getUniqueId()); if (roomReport == null || roomReport.getGroups().isEmpty()) continue; PdfWebTable table = getRoomReportTable(request, roomReport, true, type.getUniqueId()); if (table == null) continue; PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.roomReport.ord")); if (!atLeastOneRoomReport) { doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); } doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); atLeastOneRoomReport = true; } RoomReport roomReport = solver.getRoomReport(sessionDays, startDayDayOfWeek, null); if (roomReport != null && !roomReport.getGroups().isEmpty()) { PdfWebTable table = getRoomReportTable(request, roomReport, true, null); if (table != null) { PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.roomReport.ord")); if (!atLeastOneRoomReport) { doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); } doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); atLeastOneRoomReport = true; } } if (atLeastOneRoomReport) { PdfPTable pdfTable = new PdfPTable(new float[] { 10f, 100f }); pdfTable.setWidthPercentage(100); pdfTable.getDefaultCell().setPadding(3); pdfTable.getDefaultCell().setBorderWidth(0); pdfTable.setSplitRows(false); pdfTable.addCell("Group"); pdfTable.addCell("group size <minimum, maximum)"); pdfTable.addCell("Size"); pdfTable.addCell("actual group size (size of the smallest and the biggest room in the group)"); pdfTable.addCell("NrRooms"); pdfTable.addCell("number of rooms in the group"); pdfTable.addCell("ClUse"); pdfTable.addCell("number of classes that are using a room from the group (actual solution)"); pdfTable.addCell("ClShould"); pdfTable.addCell( "number of classes that \"should\" use a room of the group (smallest available room of a class is in this group)"); pdfTable.addCell("ClMust"); pdfTable.addCell( "number of classes that must use a room of the group (all available rooms of a class are in this group)"); pdfTable.addCell("HrUse"); pdfTable.addCell("average hours a room of the group is used (actual solution)"); pdfTable.addCell("HrShould"); pdfTable.addCell( "average hours a room of the group should be used (smallest available room of a class is in this group)"); pdfTable.addCell("HrMust"); pdfTable.addCell( "average hours a room of this group must be used (all available rooms of a class are in this group)"); pdfTable.addCell(""); pdfTable.addCell("*) cumulative numbers (group minimum ... inf) are displayed in parentheses."); doc.add(pdfTable); } DiscouragedInstructorBtbReport discouragedInstructorBtbReportReport = solver .getDiscouragedInstructorBtbReport(); if (discouragedInstructorBtbReportReport != null && !discouragedInstructorBtbReportReport.getGroups().isEmpty()) { PdfWebTable table = getDiscouragedInstructorBtbReportReportTable(request, discouragedInstructorBtbReportReport, true); PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.violInstBtb.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); } ViolatedDistrPreferencesReport violatedDistrPreferencesReport = solver .getViolatedDistrPreferencesReport(); if (violatedDistrPreferencesReport != null && !violatedDistrPreferencesReport.getGroups().isEmpty()) { PdfWebTable table = getViolatedDistrPreferencesReportTable(request, violatedDistrPreferencesReport, true); PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.violDistPrefReport.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); } StudentConflictsReport studentConflictsReport = solver.getStudentConflictsReport(); if (studentConflictsReport != null && !studentConflictsReport.getGroups().isEmpty()) { PdfWebTable table = getStudentConflictsReportTable(request, studentConflictsReport, true); PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.studConf.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); } SameSubpartBalancingReport sameSubpartBalancingReport = solver.getSameSubpartBalancingReport(); if (sameSubpartBalancingReport != null && !sameSubpartBalancingReport.getGroups().isEmpty()) { PdfWebTable table = getSameSubpartBalancingReportTable(request, sameSubpartBalancingReport, true); PdfPTable pdfTable = table.printPdfTable( WebTable.getOrder(sessionContext, "solutionReports.sectBalancingReport.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); } DeptBalancingReport deptBalancingReport = solver.getDeptBalancingReport(); if (deptBalancingReport != null && !deptBalancingReport.getGroups().isEmpty()) { PdfWebTable table = getDeptBalancingReportTable(request, deptBalancingReport, true); PdfPTable pdfTable = table.printPdfTable( WebTable.getOrder(sessionContext, "solutionReports.deptBalancingReport.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); } PerturbationReport perturbationReport = solver.getPerturbationReport(); if (perturbationReport != null && !perturbationReport.getGroups().isEmpty()) { PdfWebTable table = getPerturbationReportTable(request, perturbationReport, true); PdfPTable pdfTable = table .printPdfTable(WebTable.getOrder(sessionContext, "solutionReports.pert.ord")); doc.setPageSize(new Rectangle(60f + table.getWidth(), 60f + 0.75f * table.getWidth())); doc.newPage(); doc.add(new Paragraph(table.getName(), PdfFont.getBigFont(true))); doc.add(pdfTable); pdfTable = new PdfPTable(new float[] { 5f, 100f }); pdfTable.setWidthPercentage(100); pdfTable.getDefaultCell().setPadding(3); pdfTable.getDefaultCell().setBorderWidth(0); pdfTable.setSplitRows(false); pdfTable.addCell("Class"); pdfTable.addCell("Class name"); pdfTable.addCell("Time"); pdfTable.addCell("Time (initial -> assigned)"); pdfTable.addCell("Room"); pdfTable.addCell("Room (initial -> assigned)"); pdfTable.addCell("Dist"); pdfTable.addCell("Distance between assignments (if different are used buildings)"); pdfTable.addCell("St"); pdfTable.addCell("Number of affected students"); pdfTable.addCell("StT"); pdfTable.addCell("Number of affected students by time change"); pdfTable.addCell("StR"); pdfTable.addCell("Number of affected students by room change"); pdfTable.addCell("StB"); pdfTable.addCell("Number of affected students by building change"); pdfTable.addCell("Ins"); pdfTable.addCell("Number of affected instructors"); pdfTable.addCell("InsT"); pdfTable.addCell("Number of affected instructors by time change"); pdfTable.addCell("InsR"); pdfTable.addCell("Number of affected instructors by room change"); pdfTable.addCell("InsB"); pdfTable.addCell("Number of affected instructors by building change"); pdfTable.addCell("Rm"); pdfTable.addCell("Number of rooms changed"); pdfTable.addCell("Bld"); pdfTable.addCell("Number of buildings changed"); pdfTable.addCell("Tm"); pdfTable.addCell("Number of times changed"); pdfTable.addCell("Day"); pdfTable.addCell("Number of days changed"); pdfTable.addCell("Hr"); pdfTable.addCell("Number of hours changed"); pdfTable.addCell("TFSt"); pdfTable.addCell("Assigned building too far for instructor (from the initial one)"); pdfTable.addCell("TFIns"); pdfTable.addCell("Assigned building too far for students (from the initial one)"); pdfTable.addCell("DStC"); pdfTable.addCell("Difference in student conflicts"); pdfTable.addCell("NStC"); pdfTable.addCell("Number of new student conflicts"); pdfTable.addCell("DTPr"); pdfTable.addCell("Difference in time preferences"); pdfTable.addCell("DRPr"); pdfTable.addCell("Difference in room preferences"); pdfTable.addCell("DInsB"); pdfTable.addCell("Difference in back-to-back instructor preferences"); doc.add(pdfTable); } doc.close(); out.flush(); out.close(); return null; } return mapping.findForward("showSolutionReport"); }
From source file:org.unitime.timetable.webutil.PdfWebTable.java
License:Open Source License
/** * Prints pdf table. By default does not split table across * page boundaries /*from w ww . j av a2s .c om*/ * @param ordCol * @param keepTogether true does not split table across pages * @return */ public PdfPTable printPdfTable(int ordCol, boolean keepTogether) { PdfPTable table = new PdfPTable(getNrColumns()); table.setWidthPercentage(100); table.getDefaultCell().setPadding(3); table.getDefaultCell().setBorderWidth(0); table.setSplitRows(false); table.setKeepTogether(keepTogether); boolean asc = (ordCol == 0 || iAsc == null || iAsc.length <= Math.abs(ordCol) - 1 ? true : iAsc[Math.abs(ordCol) - 1]); if (ordCol < 0) asc = !asc; widths = new float[iColumns]; for (int i = 0; i < iColumns; i++) widths[i] = 0f; String lastLine[] = new String[Math.max(iColumns, (iHeaders == null ? 0 : iHeaders.length))]; if (iHeaders != null) { for (int i = 0; i < iColumns; i++) { if (isFiltered(i)) continue; PdfPCell c = createCell(); c.setBorderWidthBottom(1); float width = addText(c, iHeaders[i] == null ? "" : iHeaders[i], true); widths[i] = Math.max(widths[i], width); String align = (iAlign != null ? iAlign[i] : "left"); if ("left".equals(align)) c.setHorizontalAlignment(Element.ALIGN_LEFT); if ("right".equals(align)) c.setHorizontalAlignment(Element.ALIGN_RIGHT); if ("center".equals(align)) c.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c); } table.setHeaderRows(1); } if (ordCol != 0) { Collections.sort(iLines, new WebTableComparator(Math.abs(ordCol) - 1, asc)); } for (int el = 0; el < iLines.size(); el++) { WebTableLine wtline = (WebTableLine) iLines.elementAt(el); String[] line = wtline.getLine(); boolean blank = iBlankWhenSame; for (int i = 0; i < iColumns; i++) { if (isFiltered(i)) continue; if (blank && line[i] != null && !line[i].equals(lastLine[i])) blank = false; PdfPCell c = createCell(); float width = addText(c, blank || line[i] == null ? "" : line[i], false); widths[i] = Math.max(widths[i], width); String align = (iAlign != null ? iAlign[i] : "left"); if ("left".equals(align)) c.setHorizontalAlignment(Element.ALIGN_LEFT); if ("right".equals(align)) c.setHorizontalAlignment(Element.ALIGN_RIGHT); if ("center".equals(align)) c.setHorizontalAlignment(Element.ALIGN_CENTER); applyPdfStyle(c, wtline, (el + 1 < iLines.size() ? (WebTableLine) iLines.elementAt(el + 1) : null), ordCol); table.addCell(c); lastLine[i] = line[i]; } } try { if (getNrFilteredColumns() < 0) { table.setWidths(widths); } else { float[] x = new float[getNrColumns()]; int idx = 0; for (int i = 0; i < iColumns; i++) { if (isFiltered(i)) continue; x[idx++] = widths[i]; } table.setWidths(x); } } catch (Exception e) { e.printStackTrace(); } return table; }
From source file:org.viafirma.util.QRCodeUtil.java
License:Apache License
/** * @param texto/* w ww.j a v a 2 s .c om*/ * Texto a colocar * @param textoQR * Imagen QR a colocar * @param codFirma * Cdigo de Firma a generar. * @param url Url del servicio de verificacin desde donde se puede descargar el documento. * @param document * Documeto destino * @param pageSize * Tamao de la pgina * @return * @throws BadElementException * @throws MalformedURLException * @throws IOException * @throws DocumentException * @throws ExcepcionErrorInterno */ private static float addContent(String texto, String url, String textoQR, String codFirma, Document document, Rectangle pageSize, boolean completo, boolean isSellado) throws BadElementException, MalformedURLException, IOException, DocumentException, ExcepcionErrorInterno { Image imagenCabezera = null; float widthCabecera = 0; float heightCabecera = 0; if (!isSellado) { // Recuperamos la imagen de cabecera imagenCabezera = Image.getInstance(QRCodeUtil.class.getResource(IMAGE_CABECERA)); // Colocamos la imagen en la zona superior de la pgina imagenCabezera.setAbsolutePosition(0, pageSize.getHeight() - imagenCabezera.getHeight()); heightCabecera = imagenCabezera.getHeight(); widthCabecera = imagenCabezera.getWidth(); } // Recuperamos el pie de firma Image imagenQR = Image.getInstance(QRCodeUtil.getInstance().generate(texto, url, textoQR, codFirma)); float rescalado = pageSize.getWidth() / (widthCabecera + document.leftMargin() + document.rightMargin()); float sizeCabecera = 0; if (rescalado < 1f && !isSellado) { // Requiere rescalado, la imagen es mayor que la pgina. imagenCabezera.scalePercent(rescalado * 100); sizeCabecera = rescalado * imagenCabezera.getHeight(); } float sizeCabecerayPie = HEIGHT_QR_CODE_PDF + sizeCabecera + document.bottomMargin() + document.topMargin() + SPACE_SEPARACION; // float escaleQR = HEIGHT_QR_CODE_PDF / (imagenQR.getHeight() + 5); float escaleQR = (pageSize.getWidth() - document.leftMargin() - document.rightMargin()) / (imagenQR.getWidth() + 6); // imagen.setSpacingAfter(120); if (!isSellado) { document.add(imagenCabezera); } // Aadimos una caja de texto si estamos mostrando el contenido del // fichero firmado. if (completo) { // Aadimos el espacio ocupado por la imagen Paragraph p = new Paragraph(""); p.setSpacingAfter(heightCabecera * rescalado); document.add(p); // Aadimos una tabla con borde donde colocar el documento. PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.getDefaultCell().setBorderWidth(10); PdfPCell celda = new PdfPCell(); celda.setFixedHeight(pageSize.getHeight() - sizeCabecerayPie); table.addCell(celda); table.setSpacingAfter(SPACE_SEPARACION); document.add(table); } if (completo) { // La imagen la colocamos justo debajo imagenQR.setAbsolutePosition(document.leftMargin(), escaleQR * HEIGHT_QR_CODE_PDF - SPACE_SEPARACION * 2); } else { imagenQR.setAbsolutePosition(document.leftMargin(), pageSize.getHeight() - sizeCabecerayPie); } imagenQR.scalePercent(escaleQR * 100); document.add(imagenQR); return sizeCabecerayPie; }
From source file:org.webguitoolkit.ui.util.export.PDFEvent.java
License:Apache License
public void onEndPage(PdfWriter writer, Document document) { TableExportOptions exportOptions = wgtTable.getExportOptions(); try {//from www. ja va 2s .c om Rectangle page = document.getPageSize(); if (exportOptions.isShowDefaultHeader() || StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { PdfPTable head = new PdfPTable(3); head.getDefaultCell().setBorder(Rectangle.NO_BORDER); Paragraph title = new Paragraph(wgtTable.getTitle()); title.setAlignment(Element.ALIGN_LEFT); head.addCell(title); Paragraph empty = new Paragraph(""); head.addCell(empty); if (StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { try { URL absoluteFileUrl = wgtTable.getPage().getClass() .getResource("/" + exportOptions.getHeaderImage()); if (absoluteFileUrl != null) { String path = absoluteFileUrl.getPath(); Image jpg = Image.getInstance(path); jpg.scaleAbsoluteHeight(40); jpg.scaleAbsoluteWidth(200); head.addCell(jpg); } } catch (Exception e) { logger.error(e.getMessage()); Paragraph noImage = new Paragraph("Image not found!"); head.addCell(noImage); } } else { head.addCell(empty); } head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); } if (exportOptions.isShowDefaultFooter() || StringUtils.isNotEmpty(exportOptions.getFooterText()) || exportOptions.isShowPageNumber()) { PdfPTable foot = new PdfPTable(3); String footerText = exportOptions.getFooterText() != null ? exportOptions.getFooterText() : ""; if (!exportOptions.isShowDefaultFooter()) { foot.addCell(new Paragraph(footerText)); foot.addCell(new Paragraph("")); } else { foot.getDefaultCell().setBorder(Rectangle.NO_BORDER); String leftText = ""; if (StringUtils.isNotEmpty(exportOptions.getFooterText())) { leftText = exportOptions.getFooterText(); } Paragraph left = new Paragraph(leftText); left.setAlignment(Element.ALIGN_LEFT); foot.addCell(left); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, TextService.getLocale()); Date today = new Date(); String date = df.format(today); Paragraph center = new Paragraph(date); center.setAlignment(Element.ALIGN_CENTER); foot.addCell(center); } if (exportOptions.isShowPageNumber()) { Paragraph right = new Paragraph( TextService.getString("pdf.page@Page:") + " " + writer.getPageNumber()); right.setAlignment(Element.ALIGN_LEFT); foot.addCell(right); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } else { foot.addCell(new Paragraph("")); } } } catch (Exception e) { throw new ExceptionConverter(e); } }