private ByteArrayOutputStream getPdfData(
     JSONArray data, JSONArray res, HttpServletRequest request) throws ServiceException {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try {
     String[] colHeader = getColHeader();
     String[] colIndex = getColIndexes();
     String[] val = getColValues(colHeader, data);
     String[] resources = getResourcesColHeader(res, data);
     String[] mainHeader = {"Dates", "Duration", "Work", "Cost", "Tasks", "Resources"};
     Document document = null;
     if (landscape) {
       Rectangle recPage = new Rectangle(PageSize.A4.rotate());
       recPage.setBackgroundColor(new java.awt.Color(255, 255, 255));
       document = new Document(recPage, 15, 15, 30, 30);
     } else {
       Rectangle recPage = new Rectangle(PageSize.A4);
       recPage.setBackgroundColor(new java.awt.Color(255, 255, 255));
       document = new Document(recPage, 15, 15, 30, 30);
     }
     PdfWriter writer = PdfWriter.getInstance(document, baos);
     writer.setPageEvent(new EndPage());
     document.open();
     if (showLogo) {
       getCompanyDetails(request);
       addComponyLogo(document, request);
     }
     addTitleSubtitle(document);
     addTable(data, resources, colIndex, colHeader, mainHeader, val, document);
     document.close();
     writer.close();
     baos.close();
   } catch (ConfigurationException ex) {
     throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
   } catch (DocumentException ex) {
     throw ServiceException.FAILURE("ExportProjectReport.getPdfData", ex);
   } catch (JSONException e) {
     throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
   } catch (IOException e) {
     throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
   } catch (Exception e) {
     throw ServiceException.FAILURE("ExportProjectReport.getPdfData", e);
   }
   return baos;
 }
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
      try {
        Rectangle page = document.getPageSize();

        getHeaderFooter(document);
        // Add page header
        header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        header.writeSelectedRows(
            0, -1, document.leftMargin(), page.getHeight() - 10, writer.getDirectContent());

        // Add page footer
        footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        footer.writeSelectedRows(
            0, -1, document.leftMargin(), document.bottomMargin() - 5, writer.getDirectContent());

        // Add page border

        int bmargin = 8; // border margin
        PdfContentByte cb = writer.getDirectContent();
        cb.rectangle(
            bmargin, bmargin, page.getWidth() - bmargin * 2, page.getHeight() - bmargin * 2);
        cb.setColorStroke(Color.LIGHT_GRAY);
        cb.stroke();

      } catch (JSONException e) {
        Logger.getLogger(ExportProjectReportServlet.class.getName()).log(Level.SEVERE, null, e);
        throw new ExceptionConverter(e);
      }
    }
コード例 #3
0
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    regService = new RegService();
    nuser = (RegForm) form;
    nuser = regService.authenticate(nuser);
    String uname = nuser.getUserName();
    String pwd = nuser.getFpassword();
    String cpwd = nuser.getCpassword();
    String acc = nuser.getAccno();
    String fn = nuser.getFirstName();
    String ln = nuser.getLastName();
    String sx = nuser.getSex();
    String add = nuser.getAddress();
    String co = nuser.getCountry();
    String zp = nuser.getZip();
    String mb = nuser.getMobile();
    String em = nuser.getEmail();

    RegForm regForm = (RegForm) form;
    Rectangle pageSize = new Rectangle(400, 400);
    pageSize.setBackgroundColor(new java.awt.Color(0xDF, 0xFF, 0xDE));
    Document document = new Document(pageSize);
    PdfWriter.getInstance(
        document, new FileOutputStream("D:/kowthal training/java/31rep/Report.pdf"));
    document.open();
    PdfPTable table = new PdfPTable(2);
    table.addCell("UserName");
    table.addCell(uname);
    table.addCell("Password");
    table.addCell(pwd);
    table.addCell("Confirm Password");
    table.addCell(cpwd);
    table.addCell("First Name");
    table.addCell(fn);
    table.addCell("Last Name");
    table.addCell(ln);
    table.addCell("Sex");
    table.addCell(sx);
    table.addCell("Address");
    table.addCell(add);
    table.addCell("Country");
    table.addCell(co);
    table.addCell("Zip Code");
    table.addCell(zp);
    table.addCell("Mobile");
    table.addCell(mb);
    table.addCell("E-mail id");
    table.addCell(em);
    System.out.println("before fetch");
    document.add(table);
    System.out.println("after fetch");
    document.add(new Paragraph("Created by Kowthal(947)"));
    document.close();

    target = "success";
    return mapping.findForward(target);
  }