List of usage examples for com.lowagie.text.pdf PdfPTable addCell
public void addCell(Phrase phrase)
From source file:classroom.filmfestival_b.Movies12.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1/*from w w w .j av a 2s. com*/ Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); 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.setWidths(new float[] { 1, 5 }); File f; Image img; Paragraph p; Chunk c; Font bold = new Font(Font.HELVETICA, 12, Font.BOLD); Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC); for (FilmTitle movie : results) { f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg"); if (f.exists()) { img = Image.getInstance(f.getPath()); table.addCell(img); } else { table.addCell(""); } 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); table.addCell(p); } document.add(table); // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:classroom.filmfestival_b.Movies13.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1//from w w w .ja va2 s . c o m Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); 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.setWidths(new float[] { 1, 5 }); File f; Paragraph p; Chunk c; PdfPCell cell; Font bold = new Font(Font.HELVETICA, 12, Font.BOLD); Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC); 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); } document.add(table); // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:classroom.filmfestival_b.Movies14.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1/*from w w w.j a v a2 s. c o m*/ Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); 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.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); 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); } document.add(table); // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:classroom.filmfestival_b.Movies15.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1// w ww. j a va 2 s . co 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:classroom.filmfestival_c.Movies16.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1// w ww . jav a 2 s . c o m Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); PdfWriter.getInstance(document, os); // step 3 document.open(); // step 4 PdfPTable table; PdfPCell cell; Chunk imdb; Session session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery( "select distinct festival.id.day from FestivalScreening as festival order by festival.id.day"); java.util.List<Date> days = q.list(); java.util.List<FestivalScreening> screenings; for (Date day : days) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(day); if (gc.get(GregorianCalendar.YEAR) != YEAR) continue; table = new PdfPTable(new float[] { 7, 1, 2, 1 }); cell = new PdfPCell(new Phrase(day.toString(), NORMALWHITE)); cell.setBackgroundColor(BLACK); cell.setColspan(4); table.addCell(cell); q = session.createQuery("from FestivalScreening where id.day=? order by id.time, id.place"); q.setDate(0, day); screenings = q.list(); for (FestivalScreening screening : screenings) { table.addCell(screening.getFilmTitle().getTitle()); table.addCell(screening.getId().getPlace().toString()); cell = new PdfPCell(new Phrase(screening.getId().getTime().toString())); if (screening.getPress() == 1) { cell.setBackgroundColor(SILVER); } table.addCell(cell); if (screening.getFilmTitle().getImdb().startsWith("?")) { table.addCell(""); } else { imdb = new Chunk("imdb"); imdb.setAnchor("http://imdb.com/title/tt" + screening.getFilmTitle().getImdb()); table.addCell(new Phrase(imdb)); } } document.add(table); } // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:classroom.filmfestival_c.Movies17.java
@SuppressWarnings("unchecked") protected static Element getTable(Query q, Date date) throws DocumentException { PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100f);//ww w . j ava 2 s . com PdfPCell cell = new PdfPCell(new Phrase(date.toString(), NORMALWHITE)); cell.setBackgroundColor(BLACK); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(cell); q.setDate(0, date); java.util.List<FestivalScreening> screenings = q.list(); for (FestivalScreening screening : screenings) { addScreening(table, screening); } return table; }
From source file:classroom.filmfestival_c.Movies17.java
protected static void addScreening(PdfPTable outer, FestivalScreening screening) throws DocumentException { FilmTitle movie = screening.getFilmTitle(); // we construct a 3 column table PdfPTable table = new PdfPTable(3); table.setWidths(WIDTHS);/*from ww w . j a v a 2s.c o m*/ // the first cell with the full title spans all the columns PdfPCell cell = new PdfPCell(); cell.addElement(fullTitle(screening)); cell.setColspan(3); cell.setBorder(PdfPCell.NO_BORDER); setColor(movie, cell); table.addCell(cell); cell = new PdfPCell(); cell.setBorder(PdfPCell.NO_BORDER); cell.setUseAscender(true); cell.setUseDescender(true); table.addCell(cell); cell = new PdfPCell(); cell.addElement(directors(movie)); cell.setBorder(PdfPCell.NO_BORDER); cell.setUseAscender(true); cell.setUseDescender(true); table.addCell(cell); cell = new PdfPCell(); cell.addElement(screenings(screening)); cell.setBorder(PdfPCell.NO_BORDER); cell.setUseAscender(true); cell.setUseDescender(true); table.addCell(cell); outer.addCell(table); }
From source file:classroom.filmfestival_c.Movies17.java
protected static Element fullTitle(FestivalScreening screening) throws DocumentException { FilmTitle movie = screening.getFilmTitle(); // a table with 3 cells PdfPTable table = new PdfPTable(3); table.setWidths(INNER);/*from ww w . j ava 2s .c o m*/ table.setWidthPercentage(100); // the title(s) Paragraph p = new Paragraph(); p.add(new Phrase(movie.getTitle(), BOLD)); p.setLeading(16); // maybe an alternative title if (movie.getATitle().trim().length() > 0) { p.add(new Phrase(" (" + movie.getATitle() + ")")); } PdfPCell cell = new PdfPCell(); cell.addElement(p); cell.setBorder(PdfPCell.NO_BORDER); cell.setUseAscender(true); cell.setUseDescender(true); table.addCell(cell); // eXploreZone? cell = new PdfPCell(); cell.setBorder(PdfPCell.NO_BORDER); if (isExploreZone(movie)) { cell.setBackgroundColor(WHITE); cell.setUseAscender(true); cell.setUseDescender(true); cell.addElement(new Paragraph("eXplore")); } table.addCell(cell); // Duration / shortfilm cell = new PdfPCell(); cell.setBorder(PdfPCell.NO_BORDER); cell.setBackgroundColor(WHITE); cell.setUseAscender(true); cell.setUseDescender(true); StringBuffer buf = new StringBuffer(); buf.append(movie.getDuration()); buf.append('\''); if (getExtra(screening) > 0) { buf.append(" + KF"); buf.append(getExtra(screening)); buf.append('\''); } p = new Paragraph(buf.toString()); p.setAlignment(Element.ALIGN_CENTER); cell.addElement(p); table.addCell(cell); return table; }
From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java
License:Open Source License
public static void convertHSSF2Pdf(Workbook wb, String header, File reportFile) throws Exception { assert wb != null; assert reportFile != null; //Precompute formula FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i);//from w w w . ja va 2 s . co m for (Row r : sheet) { for (Cell c : r) { if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { try { evaluator.evaluateFormulaCell(c); } catch (Exception e) { System.err.println(e); } } } } } File tmp = File.createTempFile("tmp_", ".xlsx"); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { wb.write(out); } //Find page orientation int maxColumnsGlobal = 0; for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); maxColumnsGlobal = Math.max(maxColumnsGlobal, row.getLastCellNum()); } } Rectangle pageSize = maxColumnsGlobal < 10 ? PageSize.A4 : PageSize.A4.rotate(); Document pdfDocument = new Document(pageSize, 10f, 10f, 20f, 20f); PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(reportFile)); addHeader(writer, header); pdfDocument.open(); //we have two columns in the Excel sheet, so we create a PDF table with two columns //Note: There are ways to make this dynamic in nature, if you want to. //Loop through sheets for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); //Loop through rows, to find number of columns int minColumns = 1000; int maxColumns = 0; for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); if (row.getFirstCellNum() >= 0) minColumns = Math.min(minColumns, row.getFirstCellNum()); if (row.getLastCellNum() >= 0) maxColumns = Math.max(maxColumns, row.getLastCellNum()); } if (maxColumns == 0) continue; //Loop through first rows, to find relative width float[] widths = new float[maxColumns]; int totalWidth = 0; for (int c = 0; c < maxColumns; c++) { int w = sheet.getColumnWidth(c); widths[c] = w; totalWidth += w; } for (int c = 0; c < maxColumns; c++) { widths[c] /= totalWidth; } //Create new page and a new chapter with the sheet's name if (sheetNo > 0) pdfDocument.newPage(); Chapter pdfSheet = new Chapter(sheet.getSheetName(), sheetNo + 1); PdfPTable pdfTable = null; PdfPCell pdfCell = null; boolean inTable = false; //Loop through cells, to create the content // boolean leftBorder = true; // boolean[] topBorder = new boolean[maxColumns+1]; for (int r = 0; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); //Check if we exited a table (empty line) if (row == null) { if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); pdfTable = null; } inTable = false; continue; } //Check if we start a table (>MIN_COL_IN_TABLE columns) if (row.getLastCellNum() >= MIN_COL_IN_TABLE) { inTable = true; } if (!inTable) { //Process the data outside table, just add the text boolean hasData = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; Chunk chunk = getChunk(wb, cell); pdfSheet.add(chunk); pdfSheet.add(new Chunk(" ")); hasData = true; } if (hasData) pdfSheet.add(Chunk.NEWLINE); } else { //Process the data in table if (pdfTable == null) { //Create table pdfTable = new PdfPTable(maxColumns); pdfTable.setWidths(widths); // topBorder = new boolean[maxColumns+1]; } int cellNumber = minColumns; // leftBorder = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); for (; cellNumber < cell.getColumnIndex(); cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } Chunk phrase = getChunk(wb, cell); pdfCell = new PdfPCell(new Phrase(phrase)); pdfCell.setFixedHeight(row.getHeightInPoints() - 3); pdfCell.setNoWrap(!cell.getCellStyle().getWrapText()); pdfCell.setPaddingLeft(1); pdfCell.setHorizontalAlignment( cell.getCellStyle().getAlignment() == CellStyle.ALIGN_CENTER ? PdfPCell.ALIGN_CENTER : cell.getCellStyle().getAlignment() == CellStyle.ALIGN_RIGHT ? PdfPCell.ALIGN_RIGHT : PdfPCell.ALIGN_LEFT); pdfCell.setUseBorderPadding(false); pdfCell.setUseVariableBorders(false); pdfCell.setBorderWidthRight(cell.getCellStyle().getBorderRight() == 0 ? 0 : .5f); pdfCell.setBorderWidthBottom(cell.getCellStyle().getBorderBottom() == 0 ? 0 : cell.getCellStyle().getBorderBottom() > 1 ? 1 : .5f); pdfCell.setBorderWidthLeft(cell.getCellStyle().getBorderLeft() == 0 ? 0 : cell.getCellStyle().getBorderLeft() > 1 ? 1 : .5f); pdfCell.setBorderWidthTop(cell.getCellStyle().getBorderTop() == 0 ? 0 : cell.getCellStyle().getBorderTop() > 1 ? 1 : .5f); String color = cell.getCellStyle().getFillForegroundColorColor() == null ? null : ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getARGBHex(); if (color != null) pdfCell.setBackgroundColor(new Color(Integer.decode("0x" + color.substring(2)))); pdfTable.addCell(pdfCell); cellNumber++; } for (; cellNumber < maxColumns; cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } } //Custom code to add all images on the first sheet (works for reporting) if (sheetNo == 0 && row.getRowNum() == 0) { for (PictureData pd : wb.getAllPictures()) { try { Image pdfImg = Image.getInstance(pd.getData()); pdfImg.scaleToFit( pageSize.getWidth() * .8f - pageSize.getBorderWidthLeft() - pageSize.getBorderWidthRight(), pageSize.getHeight() * .8f - pageSize.getBorderWidthTop() - pageSize.getBorderWidthBottom()); pdfSheet.add(pdfImg); } catch (Exception e) { e.printStackTrace(); } } } } if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); } pdfDocument.add(pdfSheet); } pdfDocument.close(); }
From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java
License:Open Source License
private static void addTable(Document pdfDocument, Chapter pdfSheet, int totalWidth, float[] widths, PdfPTable pdfTable) throws Exception { final int MAX_WIDTH = (int) (pdfDocument.getPageSize().getWidth() / 842 * 65000); // if(totalWidth>MAX_WIDTH) { //Split the tables in subcolumns for (int offset = 0; offset < pdfTable.getNumberOfColumns();) { if (offset > 0) pdfSheet.add(new Chunk(" ")); //Calculates the indexes that fit the page: [offset; index2] int subTotalWidth = 0; int index2; for (index2 = offset; index2 < pdfTable.getNumberOfColumns() && subTotalWidth < MAX_WIDTH; index2++) { subTotalWidth += widths[index2] * totalWidth; }// w w w . ja va 2s. c om index2 = Math.min(index2, pdfTable.getNumberOfColumns() - 1); //Calculates the new sub widths float[] subWidths = new float[index2 - offset + 2]; for (int i = 0; i < subWidths.length - 1; i++) { subWidths[i] = widths[offset + i] * totalWidth / MAX_WIDTH; } subWidths[subWidths.length - 1] = Math.max(0, (float) (MAX_WIDTH - subTotalWidth) / MAX_WIDTH); System.out.println("PDFUtils.addTable() " + Arrays.toString(subWidths)); //Creates the new sub widths PdfPTable subtable = new PdfPTable(subWidths.length); subtable.setWidths(subWidths); for (int r = 0; r < pdfTable.getRows().size(); r++) { for (int c = 0; c < index2 - offset + 1; c++) { PdfPCell cell = pdfTable.getRow(r).getCells()[c + offset]; subtable.addCell(cell); } PdfPCell pdfCell = new PdfPCell(); pdfCell.setBorder(0); subtable.addCell(pdfCell); } subtable.setWidthPercentage(100); collapseBorder(subtable); pdfSheet.add(subtable); //Go to the next index offset offset = index2 + 1; } // } else { // //Calculates the new sub widths // float[] subWidths = new float[widths.length+1]; // for (int i = 0; i < subWidths.length-1; i++) { // subWidths[i] = widths[0]*totalWidth/MAX_WIDTH; // } // subWidths[subWidths.length-1] = Math.max(0, (float)(MAX_WIDTH-totalWidth)/MAX_WIDTH); // pdfTable.setWidths(subWidths); // pdfTable.setWidthPercentage(100); // collapseBorder(pdfTable); // pdfSheet.add(pdfTable); // } }