/** * Deletes a column in this table. * * @param column the number of the column that has to be deleted * @throws BadElementException */ public void deleteColumn(int column) throws BadElementException { float newWidths[] = new float[--columns]; System.arraycopy(widths, 0, newWidths, 0, column); System.arraycopy(widths, column + 1, newWidths, column, columns - column); setWidths(newWidths); System.arraycopy(widths, 0, newWidths, 0, columns); widths = newWidths; Row row; int size = rows.size(); for (int i = 0; i < size; i++) { row = (Row) rows.get(i); row.deleteColumn(column); rows.set(i, row); } if (column == columns) { curPosition.setLocation(curPosition.x + 1, 0); } }
/** * Shows how a table is split if it doesn't fit the page. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("table splitting"); // creation of the document with a certain size and certain margins Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); try { // creation of the different writers PdfWriter.getInstance(document, new FileOutputStream("repeatingtable.pdf")); // we add some meta information to the document document.addAuthor("Alan Soukup"); document.addSubject("This is the result of a Test."); document.open(); Table datatable = new Table(10); int headerwidths[] = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7}; datatable.setWidths(headerwidths); datatable.setWidth(100); datatable.setPadding(3); // the first cell spans 10 columns Cell cell = new Cell( new Phrase( "Administration -System Users Report", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD))); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setLeading(30); cell.setColspan(10); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); datatable.addCell(cell); // These cells span 2 rows datatable.getDefaultCell().setBorderWidth(2); datatable.getDefaultCell().setHorizontalAlignment(1); datatable.addCell("User Id"); datatable.addCell("Name\nAddress"); datatable.addCell("Company"); datatable.addCell("Department"); datatable.addCell("Admin"); datatable.addCell("Data"); datatable.addCell("Expl"); datatable.addCell("Prod"); datatable.addCell("Proj"); datatable.addCell("Online"); // this is the end of the table header datatable.endHeaders(); datatable.getDefaultCell().setBorderWidth(1); for (int i = 1; i < 30; i++) { datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); datatable.addCell("myUserId"); datatable.addCell( "Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name"); datatable.addCell("No Name Company"); datatable.addCell("D" + i); datatable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); datatable.addCell("No"); datatable.addCell("Yes"); datatable.addCell("No"); datatable.addCell("Yes"); datatable.addCell("No"); datatable.addCell("Yes"); } document.add(new Paragraph("com.lowagie.text.Table - Cells split")); document.add(datatable); document.newPage(); document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells split\n\n")); datatable.setConvert2pdfptable(true); document.add(datatable); document.newPage(); document.add(new Paragraph("com.lowagie.text.Table - Cells kept together")); datatable.setConvert2pdfptable(false); datatable.setCellsFitPage(true); document.add(datatable); document.newPage(); document.add(new Paragraph("com.lowagie.text.pdf.PdfPTable - Cells kept together\n\n")); datatable.setConvert2pdfptable(true); document.add(datatable); } catch (Exception e) { e.printStackTrace(); } // we close the document document.close(); }
// utility function to make an item Element. private Table makeItemElement(ShoppingCartItemData pItm) throws DocumentException { Table itmTbl = new PTable(mColumnCount); itmTbl.setWidth(100); itmTbl.setWidths(itmColumnWidth); itmTbl.getDefaultCell().setBorderColor(java.awt.Color.black); itmTbl.getDefaultCell().setVerticalAlignment(Cell.ALIGN_TOP); itmTbl.setOffset(0); itmTbl.setBorder(Table.NO_BORDER); if (!catalogOnly) { String t0 = ""; if (pItm.getIsaInventoryItem()) { t0 = "i"; } if (null != mSiteData && mSiteData.isAnInventoryAutoOrderItem(pItm.getProduct().getProductId())) { t0 += "a"; } Cell tpc0 = new Cell(makePhrase(t0, small, true)); if (!pItm.getIsaInventoryItem()) { tpc0.setBorder(0); } itmTbl.addCell(tpc0); } Cell tpc01 = new Cell(makePhrase("", normal, true)); itmTbl.addCell(tpc01); String t = ""; if (pItm.getProduct().isPackProblemSku()) { t += "*"; } if (t.length() > 0) t += " "; Cell tpc1 = new Cell(makePhrase(t + pItm.getActualSkuNum(), normal, true)); itmTbl.addCell(tpc1); itmTbl.addCell(makePhrase(pItm.getProduct().getCatalogProductShortDesc(), normal, true)); if (mShowSize) { itmTbl.addCell(makePhrase(pItm.getProduct().getSize(), normal, true)); } // itmTbl.addCell(makePhrase(pItm.getProduct().getPack(), normal, true)); // itmTbl.addCell(makePhrase(pItm.getProduct().getUom(), normal, true)); // itmTbl.addCell(makePhrase(pItm.getProduct().getManufacturerName(),normal,true)); if (mShowPrice) { BigDecimal price = new BigDecimal(pItm.getPrice()); String priceStr = ""; try { priceStr = mFormatter.priceFormatWithoutCurrency(price); } catch (Exception exc) { exc.printStackTrace(); } Cell pcell = new Cell(makePhrase(priceStr, normal, true)); pcell.setHorizontalAlignment(Element.ALIGN_RIGHT); itmTbl.addCell(pcell); } if (catalogOnly) { if (pItm.getProduct() != null && pItm.getProduct().getCatalogDistrMapping() != null && Utility.isTrue(pItm.getProduct().getCatalogDistrMapping().getStandardProductList())) { String yStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.y", null); itmTbl.addCell(makePhrase(yStr, normal, true)); } else { String nStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.n", null); itmTbl.addCell(makePhrase(nStr, normal, true)); } } if (!catalogOnly) { // BigDecimal amount = new BigDecimal(pItm.getAmount()); itmTbl.addCell(makePhrase("", normal, true)); } return itmTbl; }
/** @noinspection IOResourceOpenedButNotSafelyClosed */ public void print( final LogicalPageKey logicalPageKey, final LogicalPageBox logicalPage, final TableContentProducer contentProducer, final RTFOutputProcessorMetaData metaData, final boolean incremental) throws ContentProcessingException { final int startRow = contentProducer.getFinishedRows(); final int finishRow = contentProducer.getFilledRows(); if (incremental && startRow == finishRow) { return; } if (document == null) { this.cellBackgroundProducer = new CellBackgroundProducer( metaData.isFeatureSupported(AbstractTableOutputProcessor.TREAT_ELLIPSE_AS_RECTANGLE), metaData.isFeatureSupported(OutputProcessorFeature.UNALIGNED_PAGEBANDS)); final PhysicalPageBox pageFormat = logicalPage.getPageGrid().getPage(0, 0); final float urx = (float) StrictGeomUtility.toExternalValue(pageFormat.getWidth()); final float ury = (float) StrictGeomUtility.toExternalValue(pageFormat.getHeight()); final float marginLeft = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableX()); final float marginRight = (float) StrictGeomUtility.toExternalValue( pageFormat.getWidth() - pageFormat.getImageableWidth() - pageFormat.getImageableX()); final float marginTop = (float) StrictGeomUtility.toExternalValue(pageFormat.getImageableY()); final float marginBottom = (float) StrictGeomUtility.toExternalValue( pageFormat.getHeight() - pageFormat.getImageableHeight() - pageFormat.getImageableY()); final Rectangle pageSize = new Rectangle(urx, ury); document = new Document(pageSize, marginLeft, marginRight, marginTop, marginBottom); imageCache = new RTFImageCache(resourceManager); // rtf does not support PageFormats or other meta data... final PatchRtfWriter2 instance = PatchRtfWriter2.getInstance(document, new NoCloseOutputStream(outputStream)); instance.getDocumentSettings().setAlwaysUseUnicode(true); final String author = config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Author"); if (author != null) { document.addAuthor(author); } final String title = config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.Title"); if (title != null) { document.addTitle(title); } document.addProducer(); document.addCreator(RTFPrinter.CREATOR); try { document.addCreationDate(); } catch (Exception e) { RTFPrinter.logger.debug("Unable to add creation date. It will have to work without it.", e); } document.open(); } // Start a new page. try { final SheetLayout sheetLayout = contentProducer.getSheetLayout(); final int columnCount = contentProducer.getColumnCount(); if (table == null) { final int rowCount = contentProducer.getRowCount(); table = new Table(columnCount, rowCount); table.setAutoFillEmptyCells(false); table.setWidth(100); // span the full page.. // and finally the content .. final float[] cellWidths = new float[columnCount]; for (int i = 0; i < columnCount; i++) { cellWidths[i] = (float) StrictGeomUtility.toExternalValue(sheetLayout.getCellWidth(i, i + 1)); } table.setWidths(cellWidths); } // logger.debug ("Processing: " + startRow + " " + finishRow + " " + incremental); for (int row = startRow; row < finishRow; row++) { for (short col = 0; col < columnCount; col++) { final RenderBox content = contentProducer.getContent(row, col); final CellMarker.SectionType sectionType = contentProducer.getSectionType(row, col); if (content == null) { final RenderBox backgroundBox = contentProducer.getBackground(row, col); final CellBackground background; if (backgroundBox != null) { background = cellBackgroundProducer.getBackgroundForBox( logicalPage, sheetLayout, col, row, 1, 1, true, sectionType, backgroundBox); } else { background = cellBackgroundProducer.getBackgroundAt( logicalPage, sheetLayout, col, row, true, sectionType); } if (background == null) { // An empty cell .. ignore final PatchRtfCell cell = new PatchRtfCell(); cell.setBorderWidth(0); cell.setMinimumHeight( (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row))); table.addCell(cell, row, col); continue; } // A empty cell with a defined background .. final PatchRtfCell cell = new PatchRtfCell(); cell.setBorderWidth(0); cell.setMinimumHeight( (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row))); updateCellStyle(cell, background); table.addCell(cell, row, col); continue; } if (content.isCommited() == false) { throw new InvalidReportStateException("Uncommited content encountered"); } final long contentOffset = contentProducer.getContentOffset(row, col); final long colPos = sheetLayout.getXPosition(col); final long rowPos = sheetLayout.getYPosition(row); if (content.getX() != colPos || (content.getY() + contentOffset) != rowPos) { // A spanned cell .. continue; } final int colSpan = sheetLayout.getColSpan(col, content.getX() + content.getWidth()); final int rowSpan = sheetLayout.getRowSpan(row, content.getY() + content.getHeight() + contentOffset); final CellBackground realBackground = cellBackgroundProducer.getBackgroundForBox( logicalPage, sheetLayout, col, row, colSpan, rowSpan, false, sectionType, content); final PatchRtfCell cell = new PatchRtfCell(); cell.setRowspan(rowSpan); cell.setColspan(colSpan); cell.setBorderWidth(0); cell.setMinimumHeight( (float) StrictGeomUtility.toExternalValue(sheetLayout.getRowHeight(row))); if (realBackground != null) { updateCellStyle(cell, realBackground); } computeCellStyle(content, cell); // export the cell and all content .. final RTFTextExtractor etx = new RTFTextExtractor(metaData); etx.compute(content, cell, imageCache); table.addCell(cell, row, col); content.setFinishedTable(true); // logger.debug("set Finished to cell (" + col + ", " + row + "," + content.getName() + // ")"); } } if (incremental == false) { document.add(table); table = null; } } catch (DocumentException e) { throw new ContentProcessingException("Failed to generate RTF-Document", e); } }
/** * Creates a Table object based on this TableAttributes object. * * @return a com.lowagie.text.Table object * @throws BadElementException */ public Table createTable() throws BadElementException { if (content.isEmpty()) throw new BadElementException("Trying to create a table without rows."); SimpleCell row = (SimpleCell) content.get(0); SimpleCell cell; int columns = 0; for (Iterator i = row.getContent().iterator(); i.hasNext(); ) { cell = (SimpleCell) i.next(); columns += cell.getColspan(); } float[] widths = new float[columns]; float[] widthpercentages = new float[columns]; Table table = new Table(columns); table.setAlignment(alignment); table.setSpacing(cellspacing); table.setPadding(cellpadding); table.cloneNonPositionParameters(this); int pos; for (Iterator rows = content.iterator(); rows.hasNext(); ) { row = (SimpleCell) rows.next(); pos = 0; for (Iterator cells = row.getContent().iterator(); cells.hasNext(); ) { cell = (SimpleCell) cells.next(); table.addCell(cell.createCell(row)); if (cell.getColspan() == 1) { if (cell.getWidth() > 0) widths[pos] = cell.getWidth(); if (cell.getWidthpercentage() > 0) widthpercentages[pos] = cell.getWidthpercentage(); } pos += cell.getColspan(); } } float sumWidths = 0f; for (int i = 0; i < columns; i++) { if (widths[i] == 0) { sumWidths = 0; break; } sumWidths += widths[i]; } if (sumWidths > 0) { table.setWidth(sumWidths); table.setLocked(true); table.setWidths(widths); } else { for (int i = 0; i < columns; i++) { if (widthpercentages[i] == 0) { sumWidths = 0; break; } sumWidths += widthpercentages[i]; } if (sumWidths > 0) { table.setWidths(widthpercentages); } } if (width > 0) { table.setWidth(width); table.setLocked(true); } else if (widthpercentage > 0) { table.setWidth(widthpercentage); } return table; }
/** * Sets the widths of the different columns (percentages). * * <p>You can give up relative values of borderwidths. The sum of these values will be considered * 100%. The values will be recalculated as percentages of this sum. * * @param widths an array with values * @throws DocumentException */ public void setWidths(int[] widths) throws DocumentException { float tb[] = new float[widths.length]; for (int k = 0; k < widths.length; ++k) tb[k] = widths[k]; setWidths(tb); }