@Override public Element getITextElement() { PdfPCell cell = new PdfPCell(); cell.setBorderColorTop(getBorderTopColor()); cell.setBorderColorBottom(getBorderBottomColor()); cell.setBorderColorLeft(getBorderLeftColor()); cell.setBorderColorRight(getBorderRightColor()); cell.setBorderWidthTop(APdfBuilder.mmToPoints(getBorderTopWidth())); cell.setBorderWidthBottom(APdfBuilder.mmToPoints(getBorderBottomWidth())); cell.setBorderWidthLeft(APdfBuilder.mmToPoints(getBorderLeftWidth())); cell.setBorderWidthRight(APdfBuilder.mmToPoints(getBorderRightWidth())); cell.setPadding(0); cell.setPaddingTop(APdfBuilder.mmToPoints(getPaddingTop())); cell.setPaddingBottom(APdfBuilder.mmToPoints(getPaddingBottom())); cell.setPaddingLeft(APdfBuilder.mmToPoints(getPaddingLeft())); cell.setPaddingRight(APdfBuilder.mmToPoints(getPaddingRight())); cell.setExtraParagraphSpace(0); cell.setIndent(0); cell.setUseBorderPadding(false); cell.setBackgroundColor(getBackgroundColor()); cell.setColspan(getColspan()); for (ItextElement element : elements) cell.addElement(element.getITextElement()); return cell; }
public void createPdf() throws ClassNotFoundException, SQLException { /*Declaramos documento como un objeto Document Asignamos el tamaño de hoja y los margenes */ Document documento = new Document(PageSize.A4.rotate(), 1, 1, 20, 36); // writer es declarado como el método utilizado para escribir en el archivo PdfWriter writer = null; try { // Obtenemos la instancia del archivo a utilizar String filename = "C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/documentos/reporteAutorizacion.pdf"; File fichero = new File(filename); if (fichero.exists()) { System.out.println("El fichero existe222222222"); fichero.delete(); System.out.println("El fichero elimino"); } else { System.out.println("el fichero no existe"); } writer = PdfWriter.getInstance(documento, new FileOutputStream(filename)); } catch (Exception ex) { System.out.println("NO creooooo"); ex.getMessage(); } // Declaramos una instancia de los eventos en HeaderFooter HeaderFooter event = new HeaderFooter(); writer.setPageEvent(event); // Agregamos un titulo al archivo documento.addTitle("ARCHIVO PDF"); // Agregamos el autor del archivo documento.addAuthor("PROVIAS NACIONAL"); // Abrimos el documento para edición documento.open(); try { // Declaramos un texto como Paragraph // Le podemos dar formado como alineación, tamaño y color a la fuente. Paragraph title1 = new Paragraph(); title1.setAlignment(Element.ALIGN_CENTER); PdfPTable ta1 = new PdfPTable(2); ta1.setHorizontalAlignment(Element.ALIGN_CENTER); ta1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); ta1.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cel1 = new PdfPCell(); com.lowagie.text.Image image = com.lowagie.text.Image.getInstance("D:/Provias/images/imgExports.jpg"); // image.setWidthPercentage(500); cel1.addElement(image); cel1.setHorizontalAlignment(Element.ALIGN_CENTER); cel1.setVerticalAlignment(Element.ALIGN_MIDDLE); cel1.setBorder(Rectangle.NO_BORDER); ta1.addCell(cel1); PdfPCell cel2 = new PdfPCell(); cel2.addElement( new Phrase( "Reporte de Autorizaciones Especiales", FontFactory.getFont( FontFactory.HELVETICA, 12, Font.BOLD, new CMYKColor(0, 0, 0, 100)))); cel2.setFixedHeight(90); cel2.setHorizontalAlignment(Element.ALIGN_CENTER); cel2.setVerticalAlignment(Element.ALIGN_MIDDLE); cel2.setBorder(Rectangle.NO_BORDER); ta1.addCell(cel2); float[] columnWidths1 = new float[] {180f, 100f}; ta1.setWidths(columnWidths1); title1.add(ta1); // Agregamos el texto al documento documento.add(title1); // Agregamos un salto de linea documento.add(new Paragraph(" ")); // Agregamos la tabla al documento haciendo // la llamada al método tabla() documento.add(tabla()); } catch (Exception ex) { System.out.println("Por!!"); ex.getMessage(); } documento.close(); // Cerramos el documento writer.close(); // Cerramos writer }
@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); } }