List of usage examples for com.itextpdf.text BaseColor BLUE
BaseColor BLUE
To view the source code for com.itextpdf.text BaseColor BLUE.
Click Source Link
From source file:PDF.CrearPDF_Ficha.java
public static void drawRectangle(PdfContentByte content, float x, float y, float width, float height) { try {/* w w w . ja v a 2s .c o m*/ content.saveState(); PdfGState state = new PdfGState(); content.setGState(state); content.setRGBColorFill(232, 232, 232); content.setColorStroke(BaseColor.BLUE); content.setLineWidth((float) .5); content.rectangle(x, y, width, height); content.fillStroke(); content.restoreState(); BaseFont bf = BaseFont.createFont(); float fontSize = 15f; Phrase phrase = new Phrase("Foto", new Font(bf, fontSize)); ColumnText.showTextAligned(content, Element.ALIGN_CENTER, phrase, 475, 687, 0); } catch (DocumentException ex) { Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:pdf.PDFDesign.java
public PDFDesign(String s, String c, String sn, Map<String, String> fontMap) { size = s;//from w ww. ja v a 2 s . c om titleLineFixedHeight = 15.0f; priceFontInc = 0; textFontInc = 0; styleName = sn; fontPaths = new FontPaths(fontMap); nColumns = 6; switch (c) { case "black": color = BaseColor.BLACK; break; case "gray": color = BaseColor.DARK_GRAY; break; case "red": color = BaseColor.RED.darker(); break; case "green": color = BaseColor.GREEN.darker().darker().darker(); break; case "blue": color = BaseColor.BLUE.darker().darker().darker(); break; default: this.color = BaseColor.BLACK; } switch (size) { case "3x5": cellHeight = 83.8f; break; case "3.5x5": cellHeight = 97.8f; break; case "3.5x6": priceFontInc = 8; cellHeight = 97.8f; nColumns = 5; break; case "3.5x7": cellHeight = 97.8f; nColumns = 4; break; case "4x6": titleLineFixedHeight = 26.0f; priceFontInc = 8; cellHeight = 117.0f; nColumns = 5; break; case "4x7": titleLineFixedHeight = 26.0f; priceFontInc = 8; cellHeight = 117.0f; nColumns = 4; break; case "5x6": titleLineFixedHeight = 30.0f; priceFontInc = 13; textFontInc = 2; cellHeight = 145.0f; nColumns = 5; break; case "5x7": titleLineFixedHeight = 30.0f; priceFontInc = 18; textFontInc = 3; cellHeight = 145.0f; nColumns = 4; break; case "6x7": titleLineFixedHeight = 34.0f; priceFontInc = 26; textFontInc = 5; cellHeight = 175.0f; nColumns = 4; break; } try { int normal = Font.NORMAL; int bold = Font.BOLD; droidsans = BaseFont.createFont(fontPaths.getPath("droidsans"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); BaseFont impact = BaseFont.createFont(fontPaths.getPath("impact"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); switch (styleName) { case "style1": BaseFont courier = BaseFont.createFont(fontPaths.getPath("courier"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(42f, new Font(courier, 12 + textFontInc, normal, color), new Font(courier, 9 + textFontInc, normal, color), new Font(impact, 43 + priceFontInc + textFontInc, normal, color), new Font(courier, 20 + textFontInc, normal, color), new Font(courier, 8 + textFontInc, normal, color)); break; case "style2": BaseFont digital7 = BaseFont.createFont(fontPaths.getPath("digital7"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(42f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(digital7, 55 + priceFontInc + textFontInc, normal, color), new Font(digital7, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style3": BaseFont modern = BaseFont.createFont(fontPaths.getPath("modern"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(modern, 55 + priceFontInc + textFontInc, normal, color), new Font(modern, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style4": BaseFont gothic = BaseFont.createFont(fontPaths.getPath("gothic"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(gothic, 48 + priceFontInc + textFontInc, bold, color), new Font(gothic, 30, bold, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style5": BaseFont bookman = BaseFont.createFont(fontPaths.getPath("bookman"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(35f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(bookman, 45 + priceFontInc + textFontInc, bold, color), new Font(bookman, 25, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style6": initValues(45f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(impact, 45 + priceFontInc + textFontInc, normal, color), new Font(droidsans, 20 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; default: System.out.println("[*] Unknown Style"); break; } } catch (DocumentException | IOException ex) { System.out.println("[E] " + ex.getMessage()); } }
From source file:pidevhany.Controllers.GeneratePDF.java
public GeneratePDF(String ReponsesCorretes, String ReponsesFausses) throws FileNotFoundException, IOException { Document document = new Document(); OutputStream outputStream = new FileOutputStream( new File("D:\\Esprit\\Atelier Java\\piweb\\pidevHany\\src\\pidevhany\\TestFile.pdf")); try {/*from w w w .java 2 s. c om*/ PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); document.add(new Paragraph("Resultat du test :")); PdfPTable table = new PdfPTable(2); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = { 1f, 1f }; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph(ReponsesCorretes)); cell1.setBorderColor(BaseColor.BLUE); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph(ReponsesFausses)); cell2.setBorderColor(BaseColor.GREEN); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell1); table.addCell(cell2); document.add(table); document.close(); outputStream.close(); writer.close(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:pkgfinal.viewTable.java
private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed try {//from w w w. j av a2 s. com Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(" " + jComboBox1.getSelectedItem() + ".pdf")); document.open(); // Image image = Image.getInstance("save.png"); // document.add(new Paragraph("image")); // document.add(image); document.add(new Paragraph("ECHO TRADERS", FontFactory.getFont(FontFactory.TIMES_ROMAN, 18, Font.BOLD, BaseColor.RED))); document.add(new Paragraph(" " + jComboBox1.getSelectedItem() + " Report ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 18, Font.BOLD, BaseColor.BLUE))); document.add(new Paragraph(new java.util.Date().toString())); document.add(new Paragraph("--------------------------------------------------------")); PdfPTable table = new PdfPTable(7); PdfPCell cell = new PdfPCell(new Paragraph("Purcahse_Sale Report")); cell.setColspan(7); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); String sql = "select * from '" + jComboBox1.getSelectedItem() + "' "; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { String v1 = rs.getString("ID"); String add = rs.getString("Date"); String v2 = rs.getString("BrandName"); String v3 = rs.getString("ManufactureName"); String v4 = rs.getString("PurchaseQuantity"); String v5 = rs.getString("SaleQuantity"); String v6 = rs.getString("TotalQuantity"); // table.addCell("ID"); table.addCell(v1); // table.addCell(v/*.date_tf.getDateFormatString()*/); table.addCell(add); // table.addCell("Brand name"); table.addCell(v2); // table.addCell("Manyfacture Name"); table.addCell(v3); // table.addCell("purchase Quantity"); table.addCell(v4); table.addCell(v5); table.addCell(v6); } document.add(table); com.itextpdf.text.List list = new com.itextpdf.text.List(true, 20); list.add("Printed Date:_____________"); list.add("Signature:_______________"); document.add(list); document.close(); JOptionPane.showMessageDialog(null, "Saved Report"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }
From source file:proyectopdf.Pleno.java
public static void main(String a[]) throws FileNotFoundException, DocumentException, IOException { String ae;/* w w w .jav a 2 s . c o m*/ JFileChooser jfc = new JFileChooser(); jfc.showOpenDialog(null); BufferedReader archivo = new BufferedReader(new FileReader(jfc.getSelectedFile().getPath())); FileOutputStream nuevo = new FileOutputStream("hola.pdf"); Document documento = new Document(); PdfWriter.getInstance(documento, nuevo); documento.open(); String w; while (archivo.ready()) { ae = archivo.readLine(); StringTokenizer tituloI = new StringTokenizer(ae, "{T}{#T}{C}{#C}{P}{#P}{S}{#S}"); while (tituloI.hasMoreTokens()) { String a1 = tituloI.nextToken(); w = a1; documento.add(new Paragraph(a1, FontFactory.getFont("arial", // fuente 22, // tamao Font.ITALIC, // estilo BaseColor.BLUE))); } String a2 = ""; while (tituloI.hasMoreTokens()) { a2 = tituloI.nextToken(); documento.add(new Paragraph(a2, FontFactory.getFont("arial", // fuente 22, // tamao Font.ITALIC, // estilo BaseColor.RED))); } String a3 = ""; while (tituloI.hasMoreTokens()) { a3 = tituloI.nextToken(); documento.add(new Paragraph(a3, FontFactory.getFont("arial", // fuente 22, // tamao Font.ITALIC, // estilo BaseColor.CYAN))); } String a4 = ""; while (tituloI.hasMoreTokens()) { a4 = tituloI.nextToken(); documento.add(new Paragraph(a4, FontFactory.getFont("arial", // fuente 22, // tamao Font.ITALIC, // estilo BaseColor.YELLOW))); } System.out.println(archivo.readLine()); } documento.close(); }
From source file:servlet.GenerarPDF.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww.j a v a 2 s .c o 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 { response.setContentType("text/html;charset=UTF-8"); Document document = new Document(); try { ConexionDB sqlite = new ConexionDB(); java.sql.Connection cn = sqlite.Conectar(); Statement st = cn.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM mascotas;"); response.setContentType("APPLICATION/download"); response.setHeader("Content-Disposition", "filename=Mascotas.pdf"); PdfWriter.getInstance(document, response.getOutputStream()); document.open(); Image image = Image.getInstance( "C:/Users/Cristian/Documents/NetBeansProjects/adopc-mascotas/web/imagenes/logo9.png"); image.scaleAbsolute(100, 100); document.add(image); Paragraph preface = new Paragraph("LOVE MY PET"); preface.setAlignment(Element.ALIGN_CENTER); document.add(preface); PdfPTable table = new PdfPTable(5); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = { 1f, 1f, 1f, 1f, 1f }; table.setWidths(columnWidths); PdfPCell cellusuario = new PdfPCell(new Paragraph("usario")); cellusuario.setBorderColor(BaseColor.BLUE); cellusuario.setPaddingLeft(10); cellusuario.setHorizontalAlignment(Element.ALIGN_CENTER); cellusuario.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell celltipo = new PdfPCell(new Paragraph("tipo")); celltipo.setBorderColor(BaseColor.BLUE); celltipo.setPaddingLeft(10); celltipo.setHorizontalAlignment(Element.ALIGN_CENTER); celltipo.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cellnombre = new PdfPCell(new Paragraph("nombre")); cellnombre.setBorderColor(BaseColor.BLUE); cellnombre.setPaddingLeft(10); cellnombre.setHorizontalAlignment(Element.ALIGN_CENTER); cellnombre.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cellraza = new PdfPCell(new Paragraph("raza")); cellraza.setBorderColor(BaseColor.BLUE); cellraza.setPaddingLeft(10); cellraza.setHorizontalAlignment(Element.ALIGN_CENTER); cellraza.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell celledad = new PdfPCell(new Paragraph("edad")); celledad.setBorderColor(BaseColor.BLUE); celledad.setPaddingLeft(10); celledad.setHorizontalAlignment(Element.ALIGN_CENTER); celledad.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cellusuario); table.addCell(celltipo); table.addCell(cellnombre); table.addCell(cellraza); table.addCell(celledad); while (rs.next()) { PdfPCell cell1 = new PdfPCell(new Paragraph(rs.getString(1))); cell1.setPaddingLeft(1); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBackgroundColor(BaseColor.LIGHT_GRAY); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Paragraph(rs.getString(2))); cell2.setPaddingLeft(2); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setBackgroundColor(BaseColor.LIGHT_GRAY); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell2); PdfPCell cell3 = new PdfPCell(new Paragraph(rs.getString(3))); cell3.setPaddingLeft(3); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setBackgroundColor(BaseColor.LIGHT_GRAY); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell3); PdfPCell cell4 = new PdfPCell(new Paragraph(rs.getString(4))); cell4.setPaddingLeft(4); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setBackgroundColor(BaseColor.LIGHT_GRAY); cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell4); PdfPCell cell5 = new PdfPCell(new Paragraph(rs.getString(5))); cell5.setPaddingLeft(5); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setBackgroundColor(BaseColor.LIGHT_GRAY); cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell5); } document.add(table); //Add more content here cn.close(); } catch (Exception e) { e.printStackTrace(); } document.close(); }
From source file:servlet.SalesReportPDF.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.// www . j a v a 2s . c o 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 { try { Document document = new Document(); List<ArrayList> data = productSession.getEventList(); List<ArrayList> sessions = getAllProductDetailsLocal.getAllSessions(); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //get current date time with Date() Date date = new Date(); //System.out.println(dateFormat.format(date)); @SuppressWarnings("unused") PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/Yong Jing Ying/Desktop/EventRecords.pdf")); document.open(); document.add(new Paragraph("Management Report", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE))); document.add(new Paragraph("PDF created on " + dateFormat.format(date).toString() + "\n", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); document.add(new Paragraph( "------------------------------------------------------------------------------------", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLACK))); for (int i = 0; i < data.size(); i++) { document.add(new Paragraph(data.get(i).get(1).toString() + " Event Details ", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); document.add(new Paragraph("Event Type : " + data.get(i).get(4).toString() + "\nStart Date : " + data.get(i).get(2).toString() + "\nEnd Date : " + data.get(i).get(3).toString() + "\nProperty Name : " + data.get(i).get(5).toString() + "\nNo of Category : " + data.get(i).get(6).toString() + "\nPromotions : " + data.get(i).get(7).toString() + "\n\n")); document.add(new Paragraph("Session", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); PdfPTable table = new PdfPTable(6); //table.addCell("item1"); table.setSpacingBefore(5); table.setSpacingAfter(5); table.setWidths(new int[] { 1, 2, 3, 3, 3, 3 }); table.setWidthPercentage(100); PdfPCell cell; cell = new PdfPCell(new Phrase("No:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); cell = new PdfPCell(new Phrase("Name:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); cell = new PdfPCell(new Phrase("Description:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); cell = new PdfPCell(new Phrase("Start:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); cell = new PdfPCell(new Phrase("End:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); cell = new PdfPCell(new Phrase("TicketPrices:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.CYAN); table.addCell(cell); for (int j = 0; j < sessions.size(); j++) { if (sessions.get(j).get(1).toString().equals(data.get(i).get(4).toString()) && sessions.get(j).get(0).toString().equals(data.get(i).get(0).toString())) { cell = new PdfPCell(new Phrase(sessions.get(j).get(8).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(4).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(5).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(6).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(7).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(9).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); } } document.add(table); document.add(new Paragraph("\n")); document.add(new Paragraph("Alert", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); table = new PdfPTable(6); //table.addCell("item1"); table.setSpacingBefore(5); table.setSpacingAfter(5); table.setWidths(new int[] { 1, 2, 2, 3, 3, 3 }); table.setWidthPercentage(100); cell = new PdfPCell(new Phrase("No:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); cell = new PdfPCell(new Phrase("Alert Type:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); cell = new PdfPCell(new Phrase("Below Sales:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); cell = new PdfPCell(new Phrase("In-Charged Email:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); cell = new PdfPCell(new Phrase("Start:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); cell = new PdfPCell(new Phrase("End:", FontFactory.getFont(FontFactory.TIMES_BOLD, 12, Font.BOLD, BaseColor.BLACK))); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); for (int j = 0; j < sessions.size(); j++) { if (sessions.get(j).get(1).toString().equals(data.get(i).get(4).toString()) && sessions.get(j).get(0).toString().equals(data.get(i).get(0).toString()) && sessions.get(j).size() == 15) { cell = new PdfPCell(new Phrase(sessions.get(j).get(8).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(10).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(11).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(12).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(13).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); cell = new PdfPCell(new Phrase(sessions.get(j).get(14).toString())); cell.setBackgroundColor(BaseColor.WHITE); table.addCell(cell); } } document.add(table); document.add(new Paragraph("\n\n")); } document.close(); Thread.sleep(1000); PrintWriter out = response.getWriter(); String fileName = "EventRecords.pdf"; String filePath = "C:/Users/Yong Jing Ying/Desktop/"; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); FileInputStream fi = new FileInputStream("C:/Users/Yong Jing Ying/Desktop/EventRecords.pdf"); int i; while ((i = fi.read()) != -1) { out.write(i); } out.close(); fi.close(); } catch (Exception ex) { Logger.getLogger(SalesReportPDF.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:src.servlets.ManageAdmin.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request/*from w w w. j av a2 s.com*/ * @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); } } }