/* * (non-Javadoc) * * @see com.afunms.report.ExportInterface#insertChart(java.lang.String) */ public void insertChart(String path) throws Exception { if (!document.isOpen()) { document.open(); } Image png = Image.getInstance(path); // png.scaleAbsolute(560, 320); png.scalePercent(90); Table pngtable = new Table(1); pngtable.setAutoFillEmptyCells(true); pngtable.setAlignment(Element.ALIGN_CENTER); pngtable.setCellsFitPage(true); pngtable.setWidth(100); pngtable.setBorder(0); RtfCell cell = new RtfCell(png); cell.setBorder(0); pngtable.addCell(cell); document.add(pngtable); }
/* * (non-Javadoc) * * @see com.afunms.report.ExportInterface#insertTable(java.util.ArrayList) */ public void insertTable(ArrayList<String[]> tableal) throws Exception { // step 3: we open the document if (!document.isOpen()) { document.open(); } Table pdfTable = new Table(tableal.get(0).length); for (int k = 0; k < tableal.size(); k++) { String[] row = tableal.get(k); for (int j = 0; j < row.length; j++) { Cell pdfcell = new Cell(); if (k == 0) { pdfcell.addElement(new Paragraph(row[j], FontChineseTitle)); pdfcell.setBackgroundColor(Color.gray); pdfTable.endHeaders(); } else { pdfcell.addElement(new Paragraph(row[j], FontChineseRow)); if (k % 2 == 0) { pdfcell.setBackgroundColor(Color.LIGHT_GRAY); } } // 合并单元格 // pdfcell.setColspan(1); // pdfcell.setRowspan(1); // 对齐方式 pdfcell.setHorizontalAlignment(Element.ALIGN_CENTER); pdfcell.setVerticalAlignment(Element.ALIGN_MIDDLE); pdfTable.addCell(pdfcell); } } pdfTable.setWidth(100); // 设置表格填距 pdfTable.setPadding(5); pdfTable.setAlignment(Element.ALIGN_CENTER); // pdfTable.setTableFitsPage(true); document.add(pdfTable); // step 5: we close the document }
/** * 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; }