public void Build(GeneralTree <IDirectoryTreeNode> features) { string filename = string.IsNullOrEmpty(configuration.SystemUnderTestName) ? "features.docx" : configuration.SystemUnderTestName + ".docx"; string documentFileName = Path.Combine(configuration.OutputFolder.FullName, filename); if (File.Exists(documentFileName)) { File.Delete(documentFileName); } using ( WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Create(documentFileName, WordprocessingDocumentType .Document)) { MainDocumentPart mainDocumentPart = wordProcessingDocument.AddMainDocumentPart(); wordStyleApplicator.AddStylesPartToPackage(wordProcessingDocument); wordStyleApplicator.AddStylesWithEffectsPartToPackage(wordProcessingDocument); wordFontApplicator.AddFontTablePartToPackage(wordProcessingDocument); var documentSettingsPart = mainDocumentPart.AddNewPart <DocumentSettingsPart>(); documentSettingsPart.Settings = new Settings(); wordHeaderFooterFormatter.ApplyHeaderAndFooter(wordProcessingDocument); var document = new Document(); var body = new Body(); document.Append(body); var actionVisitor = new ActionVisitor <IDirectoryTreeNode>(node => { var featureDirectoryTreeNode = node as FeatureDirectoryTreeNode; if (featureDirectoryTreeNode != null) { wordFeatureFormatter.Format(body, featureDirectoryTreeNode); } }); features.AcceptVisitor(actionVisitor); mainDocumentPart.Document = document; mainDocumentPart.Document.Save(); } // HACK - Add the table of contents using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(documentFileName, true)) { XElement firstPara = wordProcessingDocument .MainDocumentPart .GetXDocument() .Descendants(W.p) .FirstOrDefault(); TocAdder.AddToc(wordProcessingDocument, firstPara, @"TOC \o '1-2' \h \z \u", null, 4); } }
public void Build(Tree features) { string filename = string.IsNullOrEmpty(this.configuration.SystemUnderTestName) ? "features.docx" : this.configuration.SystemUnderTestName + ".docx"; string documentFileName = this.fileSystem.Path.Combine(this.configuration.OutputFolder.FullName, filename); if (this.fileSystem.File.Exists(documentFileName)) { try { this.fileSystem.File.Delete(documentFileName); } catch (System.IO.IOException ex) { Log.Error("Cannot delete Word file. Is it still open in Word?", ex); return; } } using (var stream = this.fileSystem.File.Create(documentFileName)) using ( WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Create( stream, WordprocessingDocumentType.Document)) { MainDocumentPart mainDocumentPart = wordProcessingDocument.AddMainDocumentPart(); this.wordStyleApplicator.AddStylesPartToPackage(wordProcessingDocument); this.wordStyleApplicator.AddStylesWithEffectsPartToPackage(wordProcessingDocument); this.wordFontApplicator.AddFontTablePartToPackage(wordProcessingDocument); var documentSettingsPart = mainDocumentPart.AddNewPart <DocumentSettingsPart>(); documentSettingsPart.Settings = new Settings(); this.wordHeaderFooterFormatter.ApplyHeaderAndFooter(wordProcessingDocument); var document = new Document(); var body = new Body(); document.Append(body); foreach (var node in features) { var featureDirectoryTreeNode = node as FeatureNode; if (featureDirectoryTreeNode != null) { this.wordFeatureFormatter.Format(body, featureDirectoryTreeNode); } } mainDocumentPart.Document = document; mainDocumentPart.Document.Save(); } // HACK - Add the table of contents using (var stream = this.fileSystem.File.Open(documentFileName, System.IO.FileMode.Open)) using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(stream, true)) { XElement firstPara = wordProcessingDocument .MainDocumentPart .GetXDocument() .Descendants(W.p) .FirstOrDefault(); TocAdder.AddToc(wordProcessingDocument, firstPara, @"TOC \o '1-2' \h \z \u", null, 4); } }
/// <summary> /// This method gets a C# Document instance and prints every element of it to word. /// </summary> /// <param name="oDocument">The instance of Document.cs.</param> /// public void ToFile(Document oDocument) { // Delete the previous file in this dir and copy the new template there. if (File.Exists(wordFilePath)) { File.Delete(wordFilePath); } File.Copy(templateFilePath, wordFilePath, true); // Open the JsonToWord.docx in edit mode. using (WordprocessingDocument wpd = WordprocessingDocument.Open(wordFilePath, true)) { // Gets the body of the document and initializes the WordElementWriter. MainDocumentPart mainPart = wpd.MainDocumentPart; Body body = mainPart.Document.Body; wordWriter = new WordElementWriter(wpd); // Start style definitions // These lines are mandatory as they initialize the various style parts in the doc. The program crashes without these. StyleDefinitionsPart sdp = wpd.MainDocumentPart.StyleDefinitionsPart; if (sdp == null) { sdp = wpd.MainDocumentPart.AddNewPart <StyleDefinitionsPart>(); Styles root = new Styles(); root.Save(sdp); } StylesWithEffectsPart sep = wpd.MainDocumentPart.StylesWithEffectsPart; if (sep == null) { sep = wpd.MainDocumentPart.AddNewPart <StylesWithEffectsPart>(); Styles root = new Styles(); root.Save(sep); } // End of style definitions. // Call to method for replacing the title and the version on the cover page. this.ReplaceCover(wpd, oDocument); // Prints the front page with Title, MAnual version,HMI_Version,date of creation etc. Paragraph p = body.AppendChild(new Paragraph()); Run runTitle = p.AppendChild(new Run(new Word.Text(oDocument.Title))); p.Append(new Run(new Break())); Run runType = p.AppendChild(new Run(new Word.Text(oDocument.Type.ToString() + " Manual"))); p.Append(new Run(new Break())); Run runVersion = p.AppendChild(new Run(new Word.Text(" Manual Version : " + oDocument.Version))); p.Append(new Run(new Break())); Run runHMI_Version = p.AppendChild(new Run(new Word.Text("HMI version :" + oDocument.HMI_Version))); p.Append(new Run(new Break())); Run runDate = p.AppendChild(new Run(new Word.Text("Date : " + oDocument.DateCreated))); p.Append(new Run(new Break())); // The following 2 lines align the text of the previous paragraph to the center of the page. p.ParagraphProperties = new ParagraphProperties(new Justification()); p.ParagraphProperties.Justification.Val = JustificationValues.Center; // The following line breaks to a new page. body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page }))); // Inserts a TOC that includes headings through level 4. // This is the only piece of code that makes use of TocAdder project. XElement firstPara = wpd .MainDocumentPart .GetXDocument() .Descendants(W.p) .Skip(2) .FirstOrDefault(); TocAdder.AddToc(wpd, firstPara, @"TOC \o '1-4' \h \z \u", null, null); // Appends a page break. body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page }))); // Prints the elements. foreach (Chapter chapter in oDocument.ChaptersList) { // Get the chapter's title, place it to the doc and format it to Heading1. Paragraph chapterTitle = body.AppendChild(new Paragraph(new Run(new Word.Text(chapter.Title)))); chapterTitle.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId()); chapterTitle.ParagraphProperties.ParagraphStyleId.Val = "Heading1"; // This paragraph is the one not used in the methods of WordElementWriter. // It is here to serve as a future placeholder of elements, so the methods of WordElementWriter return a paragraph with the printed element. Paragraph paragraph; foreach (var block in chapter.ChapterContents) { foreach (BasicContentBlock element in block.ContentBlocks) { paragraph = new Paragraph(); wordWriter.AppendElement(element, paragraph); body.Append(new Paragraph(new Run(new Break()))); } } } wpd.Save(); } }