List of usage examples for com.lowagie.text.pdf PdfPTable setWidthPercentage
public void setWidthPercentage(float widthPercentage)
From source file:org.mapfish.print.config.layout.PivotTableBlock.java
License:Open Source License
/** * Creates a PDF pivot table. Returns null if the table is empty * @throws DocumentException//from w ww .j a v a 2 s . c o m */ private PdfPTable buildPivotTable(PJsonObject params, RenderingContext context, TableConfig tableConfig) throws DocumentException { int nbColumns = 0, nbRows = 0; ArrayList<PivotTableCell> tableBlocks = new ArrayList<PivotTableCell>(); ArrayList<String> dataIndexes = new ArrayList<String>(); PJsonArray groupHeaderObj = params.getJSONArray("groupHeaders"); if (groupHeaderObj == null) { return null; } for (int i = 0; i < groupHeaderObj.size(); i++) { int lastnbRows = 0; int colspan = 1; PJsonArray CurgroupHeader = groupHeaderObj.getJSONArray(i); for (int j = 0; j < CurgroupHeader.size(); j++) { PJsonObject cell = CurgroupHeader.getJSONObject(j); colspan = cell.getInt("colspan"); lastnbRows += colspan; PivotTableCell newTb = new PivotTableCell(i, j, colspan); newTb.setText(cell.getString("header")); newTb.setAlign(HorizontalAlign.CENTER); newTb.setSpacingAfter(10); newTb.setFontSize(8.0); tableBlocks.add(newTb); } // check if the number of columns matches if (nbColumns == 0) { nbColumns = lastnbRows; } else { if (lastnbRows != nbColumns) { throw new DocumentException("Malformed JSON : number of columns does not match"); } } nbRows++; } // iterating on columns JSON object PJsonArray columnsObj = params.getJSONArray("columns"); if (nbColumns != columnsObj.size()) { throw new DocumentException("Malformed JSON : number of columns does not match"); } for (int i = 0; i < columnsObj.size(); i++) { PJsonObject column = columnsObj.getJSONObject(i); PivotTableCell newTb = new PivotTableCell(nbRows, i, 1); newTb.setText(column.getString("header")); newTb.setAlign(HorizontalAlign.CENTER); newTb.setSpacingAfter(10); tableBlocks.add(newTb); newTb.setFontSize(8.0); dataIndexes.add(column.getString("dataIndex")); } // we increment nbRows (we need to count the one coming with the columns JSON array nbRows++; // iterating on datas rows PJsonArray datasObj = params.getJSONArray("data"); for (int i = 0; i < datasObj.size(); i++) { PJsonObject column = datasObj.getJSONObject(i); //System.out.println(column.getString("header")); for (int j = 0; j < dataIndexes.size(); j++) { String curValue; try { Float cellFloat = column.getFloat(dataIndexes.get(j)); // one decimal by default // TODO : this should be parameterized into the YAML // configuration file. curValue = String.format("%.1f", cellFloat); } catch (Exception e) { try { curValue = column.getString(dataIndexes.get(j)); } catch (Exception e2) { curValue = ""; // not found ? falling back to empty // string value } } PivotTableCell newTb = new PivotTableCell(nbRows, j, 1); newTb.setText(curValue); newTb.setFontSize(5.0); newTb.setSpacingAfter(7); newTb.setAlign(HorizontalAlign.RIGHT); tableBlocks.add(newTb); } nbRows++; } final PdfPTable table = new PdfPTable(nbColumns); table.setWidthPercentage(100f); for (int i = 0; i < tableBlocks.size(); i++) { final PivotTableCell block = tableBlocks.get(i); if (block.isVisible(context, params) && !block.isAbsolute()) { final PdfPCell cell = createCell(params, context, block, block.getRowIndex(), block.getColumnIndex(), nbRows, nbColumns, tableConfig, block.getColSpan()); table.addCell(cell); } } table.setSplitRows(false); table.setComplete(true); return table; }
From source file:org.mapfish.print.PDFUtils.java
License:Open Source License
/** * Creates a PDF table with the given items. Returns null if the table is empty *///from w ww .ja v a 2s .c o m public static PdfPTable buildTable(List<Block> items, PJsonObject params, RenderingContext context, int nbColumns, TableConfig tableConfig) throws DocumentException { int nbCells = 0; for (int i = 0; i < items.size(); i++) { final Block block = items.get(i); if (block.isVisible(context, params)) { if (block.isAbsolute()) { // absolute blocks are rendered directly (special case for // header/footer containing absolute blocks; it should not // happen in other usecases). block.render(params, null, context); } else { nbCells++; } } } if (nbCells == 0) return null; nbColumns = nbColumns > 0 ? nbColumns : nbCells; int nbRows = (nbCells + nbColumns - 1) / nbColumns; final PdfPTable table = new PdfPTable(nbColumns); table.setWidthPercentage(100f); int cellNum = 0; for (int i = 0; i < items.size(); i++) { final Block block = items.get(i); if (block.isVisible(context, params) && !block.isAbsolute()) { final PdfPCell cell = createCell(params, context, block, cellNum / nbColumns, cellNum % nbColumns, nbRows, nbColumns, tableConfig); table.addCell(cell); cellNum++; } } table.setComplete(true); return table; }
From source file:org.mifosplatform.infrastructure.dataqueries.service.ReadReportingServiceImpl.java
License:Mozilla Public License
@Override public String retrieveReportPDF(final String reportName, final String type, final Map<String, String> queryParams) { final String fileLocation = FileSystemContentRepository.MIFOSX_BASE_DIR + File.separator + ""; if (!new File(fileLocation).isDirectory()) { new File(fileLocation).mkdirs(); }/*from ww w. j a v a 2 s.c om*/ final String genaratePdf = fileLocation + File.separator + reportName + ".pdf"; try { final GenericResultsetData result = retrieveGenericResultset(reportName, type, queryParams); final List<ResultsetColumnHeaderData> columnHeaders = result.getColumnHeaders(); final List<ResultsetRowData> data = result.getData(); List<String> row; logger.info("NO. of Columns: " + columnHeaders.size()); final Integer chSize = columnHeaders.size(); final Document document = new Document(PageSize.B0.rotate()); PdfWriter.getInstance(document, new FileOutputStream(new File(fileLocation + reportName + ".pdf"))); document.open(); final PdfPTable table = new PdfPTable(chSize); table.setWidthPercentage(100); for (int i = 0; i < chSize; i++) { table.addCell(columnHeaders.get(i).getColumnName()); } table.completeRow(); Integer rSize; String currColType; String currVal; logger.info("NO. of Rows: " + data.size()); for (int i = 0; i < data.size(); i++) { row = data.get(i).getRow(); rSize = row.size(); for (int j = 0; j < rSize; j++) { currColType = columnHeaders.get(j).getColumnType(); currVal = row.get(j); if (currVal != null) { if (currColType.equals("DECIMAL") || currColType.equals("DOUBLE") || currColType.equals("BIGINT") || currColType.equals("SMALLINT") || currColType.equals("INT")) { table.addCell(currVal.toString()); } else { table.addCell(currVal.toString()); } } } } table.completeRow(); document.add(table); document.close(); return genaratePdf; } catch (final Exception e) { logger.error("error.msg.reporting.error:" + e.getMessage()); throw new PlatformDataIntegrityException("error.msg.exception.error", e.getMessage()); } }
From source file:org.odftoolkit.odfdom.converter.internal.itext.stylable.StylableParagraph.java
License:Open Source License
private PdfPTable createTable(PdfPCell cell) { PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100.0f); table.addCell(cell);/*ww w. ja v a 2s . c o m*/ return table; }
From source file:org.openswing.swing.export.java.ExportToPDF14.java
License:Open Source License
private void prepareGenericComponent(PdfPTable parentTable, int parentTableCols, Document document, ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable { if (opt.getCellsContent() == null || opt.getCellsContent().length == 0) return;/* ww w . j av a 2 s.co m*/ int cols = opt.getCellsContent()[0].length; Object[] row = null; Object obj = null; SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat()); int[] headerwidths = new int[cols]; for (int i = 0; i < headerwidths.length; i++) headerwidths[i] = (int) PageSize.A4.width() / cols; PdfPTable table = new PdfPTable(cols); table.setWidths(headerwidths); table.setWidthPercentage(90); table.getDefaultCell().setBorderWidth(2); table.getDefaultCell().setBorderColor(Color.black); table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getHeaderGrayFill()); table.getDefaultCell().setPadding(3); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.setHeaderRows(0); table.getDefaultCell().setBorderWidth(0); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); for (int i = 0; i < opt.getCellsContent().length; i++) { row = opt.getCellsContent()[i]; for (int j = 0; j < row.length; j++) { obj = row[j]; if (obj != null) { if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(new Phrase(sdatf.format((java.util.Date) obj), (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, obj))); } else { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(new Phrase(obj.toString(), (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, obj))); } } else { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(new Phrase("", (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, null))); } } } if (parentTable != null) { PdfPCell cell = new PdfPCell(table); cell.setColspan(parentTableCols); parentTable.addCell(cell); } else document.add(table); }
From source file:org.openswing.swing.export.java.ExportToPDF14.java
License:Open Source License
private void prepareGrid(PdfPTable parentTable, int parentTableCols, Document document, ExportOptions exportOptions, GridExportOptions opt) throws Throwable { // prepare vo getters methods... String methodName = null;/*from w ww. java 2 s . co m*/ String attributeName = null; Hashtable gettersMethods = new Hashtable(); Method[] voMethods = opt.getValueObjectType().getMethods(); for (int i = 0; i < voMethods.length; i++) { methodName = voMethods[i].getName(); if (methodName.startsWith("get")) { attributeName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4); if (opt.getExportAttrColumns().contains(attributeName)) gettersMethods.put(attributeName, voMethods[i]); } } Response response = null; int start = 0; int rownum = 0; Object value = null; Object vo = null; int type; SimpleDateFormat sdf = new SimpleDateFormat(exportOptions.getDateFormat()); SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat()); SimpleDateFormat stf = new SimpleDateFormat(exportOptions.getTimeFormat()); int headerwidths[] = new int[opt.getExportColumns().size()]; int total = 0; for (int i = 0; i < opt.getExportColumns().size(); i++) { headerwidths[i] = Math.max(opt.getExportColumns().get(i).toString().length() * 10, ((Integer) opt.getColumnsWidth().get(opt.getExportAttrColumns().get(i))).intValue()); total += headerwidths[i]; } Paragraph line = null; if (opt.getTitle() != null && !opt.getTitle().equals("")) { line = new Paragraph(opt.getTitle(), (Font) exportOptions.getExportToPDFAdapter().getFontTitle()); line.setAlignment(Element.ALIGN_CENTER); document.add(line); document.add(new Paragraph("\n")); } String[] filters = opt.getFilteringConditions(); if (filters != null) { for (int i = 0; i < filters.length; i++) { line = new Paragraph(filters[i]); document.add(line); } document.add(new Paragraph("\n")); } PdfPTable table = new PdfPTable(opt.getExportColumns().size()); table.setWidths(headerwidths); table.setWidthPercentage(90); table.getDefaultCell().setBorderWidth(2); table.getDefaultCell().setBorderColor(Color.black); table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getHeaderGrayFill()); table.getDefaultCell().setPadding(3); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); for (int i = 0; i < opt.getExportColumns().size(); i++) table.addCell(new Phrase(opt.getExportColumns().get(i).toString(), (Font) exportOptions .getExportToPDFAdapter().getHeaderFont(opt.getExportAttrColumns().get(i).toString()))); table.setHeaderRows(1); table.getDefaultCell().setBorderWidth(1); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); for (int j = 0; j < opt.getTopRows().size(); j++) { // create a row for each top rows... table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getTopRowsGrayFill(j)); vo = opt.getTopRows().get(j); appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 0); } do { response = opt.getGridDataLocator().loadData(GridParams.NEXT_BLOCK_ACTION, start, opt.getFilteredColumns(), opt.getCurrentSortedColumns(), opt.getCurrentSortedVersusColumns(), opt.getValueObjectType(), opt.getOtherGridParams()); if (response.isError()) throw new Exception(response.getErrorMessage()); boolean even = false; for (int j = 0; j < ((VOListResponse) response).getRows().size(); j++) { if (even) { table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getEvenRowsGrayFill()); even = false; } else { table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getOddRowsGrayFill()); even = true; } vo = ((VOListResponse) response).getRows().get(j); appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, rownum, 1); rownum++; } start = start + ((VOListResponse) response).getRows().size(); if (!((VOListResponse) response).isMoreRows()) break; } while (rownum < opt.getMaxRows()); for (int j = 0; j < opt.getBottomRows().size(); j++) { // create a row for each bottom rows... table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getBottomRowsGrayFill(j)); vo = opt.getBottomRows().get(j); appendRow(document, exportOptions, table, vo, opt, gettersMethods, sdf, sdatf, stf, j, 2); } if (parentTable != null) { PdfPCell cell = new PdfPCell(table); cell.setColspan(parentTableCols); parentTable.addCell(cell); } else document.add(table); }
From source file:org.openswing.swing.export.java.ExportToPDF15.java
License:Open Source License
private void prepareGenericComponent(PdfPTable parentTable, int parentTableCols, Document document, ExportOptions exportOptions, ComponentExportOptions opt) throws Throwable { if (opt.getCellsContent() == null || opt.getCellsContent().length == 0) return;// w w w . j a va 2 s . c om int cols = opt.getCellsContent()[0].length; Object[] row = null; Object obj = null; SimpleDateFormat sdatf = new SimpleDateFormat(exportOptions.getDateTimeFormat()); int[] headerwidths = new int[cols]; for (int i = 0; i < headerwidths.length; i++) headerwidths[i] = (int) PageSize.A4.getWidth() / cols; PdfPTable table = new PdfPTable(cols); table.setWidths(headerwidths); table.setWidthPercentage(90); table.getDefaultCell().setBorderWidth(2); table.getDefaultCell().setBorderColor(Color.black); table.getDefaultCell().setGrayFill(exportOptions.getExportToPDFAdapter().getHeaderGrayFill()); table.getDefaultCell().setPadding(3); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); table.setHeaderRows(0); table.getDefaultCell().setBorderWidth(0); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); for (int i = 0; i < opt.getCellsContent().length; i++) { row = opt.getCellsContent()[i]; for (int j = 0; j < row.length; j++) { obj = row[j]; if (obj != null) { if (obj instanceof Date || obj instanceof java.util.Date || obj instanceof java.sql.Timestamp) { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(new Phrase(sdatf.format((java.util.Date) obj), (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, obj))); } else { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(new Phrase(obj.toString(), (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, obj))); } } else { table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.addCell(new Phrase("", (Font) exportOptions.getExportToPDFAdapter().getGenericComponentFont(i, j, null))); } } } if (parentTable != null) { PdfPCell cell = new PdfPCell(table); cell.setColspan(parentTableCols); parentTable.addCell(cell); } else document.add(table); }
From source file:org.opentestsystem.delivery.testreg.rest.view.PDFReportView.java
License:Open Source License
private PdfPTable createMessageHeaders(final String[] headerColumns, final HierarchyLevel level, final String message) throws BadElementException { PdfPTable table = null; if (level != null && level == HierarchyLevel.CLIENT) { table = new PdfPTable(headerColumns.length - 3); } else {//from w ww. j a v a2 s . c o m table = new PdfPTable(headerColumns.length); } table.setWidthPercentage(100); PdfPCell cell; cell = new PdfPCell(new Paragraph(message, HEADER_MESSAGE_FONT)); cell.setColspan(headerColumns.length); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(Rectangle.NO_BORDER); table.addCell(cell); return table; }
From source file:org.oscarehr.casemgmt.print.OscarChartPrinter.java
License:Open Source License
public void printDocHeaderFooter() throws DocumentException { String headerTitle = demographic.getFormattedName() + " " + demographic.getAge() + " " + demographic.getSex() + " DOB:" + demographic.getFormattedDob(); //set up document title and header ResourceBundle propResource = ResourceBundle.getBundle("oscarResources"); String title = propResource.getString("oscarEncounter.pdfPrint.title") + " " + (String) request.getAttribute("demoName") + "\n"; String gender = propResource.getString("oscarEncounter.pdfPrint.gender") + " " + (String) request.getAttribute("demoSex") + "\n"; String dob = propResource.getString("oscarEncounter.pdfPrint.dob") + " " + (String) request.getAttribute("demoDOB") + "\n"; String age = propResource.getString("oscarEncounter.pdfPrint.age") + " " + (String) request.getAttribute("demoAge") + "\n"; String mrp = propResource.getString("oscarEncounter.pdfPrint.mrp") + " " + (String) request.getAttribute("mrp") + "\n"; String[] info = new String[] { title, gender, dob, age, mrp }; ClinicData clinicData = new ClinicData(); clinicData.refreshClinicData();//from w w w .j a va2s . c o m String[] clinic = new String[] { clinicData.getClinicName(), clinicData.getClinicAddress(), clinicData.getClinicCity() + ", " + clinicData.getClinicProvince(), clinicData.getClinicPostal(), clinicData.getClinicPhone() }; if (newPage) { document.newPage(); newPage = false; } //Header will be printed at top of every page beginning with p2 Phrase headerPhrase = new Phrase(LEADING, headerTitle, boldFont); HeaderFooter header = new HeaderFooter(headerPhrase, false); header.setAlignment(HeaderFooter.ALIGN_CENTER); document.setHeader(header); getDocument().add(headerPhrase); getDocument().add(new Phrase("\n")); Paragraph p = new Paragraph("Tel:" + demographic.getPhone(), getFont()); p.setAlignment(Paragraph.ALIGN_LEFT); // getDocument().add(p); Paragraph p2 = new Paragraph("Date of Visit: ", getFont()); p2.setAlignment(Paragraph.ALIGN_RIGHT); // getDocument().add(p); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100f); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); PdfPCell cell1 = new PdfPCell(p); cell1.setBorder(PdfPCell.NO_BORDER); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell cell2 = new PdfPCell(p2); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); cell2.setBorder(PdfPCell.NO_BORDER); table.addCell(cell1); table.addCell(cell2); getDocument().add(table); table = new PdfPTable(3); table.setWidthPercentage(100f); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); cell1 = new PdfPCell(getParagraph("Signed Provider:" + ((signingProvider != null) ? signingProvider : ""))); cell1.setBorder(PdfPCell.NO_BORDER); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell2 = new PdfPCell(getParagraph("RFR:")); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setBorder(PdfPCell.NO_BORDER); PdfPCell cell3 = new PdfPCell(getParagraph("Ref:")); cell3.setHorizontalAlignment(Element.ALIGN_RIGHT); cell3.setBorder(PdfPCell.NO_BORDER); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); getDocument().add(table); //Write title with top and bottom borders on p1 cb = writer.getDirectContent(); cb.setColorStroke(new Color(0, 0, 0)); cb.setLineWidth(0.5f); cb.moveTo(document.left(), document.top() - (font.getCalculatedLeading(LINESPACING) * 5f)); cb.lineTo(document.right(), document.top() - (font.getCalculatedLeading(LINESPACING) * 5f)); cb.stroke(); }
From source file:org.oscarehr.casemgmt.print.OscarChartPrinter.java
License:Open Source License
public void printAppointmentHistory() throws DocumentException { if (newPage) { document.newPage();/*from www . j ava2 s . c o m*/ newPage = false; } List<Appointment> appts = appointmentDao.getAppointmentHistory(demographic.getDemographicNo()); PdfPTable table = new PdfPTable(6); table.setWidthPercentage(100f); table.getDefaultCell().setBorder(PdfPCell.NO_BORDER); table.addCell("Date"); table.addCell("From"); table.addCell("To"); table.addCell("Reason"); table.addCell("Provider"); table.addCell("Notes"); for (Appointment appt : appts) { table.addCell(generalCellForApptHistory(formatter.format(appt.getAppointmentDate()))); table.addCell(generalCellForApptHistory(timeFormatter.format(appt.getStartTime()))); table.addCell(generalCellForApptHistory(timeFormatter.format(appt.getEndTime()))); table.addCell(generalCellForApptHistory(appt.getReason())); table.addCell( generalCellForApptHistory(providerDao.getProvider(appt.getProviderNo()).getFormattedName())); table.addCell(generalCellForApptHistory(appt.getNotes())); } getDocument().add(table); }