List of usage examples for com.itextpdf.text.pdf PdfPTable setWidthPercentage
public void setWidthPercentage(final float widthPercentage)
From source file:controller.CreateTranscript.java
private void CreateTranscript(MyPerson p) {//,PrintWriter out){ Document document = new Document(); String name = p.getFName() + p.getLName(); try {//from w w w . j av a 2 s . c o m if (p.getUserType() == 6) { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\" + name + ".pdf")); document.open(); PdfPTable table = new PdfPTable(8); // 8 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, 1f, 1f, 1f }; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph("Subject Name")); cell1.setBorderColor(BaseColor.BLUE); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph("Subject Code")); cell2.setBorderColor(BaseColor.BLUE); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell3 = new PdfPCell(new Paragraph("Written Grade")); cell3.setBorderColor(BaseColor.BLUE); cell3.setPaddingLeft(10); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell4 = new PdfPCell(new Paragraph("Midterm Grade")); cell4.setBorderColor(BaseColor.BLUE); cell4.setPaddingLeft(10); cell4.setHorizontalAlignment(Element.ALIGN_CENTER); cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell5 = new PdfPCell(new Paragraph("Final Exam Grade")); cell5.setBorderColor(BaseColor.BLUE); cell5.setPaddingLeft(10); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell6 = new PdfPCell(new Paragraph("Final Grade")); cell6.setBorderColor(BaseColor.BLUE); cell6.setPaddingLeft(10); cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell6.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell7 = new PdfPCell(new Paragraph("Term")); cell7.setBorderColor(BaseColor.BLUE); cell7.setPaddingLeft(10); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); cell7.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell8 = new PdfPCell(new Paragraph("Registration Date")); cell8.setBorderColor(BaseColor.BLUE); cell8.setPaddingLeft(10); cell8.setHorizontalAlignment(Element.ALIGN_CENTER); cell8.setVerticalAlignment(Element.ALIGN_MIDDLE); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); table.addCell(cell5); table.addCell(cell6); table.addCell(cell7); table.addCell(cell8); ModelOfStudent modelOfStudent = new ModelOfStudent(); ResultSet rs = modelOfStudent.ViewMyCourses(p.getCode()); // if(!rs.next()){out.println("<font color='blue'>There is no Courses Untill Now ^_^ </font>");} while (rs.next()) { table.addCell(rs.getString("SubjectName")); table.addCell(rs.getString("ID")); table.addCell(rs.getString("WritenGrade")); table.addCell(rs.getString("MidtermGrade")); table.addCell(rs.getString("FinalExamGrade")); table.addCell(rs.getString("FinalGrade")); table.addCell(rs.getString("Term")); table.addCell(rs.getString("StudentRegisterSubjectDate")); /* String SubjectRegisterDate=String.valueOf(rs.getDate("SubjectRegisterDate")); table.addCell(SubjectRegisterDate); */ } //To avoid having the cell border and the content overlap, if you are having thick cell borders //cell1.setUserBorderPadding(true); //cell2.setUserBorderPadding(true); //cell3.setUserBorderPadding(true); document.add(new Paragraph("University: " + MyPerson.ReturnUniversityName(p.getCode()))); document.add(new Paragraph("Faculity: " + MyPerson.ReturnFaculityName(p.getCode()))); document.add( new Paragraph("Student Name: " + p.getFName() + " " + p.getMName() + " " + p.getLName())); document.add(new Paragraph("Level: " + MyPerson.ReturnLevelName(p.getCode()))); document.add(new Paragraph("Department: " + MyPerson.ReturnDepartmentName(p.getCode()))); document.add(table); System.out.println( "<script type='text/javascript' > alert('Successfull Creating Transcript ^_^ ');history.back();</script>"); //out.println("Successfull Creating Transcript ^_^"); document.close(); writer.close(); } //end of if student } catch (Exception e) { System.out.println("<script type='text/javascript' > alert('Failed Creating Transcript ^_^ Error:" + e.getMessage() + " ');history.back();</script>"); // out.println(""+e.getMessage()); e.printStackTrace(); } }
From source file:controller.Personagem.java
public void GerarPDF(String NomeArq, int nivel) { Document doc = new Document(); if (!NomeArq.endsWith(".pdf")) { NomeArq += ".pdf"; }//ww w. j a v a 2s . co m Font f_Texto = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL); Font f_Legenda = new Font(Font.FontFamily.HELVETICA, 8, Font.NORMAL); try { PdfWriter.getInstance(doc, new FileOutputStream(NomeArq)); doc.open(); PdfPTable table = new PdfPTable(24); table.setWidthPercentage(110.0f); table.addCell(Cel(this.getNome(), 12, f_Texto)); table.addCell(Cel(" ", 12, f_Texto)); table.addCell(Cel("Nome do Personagem", 12, f_Legenda)); table.addCell(Cel("Nome do Jogador", 12, f_Legenda)); table.addCell(Cel(nivel + "", 12, f_Texto)); table.addCell(Cel(" ", 4, f_Texto)); table.addCell(Cel(this.getTendencia(), 4, f_Texto)); table.addCell(Cel("", 4, f_Texto)); table.addCell(Cel("Classe e Nvel", 12, f_Legenda)); table.addCell(Cel("Raa", 4, f_Legenda)); table.addCell(Cel("Tendncia", 4, f_Legenda)); table.addCell(Cel("Divindade", 4, f_Legenda)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel(" ", 3, f_Texto)); table.addCell(Cel("Tamanho", 3, f_Legenda)); table.addCell(Cel("Idade", 3, f_Legenda)); table.addCell(Cel("Sexo", 3, f_Legenda)); table.addCell(Cel("Altura", 3, f_Legenda)); table.addCell(Cel("Peso", 3, f_Legenda)); table.addCell(Cel("Olhos", 3, f_Legenda)); table.addCell(Cel("Cabelo", 3, f_Legenda)); table.addCell(Cel("Pele", 3, f_Legenda)); doc.add(table); } catch (DocumentException de) { de.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { doc.close(); } }
From source file:controller.PlanController.java
public Document PrintPressed(String Path) throws DocumentException, IOException { // step 1/* w ww . ja v a 2s . co m*/ Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(Path)); // step 3 document.open(); // step 4 PdfPTable tablaDatosPlan = new PdfPTable(2); tablaDatosPlan.setWidthPercentage(100); if (!Team.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Team") + ":" + Team.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Season.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Season") + ":" + Season.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Date.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Date") + ":" + Date.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Players.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Players") + ":" + Players.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Start.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Start") + ":" + Start.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!End.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("End") + ":" + End.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Place.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Place") + ":" + Place.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } if (!Equipment.getText().isEmpty()) { PdfPCell cell1 = new PdfPCell(new Phrase(messages.getString("Equipment") + ":" + Equipment.getText())); cell1.setBorderColor(BaseColor.WHITE); tablaDatosPlan.addCell(cell1); } document.add(tablaDatosPlan); document.add(new Phrase("\n")); document.add(new Phrase("\n")); PdfPTable tablaEjercicios = new PdfPTable(4); tablaEjercicios.setHeaderRows(contadorlineas); tablaEjercicios.addCell(messages.getString("Start")); tablaEjercicios.addCell(messages.getString("End")); tablaEjercicios.addCell(messages.getString("Drill")); tablaEjercicios.addCell(messages.getString("Notes")); int cont = 0; for (int aw = 4; aw < contadorlineas + 4; aw++) { tablaEjercicios.addCell(starColumn.getCellData(cont)); tablaEjercicios.addCell(endColumn.getCellData(cont)); tablaEjercicios.addCell(drillColumn.getCellData(cont)); tablaEjercicios.addCell(notesColumn.getCellData(cont)); cont++; } document.add(tablaEjercicios); int contadorListado = 0, contadorRecorrer = 0; if (checkboxDrills.isSelected()) { while (contadorListado < planData.size()) { contadorRecorrer = 0; while (contadorRecorrer < playData.size()) { System.out.println("Busco el ejercicio " + drillColumn.getCellData(contadorListado)); if (drillColumn.getCellData(contadorListado) .contains(playData.get(contadorRecorrer).getNombreJugada())) { int contador = 1; System.out.println("Ejercicio encontrado"); document.add(new Paragraph( messages.getString("Drill") + " " + drillColumn.getCellData(contadorListado))); while (contador < playData.get(contadorRecorrer).getContadorJugada() + 1) { document.add(new Paragraph(messages.getString("Diagram") + " " + contador)); document.add(new Paragraph(" ")); Image image2 = Image.getInstance(playData.get(contadorRecorrer).getNombreJugada() + "-" + playData.get(contadorRecorrer).getTipoJugada() + "-" + playData.get(contadorRecorrer).getSubtipoJugada() + "_" + contador); image2.scalePercent(50); PdfPTable table1 = new PdfPTable(2); table1.addCell(image2); table1.addCell( textPdf.textPdf(contador, playData.get(contadorRecorrer).getDescripcionJugada(), playData.get(contadorRecorrer).getContadorJugada())); document.add(table1); contador++; } } contadorRecorrer++; } contadorListado++; } } // step 5 document.close(); return document; }
From source file:Controller.PrintOrderManagedBean.java
public void executePDF(String maDH) { try {/*from w w w . j av a 2s . c o m*/ DonHang donhang = new DonHang(); donhang.init(maDH); float CONVERT = 28.346457f;// 1 cm FacesContext faces = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse(); // setting some response headers response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); //response.setHeader("Content-disposition","inline; filename=kiran.pdf"); response.setHeader("Pragma", "public"); response.setContentType("application/pdf"); //response.setHeader("Content-Disposition", "attachment;filename=\"ContactList.pdf\""); response.addHeader("Content-disposition", "attachment;filename=\"DataListBean.pdf\""); //step 1: creation of a document-object Document document = new Document(PageSize.A4, 0.5f * CONVERT, 0.5f * CONVERT, 1.0f * CONVERT, 1.0f * CONVERT); //step 2: we create a writer that listens to the document // and directs a PDF-stream to a temporary buffer ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); PdfWriter writer = PdfWriter.getInstance(document, baos); BaseFont bf = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(bf, 18, Font.BOLD); Font font11 = new Font(bf, 11, Font.NORMAL); Font font11_bo = new Font(bf, 11, Font.BOLD); Font font12 = new Font(bf, 12, Font.NORMAL); Font font10 = new Font(bf, 10, Font.NORMAL); Font font9 = new Font(bf, 9, Font.NORMAL); //step 3: we open the document document.open(); PdfPTable tab_Header1; tab_Header1 = new PdfPTable(1); tab_Header1.setWidthPercentage(100); tab_Header1.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell1; cell1 = new PdfPCell(new Phrase("CNG TY TNHH ABC FASHION", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell( new Phrase("?a ch: 146 Linh Trung,P. Linh Trung, Q. Th ?c, TP HCM", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S?T: 0909465621", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?N GIAO HNG", font12)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Bn:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn: CNG TY TNHH ABC FASHION", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell( new Phrase("?a ch: 146 Linh Trung,P. Linh Trung, Q. Th ?c, TP HCM", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi: 0909465621", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Mua:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn: " + donhang.getTenKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?a ch: " + donhang.getDiaChiKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi: " + donhang.getSoDTKH(), font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Bn Vn chuyn:", font11_bo)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tn:....................................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("?a ch:...............................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S in thoi:...............................", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Danh sch hng ha:", font11)); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); ///////////////////////////////bn sn phm float[] crDonHang = { 1.0f * CONVERT, 4.0f * CONVERT, 1.0f * CONVERT, 2.0f * CONVERT, 2.0f * CONVERT }; PdfPTable tab_Header2; tab_Header2 = new PdfPTable(crDonHang.length); tab_Header2.setWidthPercentage(100); tab_Header2.setWidths(crDonHang); tab_Header2.setHorizontalAlignment(Element.ALIGN_CENTER); NumberFormat formatter = new DecimalFormat("#,###,###"); String[] crheader = { "STT", "Tn Hng", "S Lng", "Gi Bn(VN?)", "Thnh Ti?n(VN?)" }; for (int i = 0; i < crheader.length; i++) { PdfPCell cell = new PdfPCell(new Phrase(crheader[i], font11)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header2.addCell(cell); } int stt = 1; for (SanPhamDH sp : donhang.getListSP()) { PdfPCell cell = new PdfPCell(new Phrase(String.valueOf(stt), font11)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(sp.getTenSP(), font11)); cell.setHorizontalAlignment(Element.ALIGN_LEFT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(sp.getSoluong(), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(formatter.format(Double.parseDouble(sp.getGiaSP())), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); cell = new PdfPCell(new Phrase(formatter.format(Double.parseDouble(sp.getThanhTien())), font11)); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); tab_Header2.addCell(cell); stt++; } cell1 = new PdfPCell(tab_Header2); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n hng: " + formatter.format( Double.parseDouble(donhang.getTienTamTinh() == null ? "0" : donhang.getTienTamTinh())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n vn chuyn: " + formatter.format(Double .parseDouble(donhang.getTienVanChuyen() == null ? "0" : donhang.getTienVanChuyen())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("Tng ti?n thanh ton: " + formatter.format(Double.parseDouble(donhang.getTongTien())) + " VN?", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("S ti?n thanh ton bng ch: " + DocTien.doctien(donhang.getTongTien().replaceAll(" ", "")) + "ng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase(" ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase("TP H Ch Minh, ngy thng nm ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_RIGHT); cell1.setPaddingRight(1.0f * CONVERT); cell1.setBorder(0); tab_Header1.addCell(cell1); cell1 = new PdfPCell(new Phrase(" ", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header1.addCell(cell1); PdfPTable tab_Header3; tab_Header3 = new PdfPTable(3); tab_Header3.setWidthPercentage(100); tab_Header3.setHorizontalAlignment(Element.ALIGN_CENTER); cell1 = new PdfPCell(new Phrase("Ng?i nhn hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("Ng?i giao hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("Ng?i bn hng", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(new Phrase("(k v ghi r h? tn)", font11)); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setPaddingBottom(5.0f); cell1.setBorder(0); tab_Header3.addCell(cell1); cell1 = new PdfPCell(tab_Header3); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setBorder(0); tab_Header1.addCell(cell1); document.add(tab_Header1); document.close(); //step 6: we output the writer as bytes to the response output // the contentlength is needed for MSIE!!! response.setContentLength(baos.size()); // write ByteArrayOutputStream to the ServletOutputStream ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); baos.flush(); faces.responseComplete(); } catch (Exception e) { e.printStackTrace(); } }
From source file:coreservlets.reportPDF.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w . j a va2 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"); try (PrintWriter out = response.getWriter()) { response.setContentType("application/pdf"); /** create Object Of document */ Document document = new Document(PageSize.FLSE); /** set margin of page */ document.setMargins(1.25F, 0.75F, 5F, 0.75F); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter pdfWriter = PdfWriter.getInstance(document, baos); document.open(); /** Table For header address */ PdfPTable headerTable = new PdfPTable(3); headerTable.setWidthPercentage(91); /** Add company Address*/ /** First Cell For address*/ PdfPCell headerCell = new PdfPCell(); headerCell.addElement(new Paragraph("My comapany")); /** add cell to headerTable*/ headerTable.addCell(headerCell); /** Second cell For CompanyLogo */ headerCell = new PdfPCell(); /** get context object */ ServletContext context = request.getSession().getServletContext(); /** get Real Path of image */ String path = context.getRealPath(login.getPath()); /** set image to cell*/ Image img = Image.getInstance(path); headerCell.setImage(img); /** add cell to headerTable*/ headerTable.addCell(headerCell); /** Third cell for lable of document **/ headerCell = new PdfPCell(); String allLedger = "ALL LEDGER"; headerCell.addElement(new Paragraph( allLedger + "\n" + "From Date:" + session.getAttribute("firstDate") + "\nTo Date:" + session.getAttribute("lastDate"), new Font(Font.TIMES_ROMAN, 10f, Font.BOLD, Color.black))); /** add cell to headerTable*/ headerTable.addCell(headerCell); /** add table to document */ document.add(headerTable); document.close(); /** Don't know why??*/ response.setContentLength(baos.size()); /** comment it if u don't want to download pdf*/ //response.addHeader("Content-Disposition", "attachment; filename=\"ledgers.pdf\""); /** get out put Stream*/ out = response.getOutputStream(); /** write to--- Don't know*/ baos.writeTo(out); /** Clear OutputStream*/ out.flush(); out.close(); } }
From source file:dbedit.actions.ExportPdfAction.java
License:Open Source License
@Override protected void performThreaded(ActionEvent e) throws Exception { boolean selection = false; JTable table = ResultSetTable.getInstance(); if (table.getSelectedRowCount() > 0 && table.getSelectedRowCount() != table.getRowCount()) { Object option = Dialog.show("PDF", "Export", Dialog.QUESTION_MESSAGE, new Object[] { "Everything", "Selection" }, "Everything"); if (option == null || "-1".equals(option.toString())) { return; }//from w w w .ja va 2s.com selection = "Selection".equals(option); } List list = ((DefaultTableModel) table.getModel()).getDataVector(); int columnCount = table.getColumnCount(); PdfPTable pdfPTable = new PdfPTable(columnCount); pdfPTable.setWidthPercentage(100); pdfPTable.getDefaultCell().setPaddingBottom(4); int[] widths = new int[columnCount]; // Row Header pdfPTable.getDefaultCell().setBorderWidth(2); for (int i = 0; i < columnCount; i++) { String columnName = table.getColumnName(i); pdfPTable.addCell(new Phrase(columnName, ROW_HEADER_FONT)); widths[i] = Math.min(50000, Math.max(widths[i], ROW_HEADER_BASE_FONT.getWidth(columnName + " "))); } pdfPTable.getDefaultCell().setBorderWidth(1); if (!list.isEmpty()) { pdfPTable.setHeaderRows(1); } // Body for (int i = 0; i < list.size(); i++) { if (!selection || table.isRowSelected(i)) { List record = (List) list.get(i); for (int j = 0; j < record.size(); j++) { Object o = record.get(j); if (o != null) { if (ResultSetTable.isLob(j)) { o = Context.getInstance().getColumnTypeNames()[j]; } } else { o = ""; } PdfPCell cell = new PdfPCell(new Phrase(o.toString())); cell.setPaddingBottom(4); if (o instanceof Number) { cell.setHorizontalAlignment(Element.ALIGN_RIGHT); } pdfPTable.addCell(cell); widths[j] = Math.min(50000, Math.max(widths[j], BASE_FONT.getWidth(o.toString()))); } } } // Size pdfPTable.setWidths(widths); int totalWidth = 0; for (int width : widths) { totalWidth += width; } Rectangle pageSize = PageSize.A4.rotate(); pageSize.setRight(pageSize.getRight() * Math.max(1f, totalWidth / 53000f)); pageSize.setTop(pageSize.getTop() * Math.max(1f, totalWidth / 53000f)); // Document Document document = new Document(pageSize); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, byteArrayOutputStream); document.open(); pdfTemplate = writer.getDirectContent().createTemplate(100, 100); pdfTemplate.setBoundingBox(new Rectangle(-20, -20, 100, 100)); writer.setPageEvent(this); document.add(pdfPTable); document.close(); FileIO.saveAndOpenFile("export.pdf", byteArrayOutputStream.toByteArray()); }
From source file:de.extra.xtt.util.pdf.PdfCreatorImpl.java
License:Apache License
/** * Erzeugt das Inhaltsverzeichnis aus den bereits vorhandenen Elementen in * der Liste <code>listEntries</code>. * //from ww w .j a v a2 s.c o m * @param docPdf * Zieldokument, falls Inhaltsverzeichnis nicht temporr erzeugt * wird * @param temp * Gibt an, ob das Inhaltsverzeichnis temporr in einer neuen * Datei/Dokument erzeugt werden soll * @return Anzahl der Seiten * @throws DocumentException * @throws IOException */ private int erzeugeInhaltsverzeichnis(Document docPdf, boolean temp) throws DocumentException, IOException { int anzPages = 0; Document docInhalt = docPdf; String filePathTempInhaltString = ""; if (temp) { // temp. Dateinamen bestimmen File fileDokuFile = new File(dateiname); filePathTempInhaltString = fileDokuFile.getParent() + "/tmp_inhalt.pdf"; // Neues Dokument erzeugen docInhalt = initPdfWriterAndDocument(filePathTempInhaltString, false); } // berschrift Chapter currChapter = new Chapter(getParagraphChapter("Inhaltsverzeichnis"), 0); // 0, damit keine Nummerierung currChapter.setNumberDepth(0); docInhalt.add(currChapter); // eine Zeile Abstand docInhalt.add(getEmptyLineTextHalf()); for (ContPdfEntry currEntry : listEntries) { // Eintrag erzeugen inkl. Abstand String strEintrag = currEntry.getBezeichnung() + " "; Chunk chunkBezeichnung; Chunk chunkSeitenzahlChunk; if (currEntry.getParentEntry() == null) { // 1. Ebene => fett, Abstand davor einfgen docInhalt.add(getEmptyLineTextHalf()); chunkBezeichnung = getChunkTextBold(strEintrag); chunkSeitenzahlChunk = getChunkTextBold("" + currEntry.getPageNumber()); } else { // 2. Ebene chunkBezeichnung = getChunkText(strEintrag); chunkSeitenzahlChunk = getChunkText("" + currEntry.getPageNumber()); } // Referenz setzen chunkBezeichnung.setLocalGoto(currEntry.getDestination()); chunkSeitenzahlChunk.setLocalGoto(currEntry.getDestination()); // Abstandzeichen generieren, Breite auffllen float widthAbstand = docInhalt.getPageSize().getWidth() * 0.81f; ; while (chunkBezeichnung.getWidthPoint() <= widthAbstand) { chunkBezeichnung.append("."); } // Tabelle erzeugen und formatieren PdfPTable currTable = new PdfPTable(2); currTable.setWidthPercentage(100f); currTable.setWidths(new int[] { 96, 4 }); // Inhalte einfgen // Zelle Bezeichnung PdfPCell currCellBezeichnung = new PdfPCell(new Phrase(chunkBezeichnung)); currCellBezeichnung.setBorder(0); currCellBezeichnung.setHorizontalAlignment(Element.ALIGN_JUSTIFIED_ALL); // Zelle Seitennummer PdfPCell currCellPageNumberCell = new PdfPCell(new Phrase(chunkSeitenzahlChunk)); currCellPageNumberCell.setBorder(0); currCellPageNumberCell.setHorizontalAlignment(Element.ALIGN_RIGHT); // Zellen zur Tabelle hinzufgen currTable.addCell(currCellBezeichnung); currTable.addCell(currCellPageNumberCell); docInhalt.add(currTable); } if (temp) { // Dokument schlieen docInhalt.close(); // Anzahl der Seitenzahlen bestimmen PdfReader reader = new PdfReader(filePathTempInhaltString); anzPages = reader.getNumberOfPages(); reader.close(); // temp. Datei lschen File currFileInhaltFile = new File(filePathTempInhaltString); currFileInhaltFile.delete(); } return anzPages; }
From source file:de.fau.osr.util.matrix.MatrixTools.java
License:Open Source License
/** * /**//from w w w. ja v a 2 s.c o m * saves matrix to {@code path} in pdf format * @param matrix matrix to save * @param path file where matrix will be saved */ public static boolean SaveMatrixToPdf(RequirementsTraceabilityMatrixByImpact matrix, File path, String matrixTitle) { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); document.addTitle(matrixTitle); Paragraph headerPar = new Paragraph("Traceability Matrix"); headerPar.add(new Paragraph(" ")); //new line document.add(headerPar); java.util.List<String> reqs = matrix.getRequirements(); //table setup PdfPTable table = new PdfPTable(reqs.size() + 1); //reqs + "file name" col table.setHeaderRows(1); table.setHorizontalAlignment(Element.ALIGN_JUSTIFIED); table.setWidthPercentage(100); table.setSpacingBefore(0); table.setSpacingAfter(0); //file name col width/req col width 4:1 float[] widths = new float[reqs.size() + 1]; widths[0] = 4; for (int i = 0; i < reqs.size(); i++) { widths[i + 1] = 1; } table.setWidths(widths); //header row //add filename col to header addCell(table, "File name", 10, Element.ALIGN_LEFT); //add req cols to header for (String req : reqs) { addCell(table, "req-" + req, 8, Element.ALIGN_CENTER); } //content, row by row: for (int i = 0; i < matrix.getFiles().size(); i++) { //filename col String fileName = matrix.getFiles().get(i); addCell(table, fileName, 5, Element.ALIGN_LEFT, 18); //impact value cols for (String req : reqs) { RequirementFileImpactValue value = matrix .getImpactValue(new RequirementFilePair(req, fileName)); if (value == null) { value = new RequirementFileImpactValue(0); } //round DecimalFormat df = new DecimalFormat("#.##"); df.setRoundingMode(RoundingMode.CEILING); String valString = df.format(value.getImpactPercentage()); addCell(table, valString, 10, Element.ALIGN_CENTER); } } document.add(table); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); return false; } finally { document.close(); } return true; }
From source file:de.tuttas.servlets.DokuServlet.java
private Document createUmfrageauswertung(List<UmfrageResult> res1, List<UmfrageResult> res2, int idUmfrage1, int idUmfrage2, String filter1, String filter2, String kopf, OutputStream out) throws DocumentException, BadElementException, IOException { Document document = new Document(); /* Basic PDF Creation inside servlet */ Umfrage u1 = em.find(Umfrage.class, idUmfrage1); Umfrage u2 = em.find(Umfrage.class, idUmfrage2); // Bild einfgen String url = "http://www.mmbbs.de/fileadmin/template/mmbbs/gfx/mmbbs_logo_druck.gif"; Image image = Image.getInstance(url); image.setAbsolutePosition(45f, 720f); image.scalePercent(50f);//from www . j a v a 2s . c o m StringBuilder htmlString = new StringBuilder(); htmlString.append(kopf); htmlString.append("<br></br>"); int maxRows = res1.size(); if (res2.size() > maxRows) { maxRows = res2.size(); } PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); writer.setPageEmpty(false); Font boldFont = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD); Font normalFont = new Font(Font.FontFamily.HELVETICA, 10, Font.ITALIC); PdfPTable table = new PdfPTable(new float[] { 1, 2, 2 }); PdfPCell qestionCell; PdfPCell group1Cell; PdfPCell group2Cell; int i = 0; for (i = 0; i < maxRows; i++) { Log.d("Print Row " + i); if (i % 5 == 0) { if (i != 0) { document.add(table); document.newPage(); document = printHead(writer, document, htmlString, out, image); } else { document = printHead(writer, document, htmlString, out, image); } table = new PdfPTable(new float[] { 1, 2, 2 }); table.setWidthPercentage((float) 100.0); qestionCell = new PdfPCell(new Phrase("\nFragen", boldFont)); group1Cell = new PdfPCell(); group1Cell.addElement(new Phrase("Hauptgruppe:", boldFont)); group1Cell.addElement(new Phrase(u1.getNAME() + "\n" + filter1, normalFont)); group2Cell = new PdfPCell(); group2Cell.addElement(new Phrase("Vergleichsgruppe:", boldFont)); group2Cell.addElement(new Phrase(u2.getNAME() + "\n" + filter2, normalFont)); qestionCell.setBorderWidth(2.0f); group1Cell.setBorderWidth(2.0f); group2Cell.setBorderWidth(2.0f); table.addCell(qestionCell); table.addCell(group1Cell); table.addCell(group2Cell); } String url1 = UmfrageUtil.getCharUrl(res1.get(i)); Log.d("URL1=" + url1); Image image1 = Image.getInstance(url1); Image image2 = null; if (res2.size() > i) { String url2 = UmfrageUtil.getCharUrl(res2.get(i)); Log.d("URL2=" + url2); image2 = Image.getInstance(url2); } Log.d("Write to pdf:" + res1.get(i).getFrage()); qestionCell = new PdfPCell(new Phrase(res1.get(i).getFrage(), normalFont)); qestionCell.setBorderWidth(1.0f); group1Cell = new PdfPCell(image1, true); group1Cell.setBorderWidth(1.0f); group1Cell.setPadding(10); if (image2 != null) group2Cell = new PdfPCell(image2, true); else group2Cell = new PdfPCell(); group2Cell.setBorderWidth(1.0f); group2Cell.setPadding(10); table.addCell(qestionCell); table.addCell(group1Cell); table.addCell(group2Cell); } if (!(i % 5 == 0)) { document.add(table); } document.close(); return document; }
From source file:Documentos.ReportesPDF.java
public void agregarTabla(Paragraph parrafo) { //Anchos de las columnas PdfPTable tabla = new PdfPTable(anchosFilas); // Porcentaje que ocupa a lo ancho de la pagina del PDF tabla.setWidthPercentage(90); //Alineacion horizontal centrada tabla.setHorizontalAlignment(Element.ALIGN_CENTER); //agregar celda que ocupa las columnas de los rotulos PdfPCell cell0 = new PdfPCell(new Paragraph("Repostera AnaIs")); cell0.setColspan(anchosFilas.length); cell0.setBackgroundColor(new BaseColor(66, 139, 202)); //Centrar contenido de celda cell0.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPCell cell = new PdfPCell(new Paragraph(tituloReporte())); cell.setColspan(anchosFilas.length); cell.setBackgroundColor(new BaseColor(49, 176, 213)); //Centrar contenido de celda cell.setHorizontalAlignment(Element.ALIGN_CENTER); //Color de fondo de la celda tabla.addCell(cell0);/* w w w .ja va2s .co m*/ tabla.addCell(cell); // Mostrar los rotulos de las columnas for (int i = 0; i < rotulosColumnas.length; i++) { cell = new PdfPCell(new Paragraph(rotulosColumnas[i])); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(new BaseColor(248, 248, 248)); tabla.addCell(cell); } switch (numReport) { case 1: DataBase.DataBasePDF.pdfVendedores(cell, tabla); break; case 2: DataBase.DataBasePDF.pdfProveedores(cell, tabla); break; case 3: DataBase.DataBasePDF.pdfClientes(cell, tabla); break; case 4: DataBase.DataBasePDF.pdfVentas(cell, tabla); break; case 5: DataBase.DataBasePDF.pdfProductos(cell, tabla); break; case 6: DataBase.DataBasePDF.pdfPedidos(cell, tabla); break; } //Agregar la tabla con los datos al parrafo que nos llego como entrada parrafo.add(tabla); }