List of usage examples for com.lowagie.text.pdf PdfPTable setComplete
public void setComplete(boolean complete)
From source file:classroom.filmfestival_b.Movies15.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1// w ww. j av a 2 s.c o m Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); PdfWriter writer = PdfWriter.getInstance(document, os); // step 3 document.open(); // step 4 Session session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery("from FilmTitle order by title"); java.util.List<FilmTitle> results = q.list(); PdfPTable table = new PdfPTable(2); table.setComplete(false); table.setWidths(new float[] { 1, 5 }); File f; Paragraph p; Chunk c; PdfPCell cell = new PdfPCell(); Font bold = new Font(Font.HELVETICA, 12, Font.BOLD); Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC); p = new Paragraph("FILMFESTIVAL", bold); p.setAlignment(Element.ALIGN_CENTER); cell.addElement(p); cell.setColspan(2); cell.setBorder(PdfPCell.NO_BORDER); table.addCell(cell); cell = new PdfPCell(); cell.setFixedHeight(20); cell.setColspan(2); cell.setBorder(PdfPCell.NO_BORDER); cell.setCellEvent(new Movies14().new PageCell()); table.addCell(cell); table.setHeaderRows(2); table.setFooterRows(1); int counter = 10; for (FilmTitle movie : results) { f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg"); if (f.exists()) { cell = new PdfPCell(Image.getInstance(f.getPath()), true); cell.setPadding(2); } else { cell = new PdfPCell(); } table.addCell(cell); p = new Paragraph(20); c = new Chunk(movie.getTitle(), bold); c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId()); p.add(c); c = new Chunk(" (" + movie.getYear() + ") ", italic); p.add(c); c = new Chunk("IMDB"); c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb()); p.add(c); cell = new PdfPCell(); cell.setUseAscender(true); cell.setUseDescender(true); cell.addElement(p); Set<DirectorName> directors = movie.getDirectorNames(); List list = new List(); for (DirectorName director : directors) { list.add(director.getName()); } cell.addElement(list); table.addCell(cell); if (counter % 10 == 0) { document.add(table); } System.out.println(writer.getPageNumber()); counter++; } table.setComplete(true); document.add(table); // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:de.jdufner.sudoku.generator.pdf.PdfPrinterImpl.java
License:Open Source License
private PdfPTable writePdfMetaTable(List<SudokuData> sudokus) throws DocumentException { // Tabelle fr Formatierung von 2 Sudokus nebeneinander PdfPTable metaTable = new PdfPTable(2); metaTable.getDefaultCell().setBorder(0); metaTable.getDefaultCell().setPadding(5); metaTable.setWidthPercentage(100);/*from www . java 2s. c o m*/ metaTable.setHorizontalAlignment(Element.ALIGN_CENTER); int cells = 0; for (SudokuData sudoku : sudokus) { metaTable.addCell(writePdfTable(sudoku)); cells++; } final int rest = cells % 2; for (int i = 0; i < rest; i++) { metaTable.addCell(""); } metaTable.setComplete(true); return metaTable; }
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 www .j av 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
/** * When we have to do some custom drawing in a block that is layed out by * iText, we first give an empty table with the good dimensions to iText, * then iText will call a callback with the actual position. When that * happens, we use the given drawer to do the actual drawing. *//* w w w . jav a 2 s. c o m*/ public static PdfPTable createPlaceholderTable(double width, double height, double spacingAfter, ChunkDrawer drawer, HorizontalAlign align, PDFCustomBlocks customBlocks) { PdfPTable placeHolderTable = new PdfPTable(1); placeHolderTable.setLockedWidth(true); placeHolderTable.setTotalWidth((float) width); final PdfPCell placeHolderCell = new PdfPCell(); placeHolderCell.setMinimumHeight((float) height); placeHolderCell.setPadding(0f); placeHolderCell.setBorder(PdfPCell.NO_BORDER); placeHolderTable.addCell(placeHolderCell); customBlocks.addChunkDrawer(drawer); placeHolderTable.setTableEvent(drawer); placeHolderTable.setComplete(true); final PdfPCell surroundingCell = new PdfPCell(placeHolderTable); surroundingCell.setPadding(0f); surroundingCell.setBorder(PdfPCell.NO_BORDER); if (align != null) { placeHolderTable.setHorizontalAlignment(align.getCode()); surroundingCell.setHorizontalAlignment(align.getCode()); } PdfPTable surroundingTable = new PdfPTable(1); surroundingTable.setSpacingAfter((float) spacingAfter); surroundingTable.addCell(surroundingCell); surroundingTable.setComplete(true); return surroundingTable; }
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 *///w w w.jav a2 s. 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; }