List of usage examples for com.itextpdf.text.pdf PdfPCell setPaddingBottom
public void setPaddingBottom(float paddingBottom)
From source file:servlet.PdfGenerator.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w . j a va 2s . co m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Document report = new Document(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(report, baos); DBManager manager = (DBManager) getServletContext().getAttribute("dbmanager"); Group groupToReport = manager.getGroup(Integer.parseInt(request.getParameter("id"))); LinkedList<User> groupUsers = manager.getUsersForGroupAndVisible(groupToReport.getId()); Timestamp lastPosted = manager.getLatestPost(groupToReport); int numberOfPosts = manager.getGroupPosts(groupToReport).size(); String context = request.getServletContext().getRealPath("/"); Iterator<User> groupIterator = groupUsers.iterator(); report.open(); // INSERTING DOCUMENT CONTENT AREA Font title = FontFactory.getFont(FontFactory.HELVETICA_BOLD, 24); Font text = FontFactory.getFont(FontFactory.HELVETICA, 12); Paragraph documentHead = new Paragraph(groupToReport.getName(), title); Paragraph newLine = new Paragraph(Chunk.NEWLINE); Paragraph latestPost = new Paragraph("Latest post inserted on: " + lastPosted, text); Paragraph postsNumberToReport = new Paragraph("Number of posts: " + numberOfPosts, text); //LAYOUT AND FINAL PARAGRAPH EDITING AREA report.add(documentHead); report.add(newLine); report.add(postsNumberToReport); report.add(newLine); report.add(latestPost); report.add(newLine); PdfPTable usersTable = new PdfPTable(5); // LOOP FOR SETTING TABLE while (groupIterator.hasNext()) { User u = groupIterator.next(); String userName = u.getName(); PdfPCell avatarCell = new PdfPCell(Image.getInstance(context + u.getAvatar(request))); avatarCell.setBorder(0); avatarCell.setPaddingBottom(10); avatarCell.setColspan(1); PdfPCell userNameCell = new PdfPCell(new Phrase(userName)); userNameCell.setColspan(4); userNameCell.setBorder(0); userNameCell.setPaddingBottom(10); userNameCell.setPaddingLeft(10); usersTable.addCell(avatarCell); usersTable.addCell(userNameCell); } usersTable.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); report.add(usersTable); report.close(); // SETTING SOME RESPONSE HEADER response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); // SETTING THE CONTENT TYPE response.setContentType("application/pdf"); // THE CONTENTLENGHT response.setContentLength(baos.size()); System.out.println("ci sono"); try (OutputStream os = response.getOutputStream()) { baos.writeTo(os); os.flush(); os.close(); } catch (Exception e) { throw new IOException(e.getMessage()); } } catch (DocumentException e) { throw new IOException(e.getMessage()); } }
From source file:src.servlets.ManageAdmin.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request// w w w .ja v a 2 s . c om * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map m = request.getParameterMap(); if (m.containsKey("GetPDF")) { try { String Report = getServletContext().getRealPath("") + "admin\\PDF_Report.pdf"; FileOutputStream file = new FileOutputStream(Report); Document document = new Document(); document.addAuthor("K00140908"); PdfWriter.getInstance(document, file); ///////////////////////ADDING THE FILES TO PDF//////////////////// //Inserting Image in PDF String uploadPath = getServletContext().getRealPath("") + "images\\logo.gif"; Image img = Image.getInstance(uploadPath); img.scaleAbsolute(120f, 60f);// width,height of image in float // Inserting Title in PDF ORIGINAL // Font fontTitle=new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.WHITE); // Chunk title=new Chunk("PDF GENERATION in Java with iText", fontTitle); // title.setBackground(new BaseColor(255,102,0), 1f, 1f, 1f, 3f); // title.setLineHeight(30f); // title.setUnderline(BaseColor.BLACK,5f,0.5f,2f,0.5f,PdfContentByte.LINE_CAP_ROUND); Font fontTitle = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD, BaseColor.BLACK); Chunk title = new Chunk("Lit Realty System Report", fontTitle); title.setLineHeight(30f); //Inserting Table in PDF PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); // Sets the width percentage that the table will occupy in the page table.setSpacingAfter(10f); table.setSpacingBefore(15f); table.setWidths(new float[] { 2f, 2f, 2f }); // Sets relative width of table Font fontHeader = new Font(Font.FontFamily.HELVETICA, 15, Font.BOLD, BaseColor.BLUE); PdfPCell headercell = new PdfPCell(new Phrase("Property Photo", fontHeader)); // Creates new cell in table headercell.setBackgroundColor(new BaseColor(230, 230, 243)); headercell.setPaddingBottom(5f); table.addCell(headercell); headercell = new PdfPCell(new Phrase("Property ID", fontHeader)); headercell.setBackgroundColor(new BaseColor(233, 233, 233)); headercell.setPaddingBottom(5f); table.addCell(headercell); headercell = new PdfPCell(new Phrase("Price", fontHeader)); headercell.setBackgroundColor(new BaseColor(233, 233, 233)); headercell.setPaddingBottom(5f); table.addCell(headercell); PdfPCell cell1 = new PdfPCell(img, false); table.addCell(cell1); table.addCell("134000"); table.addCell("213445"); table.addCell("134000"); //Inserting List com.itextpdf.text.List list = new com.itextpdf.text.List(true, 30); list.add(new ListItem("Example1")); list.add(new ListItem("Example2")); list.add(new ListItem("Example3")); //Adding elements into PDF Document document.open(); document.add(img); document.add(title); document.add(Chunk.NEWLINE); document.add(table); document.newPage(); document.add(new Chunk("List of Examples").setUnderline(+1f, -5f)); document.add(list); document.newPage(); document.add(new Chunk("List of Examples").setUnderline(+1f, -5f)); document.add(list); document.newPage(); document.add(new Chunk("List of Properts By Agent X").setUnderline(+1f, -5f)); //////////////////////GET Propertys From Entity/////////////// List<Properties> allPropertiesList = PropertiesDB.getAllProperties(); PdfPTable propertyTable = new PdfPTable(3); PdfPCell propertyHeadingcell1 = new PdfPCell(new Phrase("Photo")); PdfPCell propertyHeadingcell2 = new PdfPCell(new Phrase("Property ID")); PdfPCell propertyHeadingcell3 = new PdfPCell(new Phrase("Price")); propertyHeadingcell1.setBorder(Rectangle.NO_BORDER); propertyHeadingcell2.setBorder(Rectangle.NO_BORDER); propertyHeadingcell3.setBorder(Rectangle.NO_BORDER); propertyTable.addCell(propertyHeadingcell1); propertyTable.addCell(propertyHeadingcell2); propertyTable.addCell(propertyHeadingcell3); document.add(Chunk.NEWLINE); String uploadPathforPropertyPhoto = getServletContext().getRealPath("") + "images\\properties\\thumbnails\\"; Image propertyThumbnail; img.scaleAbsolute(120f, 60f);// width,height of image in float for (Properties anProperty : allPropertiesList) { propertyThumbnail = Image.getInstance(uploadPathforPropertyPhoto + anProperty.getPhoto()); PdfPCell propertycell1 = new PdfPCell(propertyThumbnail, false); propertycell1.setPaddingBottom(20); PdfPCell propertycell2 = new PdfPCell(new Phrase(anProperty.getListingNum().toString())); PdfPCell propertycell3 = new PdfPCell(new Phrase(anProperty.getPrice().toString())); propertycell1.setBorder(Rectangle.NO_BORDER); propertycell2.setBorder(Rectangle.NO_BORDER); propertycell3.setBorder(Rectangle.NO_BORDER); propertyTable.addCell(propertycell1); propertyTable.addCell(propertycell2); propertyTable.addCell(propertycell3); } document.add(Chunk.NEWLINE); document.add(propertyTable); //////////////////////GET Propertys From Entity/////////////// document.close(); file.close(); System.out.println("Pdf created successfully ! :)"); String filePath = Report; File downloadFile = new File(filePath); FileInputStream inStream = new FileInputStream(downloadFile); // if you want to use a relative path to context root: String relativePath = getServletContext().getRealPath(""); System.out.println("relativePath = " + relativePath); // obtains ServletContext ServletContext context = getServletContext(); // gets MIME type of the file String mimeType = context.getMimeType(filePath); if (mimeType == null) { // set to binary type if MIME mapping not found mimeType = "application/octet-stream"; } System.out.println("MIME type: " + mimeType); // modifies response response.setContentType(mimeType); response.setContentLength((int) downloadFile.length()); // forces download String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); response.setHeader(headerKey, headerValue); // obtains response's output stream OutputStream outStream = response.getOutputStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inStream.close(); outStream.close(); ///////////////// processRequest(request, response); } catch (DocumentException ex) { Logger.getLogger(ManageAdmin.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:utilities.itext.Turnover.java
private static Paragraph createTablesWithoutDetails(HashMap<String, BigDecimal> expenseData) throws DocumentException { Paragraph paragraph = new Paragraph(); PdfPTable expense = new PdfPTable(2); float[] colWidths = { 1f, 3f }; expense.setWidths(colWidths);// w w w . ja v a2 s. co m for (String cat : expenseData.keySet()) { Paragraph contentCell1 = new Paragraph(cat); PdfPCell cell1 = new PdfPCell(contentCell1); Paragraph contentCell2 = new Paragraph(formatter.format(expenseData.get(cat))); PdfPCell cell2 = new PdfPCell(contentCell2); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(4f); cell2.setPaddingBottom(4f); expense.addCell(cell1); expense.addCell(cell2); } paragraph.add(expense); return paragraph; }