//Exportaciones #region Crea_Pdf public ActionResult GetPdf() { try { using (var ManNac = new EmpleadosEntities()) { var ListNac = ManNac.Nacionalidad.ToList(); MemoryStream ms = new MemoryStream(); iTextSharp.text.Document document = new iTextSharp.text.Document(); document.SetPageSize(PageSize.A4); document.SetMargins(50, 50, 50, 50); PdfWriter pdf = PdfWriter.GetInstance(document, ms); //agrega el autor del documento document.AddAuthor("Rodrigo Menares Guzman"); // document.AddTitle("Listado_de_Cargos"); //hace la insercion del pie de pagina pdf.PageEvent = new HeadFooter(); document.Open(); //insercion de imagenes string url = Server.MapPath("/Imagenes/bg.jpg"); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(url); image.ScaleToFit(140f, 120f); image.Alignment = Element.ALIGN_LEFT; document.Add(image); // fin de insercion de imagenes //fuente, tamaño y color de cabecera BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); iTextSharp.text.Font fontText2 = new iTextSharp.text.Font(bf, 16, 4, BaseColor.BLUE); //creacion e insercion de titulos al documento iTextSharp.text.Paragraph titulo = new iTextSharp.text.Paragraph(string.Format("Listado de Nacionalidades"), fontText2); titulo.Alignment = 1; //0-Left, 1 middle,2 Right //inserta al documento document.Add(titulo); //inserta nueva linea al texto document.Add(iTextSharp.text.Chunk.NEWLINE); //esto es para estilo de letra de la tabla BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); //tamaño y color iTextSharp.text.Font fontText = new iTextSharp.text.Font(bf2, 10, 0, BaseColor.BLACK); iTextSharp.text.Font fontText3 = new iTextSharp.text.Font(bf2, 10, 0, BaseColor.WHITE); // instancia la tabla y le indica la cantidad de columnas PdfPTable table = new PdfPTable(2); //indica q ancho de la hoja va a ocupar la tabla table.WidthPercentage = 95; // instancia para la generacion de celdas en la tabla PdfPCell _cell = new PdfPCell(); //genera la cabecera de la tabla _cell = new PdfPCell(new iTextSharp.text.Paragraph("Código Nacionalidad", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Nacionalidad", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); //llena la tabla y ademas le da la alineacion a los datos foreach (var item in ListNac) { PdfPCell _cell2 = new PdfPCell(); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Id_Nac.ToString(), fontText)); _cell2.HorizontalAlignment = Element.ALIGN_RIGHT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Descripcion, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell2); } //agrega la tabla al documento document.Add(table); //cierra el documento document.Close(); //vacia la memoria(documento) hacia memory stream byte[] byteStream = ms.ToArray(); ms = new MemoryStream(); ms.Write(byteStream, 0, byteStream.Length); ms.Position = 0; //esto permite que el archivo pdf se muestre por pantalla en el explorador y a su vez sea guardado en el disco return(File(ms, "application/pdf", "ListaNacionalidades.pdf")); } } catch (Exception ex) { Logger.Error("Error On:", ex); Response.StatusCode = 500; Response.StatusDescription = ex.Message; return(Json(Response)); } }
//Exportaciones #region Crea_PDF public ActionResult CargFamPDF() { try { using (CargFam = new EmpleadosEntities()) { var ListCargFam = CargFam.Sp_Mues_CargFam().ToList(); MemoryStream ms = new MemoryStream(); iTextSharp.text.Document document = new iTextSharp.text.Document(); document.SetPageSize(PageSize.A4.Rotate()); document.SetMargins(14.2f, 14.2f, 29f, 31f); PdfWriter pdf = PdfWriter.GetInstance(document, ms); //hace la insercion del pie de pagina pdf.PageEvent = new HeadFooter(); document.Open(); //insercion de imagenes url = Server.MapPath("/Imagenes/bg.jpg"); iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(url); image.ScaleToFit(140f, 120f); image.Alignment = Element.ALIGN_LEFT; document.Add(image); // fin de insercion de imagenes //fuente, tamaño y color de cabecera BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); iTextSharp.text.Font fontText2 = new iTextSharp.text.Font(bf, 10, 4, BaseColor.BLUE); //creacion e insercion de titulos al documento iTextSharp.text.Paragraph titulo = new iTextSharp.text.Paragraph(string.Format("Listado de Cargas Familiares"), fontText2); //titulo.SpacingBefore = 200; //titulo.SpacingAfter = 0; titulo.Alignment = 1; //0-Left, 1 middle,2 Right //inserta al documento document.Add(titulo); //inserta nueva linea al texto document.Add(Chunk.NEWLINE); //esto es para estilo de letra de la tabla BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.EMBEDDED); //tamaño y color iTextSharp.text.Font fontText = new iTextSharp.text.Font(bf2, 8, 0, BaseColor.BLACK); iTextSharp.text.Font fontText3 = new iTextSharp.text.Font(bf2, 8, 0, BaseColor.WHITE); // instancia la tabla y le indica la cantidad de columnas PdfPTable table = new PdfPTable(15); //indica q ancho de la hoja va a ocupar la tabla table.WidthPercentage = 95; // instancia para la generacion de celdas en la tabla PdfPCell _cell = new PdfPCell(); //genera la cabecera de la tabla _cell = new PdfPCell(new iTextSharp.text.Paragraph("Rut", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Nombre", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Ap. Paterno", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Ap. Materno", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Fono Movil", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Fono Fijo", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Fecha Nacimiento", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Sexo", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Dirección", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Villa", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Comuna", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Ciudad", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Correo", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Nombre Empleado", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); _cell = new PdfPCell(new iTextSharp.text.Paragraph("Comentario", fontText3)); _cell.BackgroundColor = iTextSharp.text.BaseColor.DARK_GRAY; _cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell); //llena la tabla y ademas le da la alineacion a los datos foreach (var item in ListCargFam) { PdfPCell _cell2 = new PdfPCell(); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Rut_Carga, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Nombre, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Paterno, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Materno, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Fono_Movil, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Fono_Fijo, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Fecha_Nacimiento, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Sexo, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Direccion, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Villa, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Comuna, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Ciudad, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Email, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Nombre_Empleado, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); _cell2 = new PdfPCell(new iTextSharp.text.Paragraph(item.Comentarios, fontText)); _cell2.HorizontalAlignment = Element.ALIGN_LEFT; table.AddCell(_cell2); } //agrega la tabla al documento document.Add(table); //cierra el documento document.Close(); //vacia la memoria(documento) hacia memory stream byte[] byteStream = ms.ToArray(); ms = new MemoryStream(); ms.Write(byteStream, 0, byteStream.Length); ms.Position = 0; //esto permite que el archivo pdf se muestre por pantalla en el explorador y a su vez sea guardado en el disco return(File(ms, "application/pdf", "ListaCargFam.pdf")); } } catch (Exception ex) { Logger.Error("Error On:", ex); Response.StatusCode = 500; Response.StatusDescription = ex.Message; return(Json(Response)); } }