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);
        }
    }