/// <summary>
    /// hecho por cesar pulido
    /// el dia 9 de diciembre de 2012
    /// para obtener el archivo de las clausulas
    /// </summary>
    /// <returns></returns>
    public byte[] GenerarPDFClausulas(Decimal ID_CONTRATO)
    {
        //obtenemos la informacion necesaria de las clausulas
        registroContrato _registroContrato = new registroContrato(Session["idEmpresa"].ToString(), Session["USU_LOG"].ToString());
        DataTable tablaInfoClausulas = _registroContrato.ObtenerInfoParaImprimirClausulas(ID_CONTRATO);

        if (tablaInfoClausulas.Rows.Count > 0)
        {
            tools _tools = new tools();

            //como existen clausulas antes de mostrarlas se debe actualizar el estado de impresion de clausulas
            DataTable tablaCon = _registroContrato.ObtenerConRegContratosPorRegistro(Convert.ToInt32(ID_CONTRATO));

            DataRow fila = tablaCon.Rows[0];

            //En esta variable cargamos el documento plantilla
            StreamReader archivo_original = new StreamReader(Server.MapPath(@"~\plantillas_reportes\clausulas.htm"));

            String html_clausula = archivo_original.ReadToEnd();

            archivo_original.Dispose();
            archivo_original.Close();

            String html_completo = "<html><body>";

            Int32 contadorClausulas = 0;

            //tenemos que recorrer todas las clausulas e imprimirlas en hojas diferentes pero un mismo documento
            foreach (DataRow filaClausula in tablaInfoClausulas.Rows)
            {
                //como se tiene un clausula entonces al html_compleo le adicionamos una plantilla contenido de clausula
                if (contadorClausulas == 0)
                {
                    html_completo = html_clausula;
                }
                else
                {
                    html_completo += "<div>linea para paginacion de pdf</div>";
                    html_completo += html_clausula;
                }

                //despues de haber agregado la plantilla se procede a reemplazar los tags
                if (Session["idEmpresa"].ToString() == "1")
                {
                    html_completo = html_completo.Replace("[NOMBRE_EMPRESA]", tabla.VAR_NOMBRE_SERTEMPO);
                }
                else
                {
                    html_completo = html_completo.Replace("[NOMBRE_EMPRESA]", tabla.VAR_NOMBRE_EYS);
                }
                html_completo = html_completo.Replace("[NOMBRE_TRABAJADOR]", filaClausula["NOMBRES"].ToString().Trim() + " " + filaClausula["APELLIDOS"].ToString().Trim());
                html_completo = html_completo.Replace("[NOMBRE_CLAUSULA]", filaClausula["NOMBRE"].ToString().Trim());
                html_completo = html_completo.Replace("[ENCABEZADO_CLAUSULA]", filaClausula["ENCABEZADO"].ToString().Trim());
                html_completo = html_completo.Replace("[CONTENIDO_CLAUSULA]", filaClausula["DESCRIPCION"].ToString().Trim());
                html_completo = html_completo.Replace("[CIUDAD_FIRMA]", "BOGOTA");
                html_completo = html_completo.Replace("[DIAS]", DateTime.Now.Day.ToString());
                html_completo = html_completo.Replace("[MES]", _tools.obtenerNombreMes(DateTime.Now.Month));
                html_completo = html_completo.Replace("[ANNO]", DateTime.Now.Year.ToString());

                contadorClausulas += 1;
            }

            html_completo += "</body></html>";

            //creamos un configuramos el documento de pdf
            //(tamaño de la hoja,margen izq, margen der, margin arriba margen abajo)
            iTextSharp.text.Document document = new iTextSharp.text.Document(new Rectangle(595, 842), 40, 40, 80, 40);

            using (MemoryStream streamArchivo = new MemoryStream())
            {
                iTextSharp.text.pdf.PdfWriter writer = PdfWriter.GetInstance(document, streamArchivo);

                // Our custom Header and Footer is done using Event Handler
                pdfEvents PageEventHandler = new pdfEvents();
                writer.PageEvent = PageEventHandler;

                // Define the page header
                // Define the page header
                if (Session["idEmpresa"].ToString() == "1")
                {
                    PageEventHandler.dirImagenHeader = Server.MapPath("~/imagenes/reportes/logo_sertempo.png");
                }
                else
                {
                    PageEventHandler.dirImagenHeader = Server.MapPath("~/imagenes/reportes/logo_eficiencia.png");
                }

                PageEventHandler.fechaImpresion = DateTime.Now;
                PageEventHandler.tipoDocumento = "clausula";

                document.Open();

                //capturamos el archivo temporal del response
                String tempFile = Path.GetTempFileName();

                //y lo llenamos con el html de la plantilla
                using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
                {
                    tempwriter.Write(html_completo);
                }

                //leeemos el archivo temporal y lo colocamos en el documento de pdf
                List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StreamReader(tempFile), new StyleSheet());

                foreach (IElement element in htmlarraylist)
                {
                    if (element.Chunks.Count > 0)
                    {
                        if (element.Chunks[0].Content == "linea para paginacion de pdf")
                        {
                            document.NewPage();
                        }
                        else
                        {
                            document.Add(element);
                        }
                    }
                    else
                    {
                        document.Add(element);
                    }
                }

                //limpiamos todo
                document.Close();

                writer.Close();

                return streamArchivo.ToArray();
            }
        }
        else
        {
            return null;
        }
    }
    protected void Button_CLAUSULAS_Click(object sender, EventArgs e)
    {
        Decimal ID_CONTRATO = Convert.ToDecimal(HiddenField_ID_CONTRATO.Value);

        registroContrato _registroContrato = new registroContrato(Session["idEmpresa"].ToString(), Session["USU_LOG"].ToString());
        DataTable tablaInfoClausulas = _registroContrato.ObtenerInfoParaImprimirClausulas(Convert.ToDecimal(HiddenField_ID_CONTRATO.Value));

        if (tablaInfoClausulas.Rows.Count <= 0)
        {
            if (_registroContrato.MensajeError != null)
            {
                Informar(Panel_MENSAJE_IMPRESIONES_BASICAS, Label_MENSAJE_IMPRESIONES_BASICAS, _registroContrato.MensajeError, Proceso.Error);
            }
            else
            {
                DataTable tablaCon = _registroContrato.ObtenerConRegContratosPorRegistro(Convert.ToInt32(ID_CONTRATO));
                DataRow fila = tablaCon.Rows[0];
                _registroContrato.ActualizarConRegContratosImpresos(Convert.ToInt32(ID_CONTRATO), fila["CONTRATO_IMPRESO"].ToString(), "S");

                Informar(Panel_MENSAJE_IMPRESIONES_BASICAS, Label_MENSAJE_IMPRESIONES_BASICAS, "ADVERTENCIA: No existen clausulas para este contrato y perfil, puede continuar, con los siguientes tramites.", Proceso.Error);
            }
        }
        else
        {
            tools _tools = new tools();

            DataTable tablaCon = _registroContrato.ObtenerConRegContratosPorRegistro(Convert.ToInt32(ID_CONTRATO));
            DataRow fila = tablaCon.Rows[0];
            _registroContrato.ActualizarConRegContratosImpresos(Convert.ToInt32(ID_CONTRATO), fila["CONTRATO_IMPRESO"].ToString(), "S");

            StreamReader archivo_original = new StreamReader(Server.MapPath(@"~\plantillas_reportes\clausulas.htm"));

            String html_clausula = archivo_original.ReadToEnd();

            archivo_original.Dispose();
            archivo_original.Close();

            String html_completo = "<html><body>";

            Int32 contadorClausulas = 0;

            foreach (DataRow filaClausula in tablaInfoClausulas.Rows)
            {
                if (contadorClausulas == 0)
                {
                    html_completo = html_clausula;
                }
                else
                {
                    html_completo += "<div>linea para paginacion de pdf</div>";
                    html_completo += html_clausula;
                }

                if (Session["idEmpresa"].ToString() == "1")
                {
                    html_completo = html_completo.Replace("[NOMBRE_EMPRESA]", tabla.VAR_NOMBRE_SERTEMPO);
                }
                else
                {
                    html_completo = html_completo.Replace("[NOMBRE_EMPRESA]", tabla.VAR_NOMBRE_EYS);
                }
                html_completo = html_completo.Replace("[NOMBRE_TRABAJADOR]", filaClausula["NOMBRES"].ToString().Trim() + " " + filaClausula["APELLIDOS"].ToString().Trim());
                html_completo = html_completo.Replace("[NOMBRE_CLAUSULA]", filaClausula["NOMBRE"].ToString().Trim());
                html_completo = html_completo.Replace("[ENCABEZADO_CLAUSULA]", filaClausula["ENCABEZADO"].ToString().Trim());
                html_completo = html_completo.Replace("[CONTENIDO_CLAUSULA]", filaClausula["DESCRIPCION"].ToString().Trim());
                html_completo = html_completo.Replace("[DIAS]", DateTime.Now.Day.ToString());
                html_completo = html_completo.Replace("[MES]", _tools.obtenerNombreMes(DateTime.Now.Month));
                html_completo = html_completo.Replace("[ANNO]", DateTime.Now.Year.ToString());

                contadorClausulas += 1;
            }

            html_completo += "</body></html>";

            String filename = "clausulas_contrato_" + ID_CONTRATO.ToString();

            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=" + filename + ".pdf");

            Response.Clear();
            Response.ContentType = "application/pdf";

            iTextSharp.text.Document document = new iTextSharp.text.Document(new Rectangle(595, 842), 40, 40, 80, 40);

            iTextSharp.text.pdf.PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

            pdfEvents PageEventHandler = new pdfEvents();
            writer.PageEvent = PageEventHandler;

            if (Session["idEmpresa"].ToString() == "1")
            {
                PageEventHandler.dirImagenHeader = Server.MapPath("~/imagenes/reportes/logo_sertempo.png");
            }
            else
            {
                PageEventHandler.dirImagenHeader = Server.MapPath("~/imagenes/reportes/logo_eficiencia.png");
            }

            PageEventHandler.fechaImpresion = DateTime.Now;
            PageEventHandler.tipoDocumento = "clausula";

            document.Open();

            String tempFile = Path.GetTempFileName();

            using (StreamWriter tempwriter = new StreamWriter(tempFile, false))
            {
                tempwriter.Write(html_completo);
            }

            List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StreamReader(tempFile), new StyleSheet());

            foreach (IElement element in htmlarraylist)
            {
                if (element.Chunks.Count > 0)
                {
                    if (element.Chunks[0].Content == "linea para paginacion de pdf")
                    {
                        document.NewPage();
                    }
                    else
                    {
                        document.Add(element);
                    }
                }
                else
                {
                    document.Add(element);
                }
            }

            document.Close();
            writer.Close();

            Response.End();

            File.Delete(tempFile);
        }
    }