// Copy Page Layout. // // if next error message shows up, download "ooxml-schemas-1.1.jar" file and // add it to classpath. // // [Error] // The type org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar // cannot be resolved. // It is indirectly referenced from required .class files // // This error happens because there is no CTPageMar class in // poi-ooxml-schemas-3.10.1-20140818.jar. // // [ref.] http://poi.apache.org/faq.html#faq-N10025 // [ref.] http://poi.apache.org/overview.html#components // // < ooxml-schemas 1.1 download > // http://repo.maven.apache.org/maven2/org/apache/poi/ooxml-schemas/1.1/ // private static void CopyLayout(XWPFDocument srcDoc, XWPFDocument destDoc) { CT_PageMar pgMar = srcDoc.Document.body.sectPr.pgMar; string bottom = pgMar.bottom; ulong footer = pgMar.footer; ulong gutter = pgMar.gutter; ulong header = pgMar.header; ulong left = pgMar.left; ulong right = pgMar.right; string top = pgMar.top; //CT_PageMar addNewPgMar = destDoc.Document.body.sectPr.pgMar; //addNewPgMar.bottom = bottom; //addNewPgMar.footer = footer; //addNewPgMar.gutter = gutter; //addNewPgMar.setHeader(header); //addNewPgMar.setLeft(left); //addNewPgMar.setRight(right); //addNewPgMar.setTop(top); //CT_PageSz pgSzSrc = srcDoc.Document.body.sectPr.pgSz; //string code = pgSzSrc.code; //BigInteger h = pgSzSrc.getH(); //Enum orient = pgSzSrc.getOrient(); //BigInteger w = pgSzSrc.getW(); //CT_PageSz addNewPgSz = destDoc.getDocument().getBody().addNewSectPr().addNewPgSz(); //addNewPgSz.setCode(code); //addNewPgSz.setH(h); //addNewPgSz.setOrient(orient); //addNewPgSz.setW(w); }
private bool word_export_student(StudentData s) { XWPFParagraph p; XWPFRun r; int chapter_idx; int section_idx; try { _log.Info("Esportazione sudente in Word..."); _log.Info(s.Dump()); _log.Info("Cartella di esportazione: " + _settings.ExportFolder); if (Directory.Exists(_settings.ExportFolder) == false) { _log.Error("Errore! La cartella di esportazione non esiste!"); return(false); } // see https://github.com/tonyqus/npoi/blob/master/examples/xwpf/SimpleDocument/Program.cs XWPFDocument doc = new XWPFDocument(); doc.Document.body.sectPr = new CT_SectPr(); CT_PageMar margin = doc.Document.body.sectPr.pgMar; margin.top = mm_to_twip(15).ToString(); margin.bottom = mm_to_twip(15).ToString(); margin.left = mm_to_twip(15); margin.right = mm_to_twip(15); string font_family = "Courier New"; // Title p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.CENTER; p.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER; p.SpacingAfter = 500; r = p.CreateRun(); r.IsBold = true; r.FontSize = 16; r.FontFamily = font_family; r.SetText(_settings.EvaluationTitle); // Header: teacher, data p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.LEFT; r = p.CreateRun(); r.FontSize = 12; r.FontFamily = font_family; r.SetText("Insegnante: " + _settings.TeacherFirstName + " " + _settings.TeacherLastName); p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.LEFT; p.SpacingAfter = 500; r = p.CreateRun(); r.FontSize = 12; r.FontFamily = font_family; r.SetText("Data : " + DateStringName); // Student p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.LEFT; r = p.CreateRun(); r.FontSize = 12; r.FontFamily = font_family; r.SetText("Studente : " + s.FirstName + " " + s.LastName); p = doc.CreateParagraph(); p.SpacingAfter = 500; p.Alignment = ParagraphAlignment.LEFT; r = p.CreateRun(); r.FontSize = 12; r.FontFamily = font_family; r.SetText("Data di nascita: " + s.BirthDate.ToString("dd/MM/yyyy")); // Evaluation chapter_idx = 0; foreach (EvaluationChapter chapter in DataContainer.Instance.EvaluationScheme.Chapters) { // Student p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.LEFT; p.SpacingAfter = 200; r = p.CreateRun(); r.FontSize = 16; r.IsBold = true; r.FontFamily = font_family; r.SetText(chapter.Name); section_idx = 0; StringBuilder sb = new StringBuilder(); foreach (EvaluationSection section in chapter.Sections) { int level = s.GetEvaluationLevel(chapter_idx, section_idx); if (level >= 0) { sb.Append(DataContainer.Instance.EvaluationScheme.GetLevelDescription(chapter_idx, section_idx, level) + " "); } section_idx++; } p = doc.CreateParagraph(); p.Alignment = ParagraphAlignment.LEFT; p.SpacingAfter = 400; r = p.CreateRun(); r.FontSize = 12; r.FontFamily = font_family; r.SetText(sb.ToString()); chapter_idx++; } // Save to disk string word_file_name = s.LastName + "_" + s.FirstName + "_" + DateTime.Now.ToString("dd-MM-yyyy_hh-mm") + ".docx"; _log.Info("Nome file : " + word_file_name); string complete_word_file_name = Path.Combine(_settings.ExportFolder, word_file_name); FileStream out1 = new FileStream(complete_word_file_name, FileMode.Create); doc.Write(out1); out1.Close(); if (_settings.OpenAfterExport) { Process.Start(complete_word_file_name); } else { MessageBoxResult result = MessageBox.Show("Valutazione studente esportata in formato Word!", "Esportazione riuscita!", MessageBoxButton.OK); } return(true); } catch (Exception exc) { _log.Error("Exception in export_student_word(): " + exc.Message); return(false); } }