Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package com.jwt.struts.action;
  2.  
  3. import com.itextpdf.text.BaseColor;
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.Element;
  6. import com.itextpdf.text.Paragraph;
  7. import com.itextpdf.text.pdf.PdfPCell;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;
  10. import com.jwt.struts.dao.UserRegisterDAO;
  11. import com.jwt.struts.form.UserQueryForm;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionForward;
  14. import org.apache.struts.action.ActionMapping;
  15.  
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.servlet.http.HttpSession;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.OutputStream;
  22. import java.sql.ResultSet;
  23.  
  24. public class GeneratePDFaction {
  25. public ActionForward execute(ActionMapping mapping, ActionForm form,
  26. HttpServletRequest request, HttpServletResponse response)
  27. throws Exception {
  28. HttpSession ses = request.getSession(true);
  29.  
  30. UserQueryForm registerForm = (UserQueryForm) form;
  31.  
  32. String userName = registerForm.getUserName();
  33.  
  34. UserRegisterDAO dao = new UserRegisterDAO();
  35.  
  36. ResultSet result = dao.consultarPorUsuario(userName);
  37.  
  38.  
  39. String firstName = result.getString(0);
  40. String lastName = result.getString(1);
  41. String uName = result.getString(2);
  42. String password = result.getString(3);
  43. String email = result.getString(4);
  44. String phone = result.getString(5);
  45.  
  46. OutputStream file = new FileOutputStream(new File("C:\\PDF_Java4s.pdf"));;;;;
  47. Document document = new Document();
  48. PdfWriter.getInstance(document, file);
  49.  
  50. PdfPTable table=new PdfPTable(3);
  51.  
  52. PdfPCell cell = new PdfPCell (new Paragraph("Java4s.com"));
  53.  
  54. cell.setColspan (3);
  55. cell.setHorizontalAlignment (Element.ALIGN_CENTER);
  56. cell.setPadding (10.0f);
  57. cell.setBackgroundColor (new BaseColor(140, 221, 8));
  58.  
  59. table.addCell(cell);
  60. table.addCell("nombre");
  61. table.addCell("apellidos");
  62. table.addCell("nombre de usuario");
  63. table.addCell("contraseña");
  64. table.addCell("email");
  65. table.addCell("telefono");
  66. table.setSpacingBefore(30.0f);
  67. table.setSpacingAfter(30.0f);
  68.  
  69. table.addCell(firstName);
  70. table.addCell(lastName);
  71. table.addCell(uName);
  72. table.addCell(password);
  73. table.addCell(email);
  74. table.addCell(phone);
  75.  
  76. document.add(table);
  77.  
  78. document.newPage();
  79.  
  80. document.close();
  81.  
  82. file.close();
  83.  
  84. System.out.println("Pdf created successfully..");
  85.  
  86. return mapping.findForward("success");
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement