public void OpenTask(Exam exam, Ticket ticket, bool markAnswers) { CloseWord(); OneAnswerQuestion[] questions = ticket.GetQuestions().Cast <OneAnswerQuestion>().ToArray(); using (DocX docXdocument = DocX.Create(path.ToString())) { int i, j; docXdocument.AddFooters(); docXdocument.SetDefaultFont(new Xceed.Document.NET.Font("Times New Roman")); Xceed.Document.NET.Paragraph headerParagraph = docXdocument.InsertParagraph(); Xceed.Document.NET.Paragraph paragraph = docXdocument.InsertParagraph(); headerParagraph.Append($"Перегляд завдання") .Bold() .FontSize(28); paragraph.Append($"Завдання білету \"{ticket.TicketName}\":") .FontSize(24); Xceed.Document.NET.Table table = docXdocument.AddTable(questions.Length, 2); table.SetWidthsPercentage(new[] { 50f, 50 }, docXdocument.PageWidth - docXdocument.PageWidth / 5); table.SetBorder(TableBorderType.Bottom, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Top, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Left, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Right, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.InsideV, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); for (i = 0; i < questions.Length; i++) { table.Rows[i].Cells[0] .InsertParagraph() .Append($"{questions[i].QuestionNumber + exam.FirstQuestionNumber}) {questions[i].QuestionContent.Text}") .FontSize(12); Xceed.Document.NET.Paragraph tableParagraph = table.Rows[i].Cells[1].InsertParagraph(); for (j = 0; j < questions[i].QuestionContent.Answers.Count; j++) { Xceed.Document.NET.Paragraph tempParagraph = tableParagraph.Append($"{(j < questions[i].QuestionContent.Letters.Length ? questions[i].QuestionContent.Letters[j].ToString() : "*")}{questions[i].QuestionContent.Devider} { questions[i].QuestionContent.Answers[j]};{Environment.NewLine}"); if (markAnswers && questions[i].Answer.Content == questions[i].QuestionContent.Answers[j]) { tempParagraph.Bold() .FontSize(12); } } } docXdocument.InsertTable(table); docXdocument.Footers.Odd.InsertParagraph() .Append("Створено за допомогою SimplEx Program") .FontSize(10) .UnderlineStyle(UnderlineStyle.singleLine) .Alignment = Alignment.right; docXdocument.Save(); } OpenWord(); }
public ActionResult GetDocx() { try { using (var ManNac = new EmpleadosEntities()) { var ListNac = ManNac.Nacionalidad.ToList(); //Ubicacion de Archivo string filename = @"C:\Users\Rodrigo_Menares\Downloads\ListaNacionalidades.docx"; var doc = DocX.Create(filename); //Carga una imagen en formato JPG var image = doc.AddImage(Server.MapPath("/Imagenes/bg.jpg")); // Set Picture Height and Width. var picture = image.CreatePicture(50, 50); picture.Width = 50; picture.Height = 50; //Titulo Del Documento string title = "Lista De Cargos"; //Formato del Titulo Formatting titleFormat = new Formatting(); //Specify font family titleFormat.FontFamily = new Xceed.Document.NET.Font("Arial Black"); //Specify font size y color del texto titleFormat.Size = 14D; titleFormat.Position = 40; titleFormat.FontColor = System.Drawing.Color.Orange; titleFormat.UnderlineColor = System.Drawing.Color.Gray; titleFormat.Italic = true; //combina el titulo con el formato definido Xceed.Document.NET.Paragraph paragraphTitle = doc.InsertParagraph(title, false, titleFormat); // alinea el titulo al centro paragraphTitle.Alignment = Alignment.center; //Insert text Table tbl = doc.AddTable(ListNac.Count + 1, 2); //hace que la tabla este al centro de la pagina tbl.Alignment = Alignment.center; tbl.Design = TableDesign.ColorfulList; //agrega los titulos de la tabla tbl.Rows[0].Cells[0].Paragraphs.First().Append("Código Nacionalidad").FontSize(12D).Alignment = Alignment.center; tbl.Rows[0].Cells[1].Paragraphs.First().Append("Nombre Nacionalidad").FontSize(12D).Alignment = Alignment.center; //llena las celdas con los datos int fila = 1; int columna = 0; foreach (var item in ListNac) { tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Id_Nac)).FontSize(12D).Alignment = Alignment.right; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Descripcion)).FontSize(12D).Alignment = Alignment.center; fila++; columna = 0; } //inserta la tabla dentro del documento doc.InsertTable(tbl); //Genera el Pie de Pagina del Documento doc.AddFooters(); //Indica que que la primera página tendrá pies de página independientes doc.DifferentFirstPage = true; //Indica que que la página par e impar tendrá pies de página separados doc.DifferentOddAndEvenPages = true; Footer footer_main = doc.Footers.First; Paragraph pFooter = footer_main.Paragraphs.First(); pFooter.Alignment = Alignment.center; pFooter.Append("Página ").Bold(); pFooter.AppendPageNumber(PageNumberFormat.normal).Bold(); pFooter.Append("/").Bold(); pFooter.AppendPageCount(PageNumberFormat.normal).Bold(); //graba el documento doc.Save(); //abre word y el documento Process.Start("WINWORD", filename); return(RedirectToAction("Index")); } } catch (Exception ex) { Logger.Error("Error On:", ex); Response.StatusCode = 500; Response.StatusDescription = ex.Message; return(Json(Response)); } }
public ActionResult CargFamDocx() { try { using (CargFam = new EmpleadosEntities()) { var ListCargFam = CargFam.Sp_Mues_CargFam().ToList(); //Ubicacion de Archivo string filename = @"C:\Users\Rodrigo_Menares\Downloads\ListaCargFam.docx"; var doc = DocX.Create(filename); //cambia la orientacion de la pagina doc.PageLayout.Orientation = Orientation.Landscape; //Carga una imagen en formato JPG var image = doc.AddImage(Server.MapPath("/Imagenes/bg.jpg")); // Set Picture Height and Width. var picture = image.CreatePicture(50, 50); picture.Width = 50; picture.Height = 50; //Titulo Del Documento string title = "Lista De Cargas Familiares"; //Formato del Titulo Formatting titleFormat = new Formatting(); //Specify font family titleFormat.FontFamily = new Xceed.Document.NET.Font("Arial Black"); //Specify font size y color del texto titleFormat.Size = 14D; titleFormat.Position = 40; titleFormat.FontColor = System.Drawing.Color.Orange; titleFormat.UnderlineColor = System.Drawing.Color.Gray; titleFormat.Italic = true; //combina el titulo con el formato definido Xceed.Document.NET.Paragraph paragraphTitle = doc.InsertParagraph(title, false, titleFormat); // alinea el titulo al centro paragraphTitle.Alignment = Alignment.center; //define las dimensiones de la tabla (tbl(f,c)) Table tbl = doc.AddTable(ListCargFam.Count + 1, 11); //hace que la tabla este al centro de la pagina tbl.Alignment = Alignment.center; tbl.Design = TableDesign.ColorfulList; tbl.AutoFit = AutoFit.Contents; //agrega los titulos de la tabla tbl.Rows[0].Cells[0].Paragraphs.First().Append("Rut").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[1].Paragraphs.First().Append("Nombre").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[2].Paragraphs.First().Append("Ap. Paterno").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[3].Paragraphs.First().Append("Fono Movil").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[4].Paragraphs.First().Append("Fecha Nacimiento").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[5].Paragraphs.First().Append("Sexo").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[6].Paragraphs.First().Append("Dirección").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[7].Paragraphs.First().Append("Comuna").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[8].Paragraphs.First().Append("Correo").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[9].Paragraphs.First().Append("Nombre Empleado").FontSize(8D).Alignment = Alignment.center; tbl.Rows[0].Cells[10].Paragraphs.First().Append("Comentarios").FontSize(8D).Alignment = Alignment.center; //llena las celdas con los datos int fila = 1; int columna = 0; foreach (var item in ListCargFam) { tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Rut_Carga)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Nombre)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Paterno)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Fono_Movil)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Fecha_Nacimiento)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Sexo)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Direccion)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Comuna)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Email)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Nombre_Empleado)).FontSize(8D).Alignment = Alignment.left; columna++; tbl.Rows[fila].Cells[columna].Paragraphs.First().Append(Convert.ToString(item.Comentarios)).FontSize(8D).Alignment = Alignment.left; fila++; columna = 0; } //inserta la tabla dentro del documento doc.InsertTable(tbl); //Genera el Pie de Pagina del Documento doc.AddFooters(); //Indica que que la primera página tendrá pies de página independientes doc.DifferentFirstPage = true; //Indica que que la página par e impar tendrá pies de página separados doc.DifferentOddAndEvenPages = true; Footer footer_main = doc.Footers.First; Paragraph pFooter = footer_main.Paragraphs.First(); pFooter.Alignment = Alignment.center; pFooter.Append("Página ").Bold(); pFooter.AppendPageNumber(PageNumberFormat.normal).Bold(); pFooter.Append("/").Bold(); pFooter.AppendPageCount(PageNumberFormat.normal).Bold(); //graba el documento doc.Save(); //abre word y el documento Process.Start("WINWORD", filename); return(RedirectToAction("Index")); } } catch (Exception ex) { Logger.Error("Error On:", ex); Response.StatusCode = 500; Response.StatusDescription = ex.Message; return(Json(Response)); } }
public void OpenBlank(Exam exam, Ticket ticket, bool markAnswers) { CloseWord(); using (DocX docXdocument = DocX.Create(path.ToString())) { int i, j; docXdocument.AddFooters(); docXdocument.SetDefaultFont(new Xceed.Document.NET.Font("Times New Roman"), 14); Xceed.Document.NET.Paragraph headerParagraph = docXdocument.InsertParagraph(); Xceed.Document.NET.Paragraph paragraph = docXdocument.InsertParagraph(); headerParagraph.Append($"Виконання завдань") .Bold() .FontSize(16); paragraph = docXdocument.InsertParagraph(); paragraph.Append($"Здобувач освіти") .Bold() .Append("\t\t\t\t\t\t\t") .UnderlineStyle(UnderlineStyle.singleLine) .Append("білет №") .Bold() .Append("\t\t") .UnderlineStyle(UnderlineStyle.singleLine); paragraph = docXdocument.InsertParagraph(); paragraph.Append($"{Environment.NewLine}I частина") .Bold() .Alignment = Alignment.center; paragraph = docXdocument.InsertParagraph(); paragraph.Append($"\tОзнайомтесь з тестовим завданням. Дайте відповіді на тестові завдання в таблиці" + $"(поряд із номером запитання зазначте номер правильної відповіді){Environment.NewLine}") .Alignment = Alignment.left; List <OneAnswerQuestion[]> questionLists = new List <OneAnswerQuestion[]>(); for (i = 0; i < exam.Themes.Count; i++) { OneAnswerQuestion[] questions = exam.Themes[i].GetQuestions(ticket).Cast <OneAnswerQuestion>().ToArray(); Array.Sort(questions, (Question a, Question b) => { if (a.QuestionNumber > b.QuestionNumber) { return(1); } else if (a.QuestionNumber < b.QuestionNumber) { return(-1); } return(0); }); questionLists.Add(questions); } int max = questionLists.Max(a => a.Length); Xceed.Document.NET.Table table = docXdocument.AddTable(max + 1, questionLists.Count * 2); table.SetBorder(TableBorderType.Bottom, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Top, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Left, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); table.SetBorder(TableBorderType.Right, new Xceed.Document.NET.Border(BorderStyle.Tcbs_double, BorderSize.two, 0, Color.Black)); List <float> percentages = new List <float>(); float pa = 30 / (table.ColumnCount / 2); float pb = 70 / (table.ColumnCount / 2); for (i = 0; i < table.ColumnCount / 2; i += 2) { table.Rows[0].MergeCells(i, i + 1); percentages.Add(pa); percentages.Add(pb); i--; } table.SetWidthsPercentage(percentages.ToArray(), docXdocument.PageWidth - docXdocument.PageWidth / 5); for (i = 0; i < exam.Themes.Count; i++) { table.Rows[0].Cells[i].InsertParagraph() .Append(exam.Themes[i].ThemeName) .Alignment = Alignment.center; } for (i = 0; i < table.ColumnCount / 2; i++) { for (j = 0; j < questionLists[i].Length; j++) { table.Rows[1 + j].Cells[i * 2].InsertParagraph() .Append($"{questionLists[i][j].QuestionNumber + exam.FirstQuestionNumber}"); if (markAnswers) { int index = questionLists[i][j].QuestionContent.Answers.IndexOf(questionLists[i][j].Answer.Content); table.Rows[1 + j].Cells[i * 2 + 1].InsertParagraph() .Append(index >= 0 && index < questionLists[i][j].QuestionContent.Letters.Length ? questionLists[i][j].QuestionContent.Letters[index].ToString() : "*"); } } } docXdocument.InsertTable(table); paragraph = docXdocument.InsertParagraph(); paragraph.Append($"{Environment.NewLine}{Environment.NewLine}Балл: ") .Bold() .Alignment = Alignment.right; if (!markAnswers) { paragraph.Append("\t\t") .UnderlineStyle(UnderlineStyle.singleLine) .Alignment = Alignment.right; } else { paragraph.Append($"{ticket.MaxPoints:F2}") .UnderlineStyle(UnderlineStyle.singleLine) .Alignment = Alignment.right; } docXdocument.Footers.Odd.InsertParagraph() .Append("Створено за допомогою SimplEx Program") .FontSize(10) .UnderlineStyle(UnderlineStyle.singleLine) .Alignment = Xceed.Document.NET.Alignment.right; docXdocument.Save(); } OpenWord(); }