private void generateHeaderTable(User c, PdfPTable headerTable) throws Exception {
    // Generic User Image
    // PdfPTable headerTable = new PdfPTable(3);
    Image userImage = Image.getInstance(c.getImageUrl(), null);
    userImage.setAlignment(Element.ALIGN_RIGHT);
    userImage.scalePercent(40);

    PdfPCell userImageCell = new PdfPCell(userImage, false);
    userImageCell.setBorder(0);
    headerTable.addCell(userImageCell);

    // Generic logo
    Image logoImage = Image.getInstance(c.getLogoUrl());
    logoImage.setAlignment(Element.ALIGN_RIGHT);
    logoImage.scalePercent(30);

    PdfPCell logoImageCell = new PdfPCell(logoImage, false);
    logoImageCell.setBorder(0);
    headerTable.addCell(logoImageCell);

    // Generic Stamp Image
    Image stampImage = Image.getInstance(c.getStampUrl());
    stampImage.setAlignment(Element.ALIGN_RIGHT);
    stampImage.scalePercent(30);
    PdfPCell stampImageCell = new PdfPCell(stampImage, false);
    stampImageCell.setBorder(0);
    headerTable.addCell(stampImageCell);
    // cell = PDFCellStyles.borderlessCell("Expense Form");
    // headerTable.addCell(cell);
    if (c.getName() != null && c.getName() != "") {
      headerTable.addCell(PDFCellStyles.borderlessCell(""));
      headerTable.addCell(PDFCellStyles.borderlessCell("User Name"));
      headerTable.addCell(PDFCellStyles.borderlessCell(c.getName()));
    }
    if (c.getAddress() != null && c.getAddress() != "") {
      headerTable.addCell(PDFCellStyles.borderlessCell(""));
      headerTable.addCell(PDFCellStyles.borderlessCell("Company"));
      headerTable.addCell(PDFCellStyles.borderlessCell(c.getAddress()));
    }
    if (c.getPhone() != null && c.getPhone() != "") {
      headerTable.addCell(PDFCellStyles.borderlessCell(""));
      headerTable.addCell(PDFCellStyles.borderlessCell("Phone Number"));
      headerTable.addCell(PDFCellStyles.borderlessCell(c.getPhone()));
    }
    // return headerTable;
  }
示例#2
0
  @Override
  public void onEndPage(final PdfWriter writer, final Document document) {
    try {
      /* Adicionado o header */
      final Rectangle page = document.getPageSize();
      final PdfPTable header = new PdfPTable(1);
      header.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
      header.setLockedWidth(true);
      header.getDefaultCell().setFixedHeight(55);
      header.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);

      final PdfPCell cellC = new PdfPCell();
      cellC.setBorder(1);

      final PdfPTable tableContent = new PdfPTable(3);

      /* Adicionando a LogoMarca */
      URL url = null;
      String caminho = "";
      String urlInicial = "";
      Image image = null;
      caminho =
          ParametroUtil.getValorParametroCitSmartHashMap(
              Enumerados.ParametroSistema.URL_LOGO_PADRAO_RELATORIO, "");

      if ("".equals(caminho.trim()) || !UtilImagem.verificaSeImagemExiste(caminho)) {
        urlInicial =
            ParametroUtil.getValorParametroCitSmartHashMap(
                Enumerados.ParametroSistema.URL_Sistema, "");
        caminho = urlInicial + "/imagens/logo/logo.png";
      }

      try {
        url = new URL(caminho);
        final URLConnection conn = url.openConnection();
        conn.connect();
      } catch (final MalformedURLException e) {
        // the URL is not in a valid form
        e.printStackTrace();
        url = null;
      } catch (final IOException e) {
        e.printStackTrace();
        url = null;
      }

      if (url == null) {
        if (Constantes.getValue("CAMINHO_LOGO_CITGERENCIAL") != null) {
          try {
            url = new URL(Constantes.getValue("CAMINHO_LOGO_CITGERENCIAL"));
          } catch (final Exception e) {
            e.printStackTrace();
          }
        }
      }

      if (url == null) {
        caminho =
            Constantes.getValue("SERVER_ADDRESS")
                + Constantes.getValue("CONTEXTO_APLICACAO")
                + "/imagens/logoPadraoRelatorio.png";
        try {
          url = new URL(caminho);
        } catch (final Exception e) {
          e.printStackTrace();
        }
      }

      if (url != null) {
        try {
          image = Image.getInstance(url);
        } catch (final BadElementException e) {
          e.printStackTrace();
        }
      }

      if (image != null) {
        image.scaleAbsolute(150, 50);
        image.setAlignment(Image.RIGHT);
        final Chunk ck = new Chunk(image, -3, -25);
        final PdfPCell cell = new PdfPCell();
        cell.addElement(ck);
        cell.setBorderWidth(0);
        cell.setRowspan(2);
        tableContent.addCell(cell);
      } else {
        tableContent.addCell("Citsmart");
      }

      final String strCab = Constantes.getValue("TEXTO_1a_LINHA_CABECALHO_CITGERENCIAL");
      if (strCab != null && !strCab.equalsIgnoreCase("")) {
        final PdfPCell cAux =
            new PdfPCell(
                new Phrase(strCab, new Font(Font.HELVETICA, 12, Font.BOLD, new Color(0, 0, 0))));
        cAux.setColspan(2);
        cAux.setBorderWidth(1);
        cAux.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        tableContent.addCell(cAux);
      }

      /* Adicionado o Titulo do relatório */
      final PdfPCell titulo =
          new PdfPCell(
              new Phrase(titleReport, new Font(Font.HELVETICA, 14, Font.BOLD, new Color(0, 0, 0))));
      titulo.setColspan(2);
      titulo.setRowspan(1);
      titulo.setBorderWidth(0);
      titulo.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      tableContent.addCell(titulo);

      /* Adicionado o filtro */
      String strFiltro =
          this.trataParameters(hshParameters, colParmsUtilizadosNoSQL, colDefinicaoParametros);
      if (strFiltro == null) {
        strFiltro = "";
      }
      final PdfPCell cFiltro =
          new PdfPCell(
              new Phrase(strFiltro, new Font(Font.HELVETICA, 8, Font.NORMAL, new Color(0, 0, 0))));
      cFiltro.setBorderWidth(0);
      cFiltro.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      cFiltro.setColspan(2);
      tableContent.addCell(cFiltro);
      cellC.addElement(tableContent);
      header.addCell(tableContent);

      // Fim - Trata parametros
      if (!existeAgrupador) {
        if (listRetorno != null && listRetorno.size() > 0) {
          final Object[] row = (Object[]) listRetorno.get(0);
          this.geraCabecalhoPDF(row.length, gerencialItemDto, header, writer, document, page);
        }
      }

      if (page.getWidth() > 600) {
        if (!existeAgrupador) {
          header.writeSelectedRows(0, -1, 20, 565, writer.getDirectContent());
        } else {
          header.writeSelectedRows(0, -1, 20, 585, writer.getDirectContent());
        }
      } else {
        if (!existeAgrupador) {
          header.writeSelectedRows(
              0,
              -1,
              20,
              page.getHeight() - document.topMargin() + header.getTotalHeight(),
              writer.getDirectContent());
        } else {
          header.writeSelectedRows(0, -1, 20, 805, writer.getDirectContent());
        }
      }

      /* Adicionado o footer */
      final PdfPTable footer = new PdfPTable(2);
      final String emissao = (String) hshParameters.get("citcorpore.comum.emissao");
      final String pagina = (String) hshParameters.get("citcorpore.comum.pagina");

      PdfPCell cAuxPageNumber =
          new PdfPCell(
              new Phrase(
                  emissao
                      + ": "
                      + UtilDatas.convertDateToString(
                          TipoDate.DATE_DEFAULT,
                          UtilDatas.getDataAtual(),
                          WebUtil.getLanguage(request))
                      + " "
                      + UtilDatas.formatHoraFormatadaStr(UtilDatas.getHoraAtual()),
                  new Font(Font.HELVETICA, 8, Font.NORMAL, new Color(0, 0, 0))));
      cAuxPageNumber.setBorder(0);
      footer.addCell(cAuxPageNumber);

      cAuxPageNumber =
          new PdfPCell(
              new Phrase(
                  pagina + ": " + writer.getPageNumber(),
                  new Font(Font.HELVETICA, 8, Font.NORMAL, new Color(0, 0, 0))));
      cAuxPageNumber.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      cAuxPageNumber.setBorder(0);
      footer.addCell(cAuxPageNumber);
      footer.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
      footer.writeSelectedRows(
          0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent());
    } catch (final Exception e) {
      throw new ExceptionConverter(e);
    }
  }